node-sass安裝失敗解決方法總結(jié)(終有一款適合你)
前言
項(xiàng)目中常常遇到node-sass安裝失敗,動(dòng)不動(dòng)就是報(bào)各種錯(cuò)誤。以前我一次也沒(méi)有失敗過(guò),自從系統(tǒng)重裝我的天呀,node-sass就沒(méi)有成功過(guò),我能做的node卸載,sass重裝各種版,以及換了淘寶鏡像和用了vpn都安裝失敗。我也是無(wú)語(yǔ),今天沒(méi)事特意搜搜了看,一次復(fù)制了別人的文章進(jìn)行總結(jié),可以嘗試一下,應(yīng)該是可以的。
(這個(gè)是我在react項(xiàng)目中直接解決的 node-sass方法
http://chabaoo.cn/article/268609.htm
這個(gè)是我的解決react項(xiàng)目中node-sass博客地址.不過(guò)我覺(jué)得可能有更好的方案。)
好了廢話不多說(shuō)哦,總有一款適合你。
我遇到的問(wèn)題一:
npm install 時(shí)偶爾遇到報(bào)錯(cuò):沒(méi)有安裝python或node-sass 安裝失敗的問(wèn)題。
百度之后發(fā)現(xiàn)是被墻了,但根據(jù)百度的方法換了淘寶鏡像和用了vpn都安裝失敗,最后發(fā)現(xiàn)原來(lái)是因?yàn)闆](méi)有卸載之前安裝失敗的包導(dǎo)致的。文中給出了兩種報(bào)錯(cuò)問(wèn)題,使用淘寶鏡像或者使用yarn,找不到當(dāng)前環(huán)境的綁定可以刪除重跑。
node-sass 安裝失敗的原因
npm 安裝 node-sass 依賴時(shí),會(huì)從 github.com 上下載 .node 文件。由于國(guó)內(nèi)網(wǎng)絡(luò)環(huán)境的問(wèn)題,這個(gè)下載時(shí)間可能會(huì)很長(zhǎng),甚至導(dǎo)致超時(shí)失敗。
這是使用 sass 的同學(xué)可能都會(huì)遇到的郁悶的問(wèn)題。
解決方案就是使用其他源,或者使用工具下載,然后將安裝源指定到本地。
解決方法一:使用淘寶鏡像源(推薦)
設(shè)置變量 sass_binary_site,指向淘寶鏡像地址。示例:
執(zhí)行命令
npm i node-sass --sass_binary_site=https://npm.taobao.org/mirrors/node-sass/ // 也可以設(shè)置系統(tǒng)環(huán)境變量的方式。示例 // linux、mac 下 SASS_BINARY_SITE=https://npm.taobao.org/mirrors/node-sass/ npm install node-sass // window 下 set SASS_BINARY_SITE=https://npm.taobao.org/mirrors/node-sass/ && npm install node-sass
或者設(shè)置全局鏡像源:
npm config set sass_binary_site https://npm.taobao.org/mirrors/node-sass/ npm install node-sass
之后再涉及到 node-sass 的安裝時(shí)就會(huì)從淘寶鏡像下載。
解決方法二:使用 cnpm
使用 cnpm 安裝 node-sass 會(huì)默認(rèn)從淘寶鏡像源下載,也是一個(gè)辦法:
執(zhí)行命令
cnpm install node-sass
解決方法三:創(chuàng)建.npmrc文件
在項(xiàng)目根目錄創(chuàng)建.npmrc文件,復(fù)制下面代碼到該文件。
phantomjs_cdnurl=http://cnpmjs.org/downloads sass_binary_site=https://npm.taobao.org/mirrors/node-sass/ registry=https://registry.npm.taobao.org
保存后 刪除之前安裝失敗的包(第一次安裝請(qǐng)?zhí)^(guò)此步) 執(zhí)行命令
npm uninstall node-sass
重新安裝
npm install node-sass
解決方法四:下載 .node 到本地
(在國(guó)內(nèi)使用gulp編譯sass時(shí),需要安裝gulp-sass,而gulp-sass依賴node-sass.
node-sass安裝需要.node文件,國(guó)內(nèi)由于各種原因無(wú)法下載)
方法 :
1、使用命令行安裝node-sass,提升安裝失敗:Cannot download
2、 嘗試使用瀏覽器下載上面提到的地址。
命令行下載方式與瀏覽器下載方式不同。瀏覽器可能可以正常下載
3、下載完成后復(fù)制文件到指定目錄(目錄不要有空格、中文)。
如復(fù)制到E盤(pán)根目錄下
4、回到命令行安裝界面。
設(shè)置SASS_BINARY_PATH環(huán)境變量 set SASS_BINARY_PATH=E:/win32-x64-46_binding.node 文件名根據(jù)自己下載的文件自行修改
然后再次安裝,可以看到我們安裝成功了
5、node-sass安裝時(shí),查看需要的.node文件是否存在。
如果存在就不下載,如果不存在,需要下載。
我們已經(jīng)告訴node-sass文件已經(jīng)下載下來(lái)了,就在E盤(pán),你去哪里找,他一看確實(shí)有,直接使用不再下載。
6、當(dāng)然你可以直接這樣做
去根據(jù)版本號(hào)、系統(tǒng)環(huán)境,選擇下載 .node 文件,然后安裝時(shí),指定變量 sass_binary_path
npm i node-sass --sass_binary_path=/Users/lzwme/Downloads/darwin-x64-48_binding.node
安裝失敗后重新安裝問(wèn)題
之前安裝失敗,再安裝就不去下載了,怎么辦呢?那就先卸載再安裝:
npm uninstall node-sass npm i node-sass --sass_binary_site=https://npm.taobao.org/mirrors/node-sass/
問(wèn)題:[npm install 報(bào)node-sass錯(cuò)誤]
npm WARN Error: EPERM: operation not permitted, scandir 'E:\zkh360\Zkh360.Web.Admin\app\node_modules\har-validator\node_modules' npm WARN { Error: EPERM: operation not permitted, scandir 'E:\zkh360\Zkh360.Web.Admin\app\node_modules\har-validator\node_modules' npm WARN stack: 'Error: EPERM: operation not permitted, scandir \'E:\\zkh360\\Zkh360.Web.Admin\\app\\node_modules\\har-validator\\node_modules\'', npm WARN errno: -4048, npm WARN code: 'EPERM', npm WARN syscall: 'scandir', npm WARN path: 'E:\\zkh360\\Zkh360.Web.Admin\\app\\node_modules\\har-validator\\node_modules' } npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! node-sass@3.13.1 postinstall: `node scripts/build.js` npm ERR! Exit status 1 npm ERR! npm ERR! Failed at the node-sass@3.13.1 postinstall script. npm ERR! This is probably not a problem with npm. There is likely additional logging output above. 大致意思就是EPERM:不允許操作,scandir, node-sass@3.13.1 postinstall腳本失敗。 這可能不是npm的問(wèn)題。上面可能還有其他日志輸出。
問(wèn)題:Node Sass找不到當(dāng)前環(huán)境的綁定
Node Sass could not find a binding for your current environment
ERROR in Missing binding /Users/warren/Sites/random-docs/ my-cms/node_modules/node-sass/vendor/darwin-x64-11/binding.node Node Sass could not find a binding for your current environment: OS X 64-bit with Node 0.10.x
解決方案。
刪除node_modules并運(yùn)行npm install然后npm rebuild node-sass 什么都不做。
stackoverflow原文
如果這個(gè) .node 還不能解決用這個(gè)方法。直接搞定。
1 查看版本號(hào)
node -p "[process.platform, process.arch, process.versions.modules].join('-')"
配置 變量 sass_binary_path:npm 命令或者yarn命令
npm config set sass-binary-path e:/web/win32-x64-93_binding.node yarn config set sass-binary-path e:/web/win32-x64-93_binding.node
解決方法五:使用梯子 來(lái)源
假設(shè)你的梯子在你本地機(jī)器上開(kāi)啟了一個(gè)第三方服務(wù)器 127.0.0.1:8080,那么只需按照下面的方法配置一下就能正常安裝 node-sass 了(如果你開(kāi)啟的是 PAC 模式而不是全局模式,那還需要將 s3.amazonaws.com 加入 PAC 列表):
npm config set proxy http://127.0.0.1:8080 npm i node-sass 下載完成后刪除 http 代理 npm config delete proxy
我遇到的問(wèn)題二
開(kāi)始的時(shí)候引入別人的一個(gè)項(xiàng)目 咱們一般都是執(zhí)行命令 npm install,或者 yarn命令會(huì)自動(dòng)下載node-modules包 npm run dev 或者要yarn start 啟動(dòng)項(xiàng)目 但是往往就在安裝包的時(shí)候就報(bào)錯(cuò)了,各種各樣的,解決一個(gè)來(lái)一個(gè),把管理員權(quán)限配置 了還不行,有的還說(shuō)是版本的問(wèn)題,改了package.json文件還不行。 > node build/dev-server.js Listening at http://localhost:8888 webpack: wait until bundle finished: /index.html webpack built 5301489d16ee90d86896 in 3165ms Hash: 5301489d16ee90d86896 Version: webpack 1.15.0 Time: 3165ms Asset Size Chunks Chunk Names app.js 1.96 MB 0 app ERROR in Cannot find module 'node-sass' @ ./src/common/scss/index.scss 4:14-124 13:2-17:4 14:20-130 ERROR in Cannot find module 'node-sass' @ ./~/vue-style-loader!./~/css-loader!./~/vue-loader/lib/style-rewriter.js?id=data-v-e0c3a2e6!./~/sass-loader!./~/vue-loader/lib/selector.js?type=styles&index=0!./src/App.vue 4:14-240 13:2-17:4 14:20-246 ERROR in Cannot find module 'node-sass' @ ./~/vue-style-loader!./~/css-loader!./~/vue-loader/lib/style-rewriter.js?id=data-v-690f3aee&scoped=true!./~/sass-loader!./~/vue-loader/lib/selector.js?type=styles&index=0!./src/components/ratings/ratings.vue 4:14-280 13:2-17:4 14:20-286 ERROR in Cannot find module 'node-sass' @ ./~/vue-style-loader!./~/css-loader!./~/vue-loader/lib/style-rewriter.js?id=data-v-2ac60e2e&scoped=true!./~/sass-loader!./~/vue-loader/lib/selector.js?type=styles&index=0!./src/components/goods/goods.vue 4:14-278 13:2-17:4 14:20-284 ERROR in Cannot find module 'node-sass' @ ./~/vue-style-loader!./~/css-loader!./~/vue-loader/lib/style-rewriter.js?id=data-v-17cac44c&scoped=true!./~/sass-loader!./~/vue-loader/lib/selector.js?type=styles&index=0!./src/components/seller/seller.vue 4:14-279 13:2-17:4 14:20-285 ERROR in Cannot find module 'node-sass' @ ./~/vue-style-loader!./~/css-loader!./~/vue-loader/lib/style-rewriter.js?id=data-v-108aa294&scoped=true!./~/sass-loader!./~/vue-loader/lib/selector.js?type=styles&index=0!./src/components/split/split.vue 4:14-278 13:2-17:4 14:20-284 ERROR in Cannot find module 'node-sass' @ ./~/vue-style-loader!./~/css-loader!./~/vue-loader/lib/style-rewriter.js?id=data-v-13f047fa&scoped=true!./~/sass-loader!./~/vue-loader/lib/selector.js?type=styles&index=0!./src/components/star/star.vue 4:14-277 13:2-17:4 14:20-283 ERROR in Cannot find module 'node-sass' @ ./~/vue-style-loader!./~/css-loader!./~/vue-loader/lib/style-rewriter.js?id=data-v-bbdb154c&scoped=true!./~/sass-loader!./~/vue-loader/lib/selector.js?type=styles&index=0!./src/components/ratingselect/ratingselect.vue 4:14-285 13:2-17:4 14:20-291 ERROR in Cannot find module 'node-sass' @ ./~/vue-style-loader!./~/css-loader!./~/vue-loader/lib/style-rewriter.js?id=data-v-652a5408&scoped=true!./~/sass-loader!./~/vue-loader/lib/selector.js?type=styles&index=0!./src/components/cartcontrol/cartcontrol.vue 4:14-284 13:2-17:4 14:20-290 ERROR in Cannot find module 'node-sass' @ ./~/vue-style-loader!./~/css-loader!./~/vue-loader/lib/style-rewriter.js?id=data-v-1728dbcc&scoped=true!./~/sass-loader!./~/vue-loader/lib/selector.js?type=styles&index=0!./src/components/header/header.vue 4:14-279 13:2-17:4 14:20-285 ERROR in Cannot find module 'node-sass' @ ./~/vue-style-loader!./~/css-loader!./~/vue-loader/lib/style-rewriter.js?id=data-v-40efc97a&scoped=true!./~/sass-loader!./~/vue-loader/lib/selector.js?type=styles&index=0!./src/components/shopcart/shopcart.vue 4:14-281 13:2-17:4 14:20-287 ERROR in Cannot find module 'node-sass' @ ./~/vue-style-loader!./~/css-loader!./~/vue-loader/lib/style-rewriter.js?id=data-v-617e6f7a&scoped=true!./~/sass-loader!./~/vue-loader/lib/selector.js?type=styles&index=0!./src/components/food/food.vue 4:14-277 13:2-17:4 14:20-283 Child html-webpack-plugin for "index.html": Asset Size Chunks Chunk Names index.html 1.47 MB 0 webpack: Failed to compile.
npm i node-sass -s安裝
報(bào)錯(cuò)信息
E:\kibana>npm install node-sass > node-sass@3.8.0 install E:\kibana\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:\kibana\node_modules\node-sass > node scripts/build.js Building: D:\Program Files\nodejs\node.exe E:\kibana\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@3.6.0 gyp info using node@6.2.0 | 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:\kibana\node_modules\which\wh ich.js:13:12) gyp verb `which` failed at F (E:\kibana\node_modules\which\which.js:68:19) gyp verb `which` failed at E (E:\kibana\node_modules\which\which.js:80:29) gyp verb `which` failed at E:\kibana\node_modules\which\which.js:89:16 gyp verb `which` failed at E:\kibana\node_modules\isexe\index.js:42:5 gyp verb `which` failed at E:\kibana\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:\kibana\node_modules\which\wh ich.js:13:12) gyp verb `which` failed at F (E:\kibana\node_modules\which\which.js:68:19) gyp verb `which` failed at E (E:\kibana\node_modules\which\which.js:80:29) gyp verb `which` failed at E:\kibana\node_modules\which\which.js:89:16 gyp verb `which` failed at E:\kibana\node_modules\isexe\index.js:42:5 gyp verb `which` failed at E:\kibana\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: 6.2.0 gyp verb command install [ '6.2.0' ] gyp verb install input version string "6.2.0" gyp verb install installing version: 6.2.0 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: 6.2.0 gyp verb build dir attempting to create "build" dir: E:\kibana\node_modules\node -sass\build gyp verb build dir "build" dir needed to be created? E:\kibana\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:\kibana\node_modules\node- sass\build\config.gypi gyp verb config.gypi checking for gypi file: E:\kibana\node_modules\node-sass\co nfig.gypi gyp verb common.gypi checking for gypi file: E:\kibana\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:\\kibana\\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:\\kibana\\node_modules\\node-sass\\build\\config.gypi', gyp info spawn args '-I', gyp info spawn args 'E:\\kibana\\node_modules\\node-gyp\\addon.gypi', gyp info spawn args '-I', gyp info spawn args 'C:\\Users\\15061857\\.node-gyp\\6.2.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=C:\\Users\\15061857\\.node-gyp\\6.2.0', gyp info spawn args '-Dnode_gyp_dir=E:\\kibana\\node_modules\\node-gyp', gyp info spawn args '-Dnode_lib_file=node.lib', gyp info spawn args '-Dmodule_root_dir=E:\\kibana\\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:\\kibana\\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 C:\Users\15061857\.node-gyp\6.2.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 C:\Users\15061857\.node-gyp\6.2.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:\kibana\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”。要解決此問(wèn)題, 1) 安裝 .NET Fram ework 2.0 SDK;2) 安裝 Microsoft Visual Studio 2005;或 3) 如果將該組件安裝到了 其他位置,請(qǐng)將其位置添加到系統(tǒng) 路徑中。 [E:\kibana\node_modules\node-sass\build\binding.sln] Done Building Project "E:\kibana\node_modules\node-sass\build\binding.sln" (def ault targets) -- FAILED. Build FAILED. "E:\kibana\node_modules\node-sass\build\binding.sln" (default target) (1) -> (_src_\libsass target) -> MSBUILD : error MSB3428: 未能加載 Visual C++ 組件“VCBuild.exe”。要解決此問(wèn)題 ,1) 安裝 .NET Fr amework 2.0 SDK;2) 安裝 Microsoft Visual Studio 2005;或 3) 如果將該組件安裝到 了其他位置,請(qǐng)將其位置添加到 系統(tǒng)路徑中。 [E:\kibana\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:\kibana\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:\\Program Files\\nodejs\\node.exe" "E:\\kibana\\node_modules \\node-gyp\\bin\\node-gyp.js" "rebuild" "--verbose" "--libsass_ext=" "--libsass_ cflags=" "--libsass_ldflags=" "--libsass_library=" gyp ERR! cwd E:\kibana\node_modules\node-sass gyp ERR! node -v v6.2.0 gyp ERR! node-gyp -v v3.6.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@3.8.0 postinstall: `node scripts/build.js` npm ERR! Exit status 1 npm ERR! npm ERR! Failed at the node-sass@3.8.0 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! C:\Users\15061857\AppData\Roaming\npm-cache\_logs\2017-04-15T03_57_ 07_545Z-debug.log
看著這報(bào)錯(cuò)信息,看到有文件下載出現(xiàn)404,然后我又去找文件,結(jié)果搞hosts文件,我以為是python問(wèn)題,然后重裝各種版本python…
花了3小時(shí),試了網(wǎng)上無(wú)數(shù)種答案,沒(méi)有可以解決的
解決方案一(有可能你還是不會(huì)成功,因?yàn)槲乙苍嚵耍@種方法以前是可以的)
將sass-loader和node-sass的版本號(hào)提高到最新版,目前的最新版如下(2019年7月12日)
“sass-loader”: “^7.1.0”,
“node-sass”: “^4.12.0”,
如圖所示,把package.json文件修改一下
然后再執(zhí)行如下命令安裝
npm i sass-loader npm i node-sass
2020-05-06更新
這個(gè)問(wèn)題出現(xiàn)的原因往往是 Node-sass 的版本問(wèn)題,可能的一個(gè)原因是:因?yàn)榘姹咎纤詴?huì)導(dǎo)致有的依賴項(xiàng)已經(jīng)找不到了,公司有一個(gè)4年前的老項(xiàng)目就是這種問(wèn)題,上面修改Node-sass的版本為4.12并不一定適合所有項(xiàng)目,可以多測(cè)試幾個(gè)版本,如果上面的版本失敗了可以試一試這個(gè)版本
"node-sass": "^4.13.1", "sass-loader": "^4.1.1",
2021-06-06更新
Nodejs版本切換為13.6,這樣才可以匹配上node sass的版本
簡(jiǎn)單的方法是,先卸載nodejs,然后安裝13.6版本的
Nodejs13.6版本官網(wǎng)下載
仍然失敗怎么辦?那就試試第二種解決方案
發(fā)現(xiàn): gyp ERR! build error 應(yīng)該是沒(méi)有安裝什么
解決辦法二:
主要是windows平臺(tái)缺少編譯環(huán)境,
1、先運(yùn)行:
npm install -g node-gyp
2、然后運(yùn)行:
npm install --global --production windows-build-tools 可以自動(dòng)安裝跨平臺(tái)的編譯器:gym注:第二句執(zhí)行下載好msi文件卡著不動(dòng)不安裝 , 手動(dòng)去對(duì)應(yīng)的目錄底下安裝一下 在執(zhí)行一遍。
本以為萬(wàn)事大吉 一執(zhí)行 npm install node-sass --save-dev 悲劇的事情上演了
> node-sass@3.13.1 install E:\web_hwf\Vue\02app\node_modules\node-sass > node scripts/install.js Downloading binary from https://github.com/sass/node-sass/releases/download/v3.13.1/win32-x64-64_binding.node Cannot download "https://github.com/sass/node-sass/releases/download/v3.13.1/win32-x64-64_binding.node": HTTP error 404 Not Found 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.13.1 postinstall E:\web_hwf\Vue\02app\node_modules\node-sass > node scripts/build.js Building: E:\26_nodejs\node.exe E:\web_hwf\Vue\02app\node_modules\node-gyp\bin\node-gyp.js rebuild --verbose --libsass_ext= --libsass_cflags= --libsass_ldflags= --libsass_library= gyp info it worked if it ends with ok gyp verb cli [ 'E:\\26_nodejs\\node.exe', gyp verb cli 'E:\\web_hwf\\Vue\\02app\\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@3.6.2 gyp info using node@10.1.0 | 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 "C:\Python27\python.exe" in the PATH gyp verb `which` succeeded C:\Python27\python.exe C:\Python27\python.exe gyp verb check python version `C:\Python27\python.exe -c "import platform; print(platform.python_version());"` returned: "2.7.14\r\n" gyp verb get node dir no --target version specified, falling back to host node version: 10.1.0 gyp verb command install [ '10.1.0' ] gyp verb install input version string "10.1.0" gyp verb install installing version: 10.1.0 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: 10.1.0 gyp verb build dir attempting to create "build" dir: E:\web_hwf\Vue\02app\node_modules\node-sass\build gyp verb build dir "build" dir needed to be created? E:\web_hwf\Vue\02app\node_modules\node-sass\build gyp verb build/config.gypi creating config file gyp verb build/config.gypi writing out config file: E:\web_hwf\Vue\02app\node_modules\node-sass\build\config.gypi gyp verb config.gypi checking for gypi file: E:\web_hwf\Vue\02app\node_modules\node-sass\config.gypi gyp verb common.gypi checking for gypi file: E:\web_hwf\Vue\02app\node_modules\node-sass\common.gypi gyp verb gyp gyp format was not specified; forcing "msvs" gyp info spawn C:\Python27\python.exe gyp info spawn args [ 'E:\\web_hwf\\Vue\\02app\\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=2015', gyp info spawn args '-I', gyp info spawn args 'E:\\web_hwf\\Vue\\02app\\node_modules\\node-sass\\build\\config.gypi', gyp info spawn args '-I', gyp info spawn args 'E:\\web_hwf\\Vue\\02app\\node_modules\\node-gyp\\addon.gypi', gyp info spawn args '-I', gyp info spawn args 'C:\\Users\\Administrator\\.node-gyp\\10.1.0\\include\\node\\common.gypi', gyp info spawn args '-Dlibrary=shared_library', gyp info spawn args '-Dvisibility=default', gyp info spawn args '-Dnode_root_dir=C:\\Users\\Administrator\\.node-gyp\\10.1.0', gyp info spawn args '-Dnode_gyp_dir=E:\\web_hwf\\Vue\\02app\\node_modules\\node-gyp', gyp info spawn args '-Dnode_lib_file=C:\\Users\\Administrator\\.node-gyp\\10.1.0\\<(target_arch)\\node.lib', gyp info spawn args '-Dmodule_root_dir=E:\\web_hwf\\Vue\\02app\\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:\\web_hwf\\Vue\\02app\\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 C:\Users\Administrator\.node-gyp\10.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 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' ] Building the projects in this solution one at a time. To enable parallel build, please add the "/m" switch. Build started 2018/6/6 15:52:53. Project "E:\web_hwf\Vue\02app\node_modules\node-sass\build\binding.sln" on node 1 (default targets). ValidateSolutionConfiguration: Building solution configuration "Release|x64". Project "E:\web_hwf\Vue\02app\node_modules\node-sass\build\binding.sln" (1) is building "E:\web_hwf\Vue\02app\node_modules\node-sass\build\binding.vcxproj.metaproj" (2) on node 1 (default targets). Project "E:\web_hwf\Vue\02app\node_modules\node-sass\build\binding.vcxproj.metaproj" (2) is building "E:\web_hwf\Vue\02app\node_modules\node-sass\build\src\libsass.vcxproj" (3) on node 1 (default targets). E:\web_hwf\Vue\02app\node_modules\node-sass\build\src\libsass.vcxproj(20,3): error MSB4019: The imported project "E:\Microsoft.Cpp.Default.props" was not found. Confirm that the path in the <Import> declaration is correct, and that the file exists on disk. Done Building Project "E:\web_hwf\Vue\02app\node_modules\node-sass\build\src\libsass.vcxproj" (default targets) -- FAILED. Done Building Project "E:\web_hwf\Vue\02app\node_modules\node-sass\build\binding.vcxproj.metaproj" (default targets) -- FAILED. Done Building Project "E:\web_hwf\Vue\02app\node_modules\node-sass\build\binding.sln" (default targets) -- FAILED. Build FAILED. "E:\web_hwf\Vue\02app\node_modules\node-sass\build\binding.sln" (default target) (1) -> "E:\web_hwf\Vue\02app\node_modules\node-sass\build\binding.vcxproj.metaproj" (default target) (2) -> "E:\web_hwf\Vue\02app\node_modules\node-sass\build\src\libsass.vcxproj" (default target) (3) -> E:\web_hwf\Vue\02app\node_modules\node-sass\build\src\libsass.vcxproj(20,3): error MSB4019: The imported project "E:\Microsoft.Cpp.Default.props" was not found. Confirm that the path in the <Import> declaration is correct, and that the file exists on disk. 0 Warning(s) 1 Error(s) Time Elapsed 00:00:00.07 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:\web_hwf\Vue\02app\node_modules\node-gyp\lib\build.js:258:23) gyp ERR! stack at ChildProcess.emit (events.js:182:13) gyp ERR! stack at Process.ChildProcess._handle.onexit (internal/child_process.js:235:12) gyp ERR! System Windows_NT 6.1.7601 gyp ERR! command "E:\\26_nodejs\\node.exe" "E:\\web_hwf\\Vue\\02app\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild" "--verbose" "--libsass_ext=" "--libsass_cflags=" "--libsass_ldflags=" "--libsass_library=" gyp ERR! cwd E:\web_hwf\Vue\02app\node_modules\node-sass gyp ERR! node -v v10.1.0 gyp ERR! node-gyp -v v3.6.2 gyp ERR! not ok Build failed with error code: 1 npm WARN rollback Rolling back source-map@0.4.4 failed (this is probably harmless): EPERM: operation not permitted, lstat 'E:\web_hwf\Vue\02app\node_modules\scss-tokenizer\node_modules' npm WARN eslint-config-standard@6.2.1 requires a peer of eslint-plugin-promise@>=3.3.0 but none is installed. You must install peer dependencies yourself. npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.4 (node_modules\fsevents): npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.4: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"}) npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! node-sass@3.13.1 postinstall: `node scripts/build.js` npm ERR! Exit status 1 npm ERR! npm ERR! Failed at the node-sass@3.13.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! C:\Users\Administrator\AppData\Roaming\npm-cache\_logs\2018-06-06T07_52_55_703Z-debug.log
解決辦法:
保存后 刪除之前安裝失敗的包(第一次安裝請(qǐng)?zhí)^(guò)此步)
npm uninstall node-sass
重新安裝
npm install node-sass
解決辦法三,重新配置node-sass
通常在各種方式都試過(guò)之后還是不行的時(shí)候,我們要考慮下是不是遺忘了哪個(gè)細(xì)節(jié)?
緩存!
沒(méi)錯(cuò),可能我們思路本來(lái)都是對(duì)的,但是實(shí)際在運(yùn)行時(shí)并沒(méi)有按照我們預(yù)期的去執(zhí)行,緩存就是可能導(dǎo)致這種情況的因素之一。
現(xiàn)在前端安裝依賴大家常用的工具是npm和yarn
而這兩者都有各自的緩存機(jī)制,有時(shí)候,第一次安裝node-sass失敗后,可能這個(gè)包就已經(jīng)被緩存了,導(dǎo)致我們后續(xù)再進(jìn)行安裝時(shí),就
不再會(huì)從遠(yuǎn)程服務(wù)器或者是代理去拉取了。這就導(dǎo)致了一個(gè)死循環(huán),永遠(yuǎn)拉不到正確的文件。
成因
其實(shí)node-sass安裝不成功的跟本原因大家基本都清楚,就是他在安裝過(guò)程中,需要用到一個(gè)二進(jìn)制文件:binding.node,但是坑爹的是,這個(gè)二進(jìn)制文件在“外面”,也就是需要科學(xué)上網(wǎng)才能獲取,這就導(dǎo)致了絕大部分的安裝失敗。
除此之外,其實(shí)還有一些是因?yàn)橥ㄟ^(guò)設(shè)置各種代理之后,仍然不能獲取到正確的binding.node文件,具體原因可能多種多樣,但其實(shí)我們不需要關(guān)心這些問(wèn)題,我們知道了問(wèn)題的成因,那么直接從根源上解決問(wèn)題即可。
解決辦法
分析清除了成因之后,解決問(wèn)題就相對(duì)容易多了。既然node-sass總是從緩存拉取,那么我們就在緩存那里,直接把正確的文件放在那里,這樣在安裝node-sass時(shí),直接從緩存中拉倒了正確的binding.node文件,問(wèn)題便得以解決。
所以我們要做以下幾件事情:
1、檢測(cè)當(dāng)前環(huán)境中,node-sass要使用的binding.node對(duì)應(yīng)的版本
2、獲取到對(duì)應(yīng)版本的binding.node
3、將獲取到的binding.node放到緩存中
操作步驟
1、查看當(dāng)前環(huán)境適用的binding.node
node -p "[process.platform, process.arch, process.versions.modules].join('-')"
2、到這里下載對(duì)應(yīng)版本的文件。鏈接地址: https://github.com/sass/node-sass/releases
3、將下載好的文件放入緩存目錄中
yarn緩存目錄:C:Users你的用戶名-AppData下的文件夾Local下的文件夾Yarn下的文件夾Cache
npm緩存目錄:C:Users你的用戶名-AppData下的文件夾Roaming下的文件夾npm-cache下的node-sass
注意
node-sass最終安裝正確的情況下,目錄結(jié)構(gòu)是這樣的:
node-sass| -... -vender| -win32-x64-93 -binding.node
所以為了保證正確安裝,我們需要按照這個(gè)格式把下載到的文件放到緩存的目錄下。
這里,win32-x64-93在不同的環(huán)境下可能有不同的版本,要注意看一下你本地對(duì)應(yīng)的是什么版本。
查看版本上面給出了一個(gè)命令
(node -p "[process.platform, process.arch, process.versions.modules].join('-')"),可以試一下。
查看版本還有一種方式,就是當(dāng)你第一次安裝失敗時(shí),你去node-modules中去找node-sass目錄,然后找到vender目錄,進(jìn)去就能看到你的環(huán)境下,building.node對(duì)應(yīng)的版本了。
我本地的目錄結(jié)構(gòu)如下,供參考:
4、重新安裝node-sass
總結(jié)
到此這篇關(guān)于node-sass安裝失敗解決方法的文章就介紹到這了,更多相關(guān)node-sass安裝失敗解決內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Node.js web 應(yīng)用如何封裝到Docker容器中
這篇文章主要介紹了Node.js web 應(yīng)用如何封裝到Docker容器中,幫助大家更好的學(xué)習(xí)node.js和使用docker容器,感興趣的朋友可以了解下2020-09-09pnpm workspace管理monorepo項(xiàng)目使用過(guò)程詳解
這篇文章主要為大家介紹了pnpm workspace管理monorepo項(xiàng)目使用過(guò)程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10從零開(kāi)始學(xué)習(xí)Node.js系列教程之基于connect和express框架的多頁(yè)面實(shí)現(xiàn)數(shù)學(xué)運(yùn)算示例
這篇文章主要介紹了Node.js基于connect和express框架的多頁(yè)面實(shí)現(xiàn)數(shù)學(xué)運(yùn)算,簡(jiǎn)單講述了connect和express框架的原理及數(shù)學(xué)運(yùn)算相關(guān)操作技巧,需要的朋友可以參考下2017-04-04Node.js實(shí)現(xiàn)修改文件字符集功能的具體過(guò)程
在日常生活、工作中,我們經(jīng)常會(huì)遇到需要處理不同編碼格式的文件,有時(shí),在嘗試打開(kāi)這些文件時(shí)會(huì)遇到亂碼,原因通常是文件的編碼與我們使用的文本編輯器或編程語(yǔ)言的默認(rèn)編碼不匹配,這篇文章將介紹Node.js修改文件字符集的實(shí)現(xiàn)思路和具體實(shí)現(xiàn)過(guò)程2024-08-08node.js基于dgram數(shù)據(jù)報(bào)模塊創(chuàng)建UDP服務(wù)器和客戶端操作示例
這篇文章主要介紹了node.js基于dgram數(shù)據(jù)報(bào)模塊創(chuàng)建UDP服務(wù)器和客戶端操作,結(jié)合實(shí)例形式分析了node.js使用dgram數(shù)據(jù)報(bào)模塊創(chuàng)建UDP服務(wù)器和客戶端,以及進(jìn)行UDP廣播、組播相關(guān)操作技巧,需要的朋友可以參考下2020-02-02