About the solution to the error reported when installing node-sass on the M1 chip of the macOS system

foreword

Recently changed the macOS system, and after pulling the project, the installation dependency reported an error:

Error: Can't find Python executable "python", you can set the PYTHON env var
index.js:1 Uncaught Error: Module build failed: Error: ENOENT: no such file

After referring to this article on installing node-sass on the M1 chip , I learned that the reason is: the version of node and node-sass do not match . The node-sass of the code is version v4.14.1 (requires a version below node15), and my node is v14 (but the M1 chip is only compatible with node v15.3.0 or above, because the lower version of node is not based on the arm64 architecture).

solution:

Solution 1: Let the shell run under Rosetta2 through arch -x86_64 zsh, and the installed npm package will be translated by Rosetta2

#通过命令可以让 shell 运行在Rosetta2下
arch -x86_64 zsh

安装低版本 Node
nvm install v12

Then use cnpm to install [email protected] version separately. At this time, the dependent download error has been resolved.

If an error is reported when the project starts:

Syntax Error: Error: Node Sass does not yet support your current environment: OS X Unsupported architecture (arm64) with Node.js 14.x

This error means that Node Sass does not support the architecture (arm64) of your environment, which is OS X with Node.js version 14.X. Switching to a lower version of Node.js (12.x) can also solve the above error reporting problem.

Solution 2: Compile Node Sass with GCC or clang interpreter

After consulting the information, in addition to the above solution, you can also compile node-sass through other architectures to solve the incompatibility problem.
However, this solution needs to expand additional interpreter tools and is relatively cumbersome to use, so it is not recommended; here I only give a brief introduction to the use (ps: I have not verified this solution).

This solution involves the knowledge of gcc, llvm and clang compilers. For details, please refer to this article: Detailed explanation of the three major compilers: gcc, llvm and clang

Specific operation:
Use the following commands in the terminal to use different architectures (such as x86-64):

llvm-gcc -march=x86-64 -mcpu=sandybridge yourfile.c -o yourfile
clang -march=x86-64 -mcpu=sandybridge yourfile.c -o yourfile

These two instructions are used to compile your program. Specifically, they specified that the compiler compiles your program using the x86-64 instruction set provided by the Sandy Bridge architecture to run on an x86-64 architecture processor.

The above command will compile the file "yourfile.c" and name it "yourfile". The -march parameter specifies the machine architecture, while the -mcpu parameter specifies the actual processor instruction set of the architecture (in this case Shaheqiao).

You can also use alternative statements such as "gcc" or "clang" instead of "llvm-gcc" or "clang" mentioned above, to compile programs with standard GCC or Clang compilers.

other extensions

Install the GCC compiler via the Homebrew command line

The installation process is as follows:

  •   在终端中输入brew install gcc,开始安装gcc包。
    
  •   输入brew link --overwrite gcc,完成gcc安装和链接。
    

To use GCC, you can enter gcc –v in the terminal to obtain GCC version information, or enter gcc myfile.c –o myfile to compile the myfile.c file and generate the myfile file.

An example command to compile Node Sass with the GCC compiler:
gcc ./node-sass.c -o node-sass

It may take a long time to download gcc, because the related dependent packages will be downloaded synchronously. I finally failed to download, and the error is as follows:

Error: gcc: Failed to download resource "gcc"
Download failed: https://ghcr.io/v2/homebrew/core/gcc/blobs/sha256:d0dd262f7d681d5bbcc39bea85d0dd355f2f962060afe4de16432fbe9596bf8c

Then modify the brew source address to continue,
git -C "$(brew --repo)" remote set-url origin [source address]

#默认下载地址
https://mirrors.ustc.edu.cn/brew.git   
#腾讯云下载地址
https://mirrors.cloud.tencent.com/homebrew/brew.git
#华为云下载地址
https://mirrors.huaweicloud.com/homebrew/brew.git

After a long wait, the gcc and llvm compilers still failed to download. . .

Guess you like

Origin blog.csdn.net/var_deng/article/details/128916127