node-sass 安装失败,解决大全

1,279 阅读7分钟

node-sass是什么?

667ff8e3-9606-4ff6-a32e-1c43411063be.jfif

node-sass是一个项目依赖,在一个项目中在使用sass语法的时候,必须通过sass-loader来解析sass,从而使sass语法变成浏览器能够识别的CSS语法,而node-sass模块就是对sass-loader的支持模块,所以不安装node-sass,sass-loader就不能正常工作

node-sass 安装失败的原因

npm 安装 node-sass 依赖时,会从 github.com 上下载 .node 文件。由于国内网络环境的问题,这个下载时间可能会很长,甚至导致超时失败。

解决方案

  • 使用淘宝镜像
npm install -g node-sass --registry=https://registry.npm.taobao.org
  • 使用淘cpm 或者yarn
npm install -g yarn // npm install -g cnpm
yarn add node-sass // cnpm install node-sass
  • 使用配置文件, 在项目根目录创建 .npmrc文件
//  .npmrc
phantomjs_cdnurl=http://cnpmjs.org/downloads
sass_binary_site=https://npm.taobao.org/mirrors/node-sass/
registry=https://registry.npm.taobao.org

保存后 删除之前安装失败的包(第一次安装请跳过此步),然后重新安装。

  • 缺少python环境、build失败等
gyp ERR! configure error
gyp ERR! stack Error: Can't find Python executable "C:\Users\zhuon\AppData\Local\Programs\Python\Python36\python.EXE", you can set the PYTHON env variable.
gyp ERR! stack   at PythonFinder.failNoPython (E:\前端工程化\aaa\\node_modules\node-gyp\lib\configure.js:483:19)
gyp ERR! stack   at PythonFinder.<anonymous> (E:\前端工程化\aaa\\node_modules\node-gyp\lib\configure.js:508:16)
gyp ERR! stack   at E:\前端工程化\aaa\\node_modules\graceful-fs\polyfills.js:284:29
gyp ERR! stack   at FSReqWrap.oncomplete (fs.js:152:21)
gyp ERR! System Windows_NT 10.0.15063
gyp ERR! command "C:\\dev\\nodejs\\node.exe" "E:\\前端工程化\\aaa\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild" "--verbose" "--libsass_ext=" "--libsass_cflags=" "--libsass_ldflags="
"--libsass_library="
gyp ERR! cwd G:\Workspace\ManYan\manyan-nav\node_modules\node-sass
gyp ERR! node -v v14.18.1
gyp ERR! node-gyp -v v7.1.0
gyp ERR! not ok
Build failed
 
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! node-sass@6.0.1 postinstall: `node scripts/build.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the node-sass@6.0.1 postinstall script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
 
npm ERR! A complete log of this run can be found in:
npm ERR!   D:\nodejs\cache\_logs\2021-11-08T16_06_24_298Z-debug.log

安装python环境

  • windows平台缺少编译环境
E:\前端工程化\aaa>npm install node-sass
 
> node-sass@3.8.0 install E:\前端工程化\aaa\node_modules\node-sass
> node scripts/install.js
Cannot download "https://github.com/sass/node-sass/releases/download/v3.8.0/win3
2-x64-48_binding.node":
tunneling socket could not be established, cause=socket hang up
Hint: If github.com is not accessible in your location
      try setting a proxy via HTTP_PROXY, e.g.
      export HTTP_PROXY=http://example.com:1234
or configure npm proxy via
      npm config set proxy http://example.com:8080
> node-sass@3.8.0 postinstall E:\前端工程化\aaa\node_modules\node-sass
> node scripts/build.js
Building: D:\Program Files\nodejs\node.exe E:\前端工程化\aaa\node_modules\node-gyp\bin\n
ode-gyp.js rebuild --verbose --libsass_ext= --libsass_cflags= --libsass_ldflags=
 --libsass_library=
gyp info it worked if it ends with ok
gyp verb cli [ 'D:\\Program Files\\nodejs\\node.exe',
gyp verb cli   'E:\\kibana\\node_modules\\node-gyp\\bin\\node-gyp.js',
gyp verb cli   'rebuild',
gyp verb cli   '--verbose',
gyp verb cli   '--libsass_ext=',
gyp verb cli   '--libsass_cflags=',
gyp verb cli   '--libsass_ldflags=',
gyp verb cli   '--libsass_library=' ]
gyp info using node-gyp@7.1.0
gyp info using node@14.18.1 | win32 | x64
gyp verb command rebuild []
gyp verb command clean []
gyp verb clean removing "build" directory
gyp verb command configure []
gyp verb check python checking for Python executable "python2" in the PATH
gyp verb `which` failed Error: not found: python2
gyp verb `which` failed     at getNotFoundError (E:\前端工程化\aaa\node_modules\which\wh
ich.js:13:12)
gyp verb `which` failed     at F (E:\前端工程化\aaa\node_modules\which\which.js:68:19)
gyp verb `which` failed     at E (E:\前端工程化\aaa\node_modules\which\which.js:80:29)
gyp verb `which` failed     at E:\前端工程化\aaa\node_modules\which\which.js:89:16
gyp verb `which` failed     at E:\前端工程化\aaa\node_modules\isexe\index.js:42:5
gyp verb `which` failed     at E:\前端工程化\aaa\node_modules\isexe\windows.js:36:5
gyp verb `which` failed     at FSReqWrap.oncomplete (fs.js:117:15)
gyp verb `which` failed  python2 { Error: not found: python2
gyp verb `which` failed     at getNotFoundError (E:\前端工程化\aaa\node_modules\which\wh
ich.js:13:12)
gyp verb `which` failed     at F (E:\前端工程化\aaa\node_modules\which\which.js:68:19)
gyp verb `which` failed     at E (E:\前端工程化\aaa\node_modules\which\which.js:80:29)
gyp verb `which` failed     at E:\前端工程化\aaa\node_modules\which\which.js:89:16
gyp verb `which` failed     at E:\前端工程化\aaa\node_modules\isexe\index.js:42:5
gyp verb `which` failed     at E:\前端工程化\aaa\node_modules\isexe\windows.js:36:5
gyp verb `which` failed     at FSReqWrap.oncomplete (fs.js:117:15) code: 'ENOENT
' }
gyp verb check python checking for Python executable "python" in the PATH
gyp verb `which` succeeded python C:\Python27\python.EXE
gyp verb check python version `C:\Python27\python.EXE -c "import platform; print
(platform.python_version());"` returned: "2.7.12\r\n"
gyp verb get node dir no --target version specified, falling back to host node v
ersion: 14.18.1
gyp verb command install [ '14.18.1' ]
gyp verb install input version string "14.18.1"
gyp verb install installing version: 14.18.1
gyp verb install --ensure was passed, so won't reinstall if already installed
gyp verb install version is already installed, need to check "installVersion"
gyp verb got "installVersion" 9
gyp verb needs "installVersion" 9
gyp verb install version is good
gyp verb get node dir target node version installed: 14.18.1
gyp verb build dir attempting to create "build" dir: E:\前端工程化\aaa\node_modules\node
-sass\build
gyp verb build dir "build" dir needed to be created? E:\前端工程化\aaa\node_modules\node
-sass\build
gyp verb Not using VS2017: Could not use PowerShell to find VS2017
gyp verb build/config.gypi creating config file
gyp verb build/config.gypi writing out config file: E:\前端工程化\aaa\node_modules\node-
sass\build\config.gypi
gyp verb config.gypi checking for gypi file: E:\前端工程化\aaa\node_modules\node-sass\co
nfig.gypi
gyp verb common.gypi checking for gypi file: E:\前端工程化\aaa\node_modules\node-sass\co
mmon.gypi
gyp verb gyp gyp format was not specified; forcing "msvs"
gyp info spawn C:\Python27\python.EXE
gyp info spawn args [ 'E:\\前端工程化\\aaa\\node_modules\\node-gyp\\gyp\\gyp_main.py',
gyp info spawn args   'binding.gyp',
gyp info spawn args   '-f',
gyp info spawn args   'msvs',
gyp info spawn args   '-G',
gyp info spawn args   'msvs_version=auto',
gyp info spawn args   '-I',
gyp info spawn args   'E:\\前端工程化\\aaa\\node_modules\\node-sass\\build\\config.gypi',
gyp info spawn args   '-I',
gyp info spawn args   'E:\\前端工程化\\aaa\\node_modules\\node-gyp\\addon.gypi',
gyp info spawn args   '-I',
gyp info spawn args   'D:\\nodejs\\.node-gyp\\7.1.0\\include\\node\\com
mon.gypi',
gyp info spawn args   '-Dlibrary=shared_library',
gyp info spawn args   '-Dvisibility=default',
gyp info spawn args   '-Dnode_root_dir=D:\nodejs\cache\\.node-gyp\\7.1.0',
gyp info spawn args   '-Dnode_gyp_dir=E:\\前端工程化\\aaa\\node_modules\\node-gyp',
gyp info spawn args   '-Dnode_lib_file=node.lib',
gyp info spawn args   '-Dmodule_root_dir=E:\\前端工程化\\aaa\\node_modules\\node-sass',
gyp info spawn args   '-Dnode_engine=v8',
gyp info spawn args   '--depth=.',
gyp info spawn args   '--no-parallel',
gyp info spawn args   '--generator-output',
gyp info spawn args   'E:\\前端工程化\\aaa\\node_modules\\node-sass\\build',
gyp info spawn args   '-Goutput_dir=.' ]
gyp verb command build []
gyp verb build type Release
gyp verb architecture x64
gyp verb node dev dir D:\nodejs\.node-gyp\7.1.0
gyp verb found first Solution file build/binding.sln
gyp verb could not find "msbuild.exe" in PATH - finding location in registry
gyp verb "Release" dir needed to be created? null
gyp verb copying "node.lib" for x64 D:\nodejs\.node-gyp\7.1.0\Release\no
de.lib
gyp info spawn C:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild.exe
gyp info spawn args [ 'build/binding.sln',
gyp info spawn args   '/nologo',
gyp info spawn args   '/p:Configuration=Release;Platform=x64' ]
Build started 2017/4/15 11:57:06.
Project "E:\前端工程化\aaa\node_modules\node-sass\build\binding.sln" on node 1 (default
 targets).
ValidateSolutionConfiguration:
  Building solution configuration "Release|x64".
MSBUILD : error MSB3428: 未能加载 Visual C++ 组件“VCBuild.exe”。要解决此问题,
1) 安装 .NET Fram
ework 2.0 SDK;2) 安装 Microsoft Visual Studio 2005;或 3) 如果将该组件安装到了
其他位置,请将其位置添加到系统
路径中。 [E:\前端工程化\aaa\node_modules\node-sass\build\binding.sln]
Done Building Project "E:\前端工程化\aaa\node_modules\node-sass\build\binding.sln" (def
ault targets) -- FAILED.
Build FAILED.
"E:\前端工程化\aaa\node_modules\node-sass\build\binding.sln" (default target) (1) ->
(_src_\libsass target) ->
  MSBUILD : error MSB3428: 未能加载 Visual C++ 组件“VCBuild.exe”。要解决此问题
,1) 安装 .NET Fr
amework 2.0 SDK;2) 安装 Microsoft Visual Studio 2005;或 3) 如果将该组件安装到
了其他位置,请将其位置添加到
系统路径中。 [E:\前端工程化\aaa\node_modules\node-sass\build\binding.sln]
    0 Warning(s)
    1 Error(s)
Time Elapsed 00:00:00.23
gyp ERR! build error
gyp ERR! stack Error: `C:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild.exe
` failed with exit code: 1
gyp ERR! stack     at ChildProcess.onExit (E:\前端工程化\aaa\node_modules\node-gyp\lib\b
uild.js:285:23)
gyp ERR! stack     at emitTwo (events.js:106:13)
gyp ERR! stack     at ChildProcess.emit (events.js:191:7)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_proces
s.js:204:12)
gyp ERR! System Windows_NT 6.1.7601
gyp ERR! command "D:\\nodejs\\node.exe" "E:\\前端工程化\\aaa\\node_modules
\\node-gyp\\bin\\node-gyp.js" "rebuild" "--verbose" "--libsass_ext=" "--libsass_
cflags=" "--libsass_ldflags=" "--libsass_library="
gyp ERR! cwd E:\前端工程化\aaa\node_modules\node-sass
gyp ERR! node -v v7.1.0
gyp ERR! node-gyp -v v7.1.0
gyp ERR! not ok
Build failed
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@^1.0.0 (node_modules\ba
bel-cli\node_modules\chokidar\node_modules\fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@
1.1.1: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"}
)
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@^1.0.0 (node_modules\ch
okidar\node_modules\fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@
1.1.1: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"}
)
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@^1.0.0 (node_modules\wa
tchpack\node_modules\chokidar\node_modules\fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@
1.1.1: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"}
)
npm WARN grunt-angular-translate@0.3.0 requires a peer of grunt@~0.4.0 but none
was installed.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! node-sass@6.0.1 postinstall: `node scripts/build.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the node-sass@6.0.1 postinstall script 'node scripts/build.js
'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the node-sass package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     node scripts/build.js
npm ERR! You can get information on how to open an issue for this project with:
npm ERR!     npm bugs node-sass
npm ERR! Or if that isn't available, you can get their info via:
npm ERR!     npm owner ls node-sass
npm ERR! There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR!     D:\nodejs\cache\_logs\2021-11-08T16_06_24_298Z-debug.log

这样的方案

npm install -g node-gyp
npm install --g --production windows-build-tools
  • 使用 Windos PowerShell 出现 cnpm - 解决“cnpm:无法加载文件C:*******,因为在此系统上禁止运行脚本……(等有关信息)

win10搜索框中输入Windos PowerShell,然后选择管理员身份运行

在这里插入图片描述

set-ExecutionPolicy RemoteSigned,然后修改权限为A,最后最后通过 get-ExecutionPolicy 查看当前的状态

set-ExecutionPolicy RemoteSigned

在这里插入图片描述