在服务器上部署一个react写的网站(windows开发,linux部署)的时候,npm install会报很多错:

npm WARN using --force Recommended protections disabled.
npm WARN deprecated @npmcli/move-file@1.1.2: This functionality has been moved to @npmcli/fs
npm WARN deprecated stable@0.1.8: Modern JS already guarantees Array#sort() is a stable sort, so this library is deprecated. See the compatibility table on MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#browser_compatibility
npm WARN deprecated rollup-plugin-terser@7.0.2: This package has been deprecated and is no longer maintained. Please use @rollup/plugin-terser
npm WARN deprecated har-validator@5.1.5: this library is no longer supported
npm WARN deprecated sourcemap-codec@1.4.8: Please use @jridgewell/sourcemap-codec instead
npm WARN deprecated uuid@3.4.0: Please upgrade  to version 7 or higher.  Older versions may use Math.random() in certain circumstances, which is known to be problematic.  See https://v8.dev/blog/math-random for details.
npm WARN deprecated request@2.88.2: request has been deprecated, see https://github.com/request/request/issues/3142
npm WARN deprecated svgo@1.3.2: This SVGO version is no longer supported. Upgrade to v2.x.x.
npm ERR! code 1
npm ERR! path /Projects/CLogin/login-frontend/node_modules/node-sass
npm ERR! command failed
npm ERR! command sh -c -- node scripts/build.js
npm ERR! Building: /program/nvm/nvm-0.39.0/versions/node/v18.12.1/bin/node /Projects/CLogin/login-frontend/node_modules/node-gyp/bin/node-gyp.js rebuild --verbose --libsass_ext= --libsass_cflags= --libsass_ldflags= --libsass_library=
npm ERR! make: Entering directory '/Projects/CLogin/login-frontend/node_modules/node-sass/build'
npm ERR!   g++ -o Release/obj.target/libsass/src/libsass/src/ast.o ../src/libsass/src/ast.cpp '-DNODE_GYP_MODULE_NAME=libsass' '-DUSING_UV_SHARED=1' '-DUSING_V8_SHARED=1' '-DV8_DEPRECATION_WARNINGS=1' '-DV8_DEPRECATION_WARNINGS' '-DV8_IMMINENT_DEPRECATION_WARNINGS' '-D_GLIBCXX_USE_CXX11_ABI=1' '-D_LARGEFILE_SOURCE' '-D_FILE_OFFSET_BITS=64' '-D__STDC_FORMAT_MACROS' '-DOPENSSL_NO_PINSHARED' '-DOPENSSL_THREADS' '-DLIBSASS_VERSION="3.5.5"' -I/root/.cache/node-gyp/18.12.1/include/node -I/root/.cache/node-gyp/18.12.1/src -I/root/.cache/node-gyp/18.12.1/deps/openssl/config -I/root/.cache/node-gyp/18.12.1/deps/openssl/openssl/include -I/root/.cache/node-gyp/18.12.1/deps/uv/include -I/root/.cache/node-gyp/18.12.1/deps/zlib -I/root/.cache/node-gyp/18.12.1/deps/v8/include -I../src/libsass/include  -fPIC -pthread -Wall -Wextra -Wno-unused-parameter -m64 -O3 -fno-omit-frame-pointer -std=gnu++17 -std=c++0x -fexceptions -frtti -MMD -MF ./Release/.deps/Release/obj.target/libsass/src/libsass/src/ast.o.d.raw   -c
npm ERR! make: Leaving directory '/Projects/CLogin/login-frontend/node_modules/node-sass/build'
npm ERR! gyp info it worked if it ends with ok
npm ERR! gyp verb cli [
npm ERR! gyp verb cli   '/program/nvm/nvm-0.39.0/versions/node/v18.12.1/bin/node',
npm ERR! gyp verb cli   '/Projects/CLogin/login-frontend/node_modules/node-gyp/bin/node-gyp.js',
npm ERR! gyp verb cli   'rebuild',
npm ERR! gyp verb cli   '--verbose',
npm ERR! gyp verb cli   '--libsass_ext=',
npm ERR! gyp verb cli   '--libsass_cflags=',
npm ERR! gyp verb cli   '--libsass_ldflags=',
npm ERR! gyp verb cli   '--libsass_library='
npm ERR! gyp verb cli ]
npm ERR! gyp info using node-gyp@8.4.1
npm ERR! gyp info using node@18.12.1 | linux | x64
npm ERR! gyp verb command rebuild []
npm ERR! gyp verb command clean []
npm ERR! gyp verb clean removing "build" directory
npm ERR! gyp verb command configure []
npm ERR! gyp verb find Python Python is not set from command line or npm configuration
npm ERR! gyp verb find Python Python is not set from environment variable PYTHON
npm ERR! gyp verb find Python checking if "python3" can be used
npm ERR! gyp verb find Python - executing "python3" to get executable path
npm ERR! gyp verb find Python - executable path is "/usr/bin/python3"
npm ERR! gyp verb find Python - executing "/usr/bin/python3" to get version
npm ERR! gyp verb find Python - version is "3.6.8"
npm ERR! gyp info find Python using Python version 3.6.8 found at "/usr/bin/python3"
npm ERR! gyp verb get node dir no --target version specified, falling back to host node version: 18.12.1
npm ERR! gyp verb command install [ '18.12.1' ]
npm ERR! gyp verb install input version string "18.12.1"
npm ERR! gyp verb install installing version: 18.12.1
npm ERR! gyp verb install --ensure was passed, so won't reinstall if already installed
npm ERR! gyp verb install version is already installed, need to check "installVersion"
npm ERR! gyp verb got "installVersion" 9
npm ERR! gyp verb needs "installVersion" 9
npm ERR! gyp verb install version is good
npm ERR! gyp verb get node dir target node version installed: 18.12.1
npm ERR! gyp verb build dir attempting to create "build" dir: /Projects/CLogin/login-frontend/node_modules/node-sass/build
npm ERR! gyp verb build dir "build" dir needed to be created? Yes
npm ERR! gyp verb build/config.gypi creating config file
npm ERR! gyp verb build/config.gypi writing out config file: /Projects/CLogin/login-frontend/node_modules/node-sass/build/config.gypi
npm ERR! gyp verb config.gypi checking for gypi file: /Projects/CLogin/login-frontend/node_modules/node-sass/config.gypi
npm ERR! gyp verb common.gypi checking for gypi file: /Projects/CLogin/login-frontend/node_modules/node-sass/common.gypi
npm ERR! gyp verb gyp gyp format was not specified; forcing "make"
npm ERR! gyp info spawn /usr/bin/python3
npm ERR! gyp info spawn args [
npm ERR! gyp info spawn args   '/Projects/CLogin/login-frontend/node_modules/node-gyp/gyp/gyp_main.py',
npm ERR! gyp info spawn args   'binding.gyp',
npm ERR! gyp info spawn args   '-f',
npm ERR! gyp info spawn args   'make',
npm ERR! gyp info spawn args   '-I',
npm ERR! gyp info spawn args   '/Projects/CLogin/login-frontend/node_modules/node-sass/build/config.gypi',
npm ERR! gyp info spawn args   '-I',
npm ERR! gyp info spawn args   '/Projects/CLogin/login-frontend/node_modules/node-gyp/addon.gypi',
npm ERR! gyp info spawn args   '-I',
npm ERR! gyp info spawn args   '/root/.cache/node-gyp/18.12.1/include/node/common.gypi',
npm ERR! gyp info spawn args   '-Dlibrary=shared_library',
npm ERR! gyp info spawn args   '-Dvisibility=default',
npm ERR! gyp info spawn args   '-Dnode_root_dir=/root/.cache/node-gyp/18.12.1',
npm ERR! gyp info spawn args   '-Dnode_gyp_dir=/Projects/CLogin/login-frontend/node_modules/node-gyp',
npm ERR! gyp info spawn args   '-Dnode_lib_file=/root/.cache/node-gyp/18.12.1/<(target_arch)/node.lib',
npm ERR! gyp info spawn args   '-Dmodule_root_dir=/Projects/CLogin/login-frontend/node_modules/node-sass',
npm ERR! gyp info spawn args   '-Dnode_engine=v8',
npm ERR! gyp info spawn args   '--depth=.',
npm ERR! gyp info spawn args   '--no-parallel',
npm ERR! gyp info spawn args   '--generator-output',
npm ERR! gyp info spawn args   'build',
npm ERR! gyp info spawn args   '-Goutput_dir=.'
npm ERR! gyp info spawn args ]
npm ERR! gyp verb command build []
npm ERR! gyp verb build type Release
npm ERR! gyp verb architecture x64
npm ERR! gyp verb node dev dir /root/.cache/node-gyp/18.12.1
npm ERR! gyp verb `which` succeeded for `make` /usr/bin/make
npm ERR! gyp info spawn make
npm ERR! gyp info spawn args [ 'V=1', 'BUILDTYPE=Release', '-C', 'build' ]
npm ERR! make: g++: Command not found
npm ERR! make: *** [src/libsass.target.mk:159: Release/obj.target/libsass/src/libsass/src/ast.o] Error 127
npm ERR! gyp ERR! build error 
npm ERR! gyp ERR! stack Error: `make` failed with exit code: 2
npm ERR! gyp ERR! stack     at ChildProcess.onExit (/Projects/CLogin/login-frontend/node_modules/node-gyp/lib/build.js:194:23)
npm ERR! gyp ERR! stack     at ChildProcess.emit (node:events:513:28)
npm ERR! gyp ERR! stack     at ChildProcess._handle.onexit (node:internal/child_process:291:12)
npm ERR! gyp ERR! System Linux 4.18.0-193.28.1.el8_2.x86_64
npm ERR! gyp ERR! command "/program/nvm/nvm-0.39.0/versions/node/v18.12.1/bin/node" "/Projects/CLogin/login-frontend/node_modules/node-gyp/bin/node-gyp.js" "rebuild" "--verbose" "--libsass_ext=" "--libsass_cflags=" "--libsass_ldflags=" "--libsass_library="
npm ERR! gyp ERR! cwd /Projects/CLogin/login-frontend/node_modules/node-sass
npm ERR! gyp ERR! node -v v18.12.1
npm ERR! gyp ERR! node-gyp -v v8.4.1
npm ERR! gyp ERR! not ok 
npm ERR! Build failed with error code: 1
npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2023-01-13T14_02_28_286Z-debug-0.log

如果你遇见这个问题,我总结了一些解决方案(并行方案,不是步骤):

  1. 重新编译node-sass二进制文件:npm rebuild node-sass
  2. 卸载node-sass: npm uninstall node-sass,安装sass:npm install sass。(用sass替换node-sass)
  3. 删除node_modules目录重新npm install
  4. 删除node_modulespackage-lock.json重新npm install
  5. 查看你的node-sass版本是否对应你的node版本(通常会有报错node sass does not yet support your current environment: linux 64-bit with unsupported runtime)
Node 版本 node-sass 版本
Node 17 7.0+
Node 16 6.0+
Node 15 5.0+
Node 14 4.14+
Node 13 4.13+, <5.0
Node 12 4.12+
Node 11 4.10+, <5.0
Node 10 4.9+, <6.0
Node 8 4.5.3+, <5.0
Node <8 <5.0
  1. 如果上面的方法都不行,那么你一定要仔细核对,开发时使用的node版本是否和部署时的一致。注意,并不是简单查看node版本号,发现一样即可,而是要弄清楚你开发环境安装依赖的时候,使用的node版本是否和服务器上一致! 如果你使用过nvm之类的版本管理工具,那么把每个版本都试试,可能你某些时候切换过,但是忘记了。

发表回复