NPM 安装失败解决方案集合

简单情况

第一种情况

使用命令 npm install -g @vue/cli 或 yarn global add @vue/cli安装完成后,使用 vue -V 提示:

无法加载文件 C:\Users\root\AppData\Roaming\npm\vue.ps1,因为在此系统上禁止运行脚本。

Win10 解决方案:

  • 以管理员身份运行 powershell,不是 cmd
  • 执行:get-ExecutionPolicy,回复Restricted,表示状态是禁止的
  • 执行:set-ExecutionPolicy RemoteSigned
  • 选择 Y

管理员身份运行 powershell 的方法:

  • 左下角搜索输入 powershell
  • 对查找出的项右键 -> 以管理员身份运行

第二种情况

gyp verb check python checking for Python executable "python2" in the PATH 详细报错:

yarn install v1.22.4
[1/4] Resolving packages...
warning node-sass > request@2.88.2: request has been deprecated, see https://github.com/request/request/issues/3142
warning node-sass > node-gyp > request@2.88.2: request has been deprecated, see https://github.com/request/request/issues/3142
[2/4] Fetching packages...
info fsevents@1.2.13: The platform "win32" is incompatible with this module.
info "fsevents@1.2.13" is an optional dependency and failed compatibility check. Excluding it from installation.
info fsevents@2.1.3: The platform "win32" is incompatible with this module.
info "fsevents@2.1.3" is an optional dependency and failed compatibility check. Excluding it from installation.
[3/4] Linking dependencies...
warning " > sass-loader@8.0.2" has unmet peer dependency "webpack@^4.36.0 || ^5.0.0".  
[4/4] Building fresh packages...
[-/4] ⠄ waiting...
[-/4] ⡀ waiting...
[-/4] ⡀ waiting...
error F:\开课吧\10-vuejs-TS\vuets\node_modules\node-sass: Command failed.
Exit code: 1
Command: node scripts/build.js
Arguments:
Directory: F:\开课吧\10-vuejs-TS\vuets\node_modules\node-sass
Output:
Building: C:\Program Files\nodejs\node.exe F:\开课吧\10-vuejs-TS\vuets\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 [
gyp verb cli   'C:\\Program Files\\nodejs\\node.exe',
gyp verb cli   'F:\\开课吧\\10-vuejs-TS\\vuets\\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 verb cli ]
gyp info using node-gyp@3.8.0
gyp info using node@12.14.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 (F:\开课吧\10-vuejs-TS\vuets\node_modules\which\which.js:13:12)
gyp verb `which` failed     at F (F:\开课吧\10-vuejs-TS\vuets\node_modules\which\which.js:68:19)
gyp verb `which` failed     at E (F:\开课吧\10-vuejs-TS\vuets\node_modules\which\which.js:80:29)
gyp verb `which` failed     at F:\开课吧\10-vuejs-TS\vuets\node_modules\which\which.js:89:16
gyp verb `which` failed     at F:\开课吧\10-vuejs-TS\vuets\node_modules\isexe\index.js:42:5
gyp verb `which` failed     at F:\开课吧\10-vuejs-TS\vuets\node_modules\isexe\windows.js:36:5
gyp verb `which` failed     at FSReqCallback.oncomplete (fs.js:158:21)
gyp verb `which` failed  python2 Error: not found: python2
gyp verb `which` failed     at getNotFoundError (F:\开课吧\10-vuejs-TS\vuets\node_modules\which\which.js:13:12)
gyp verb `which` failed     at F (F:\开课吧\10-vuejs-TS\vuets\node_modules\which\which.js:68:19)
gyp verb `which` failed     at E (F:\开课吧\10-vuejs-TS\vuets\node_modules\which\which.js:80:29)
gyp verb `which` failed     at F:\开课吧\10-vuejs-TS\vuets\node_modules\which\which.js:89:16
gyp verb `which` failed     at F:\开课吧\10-vuejs-TS\vuets\node_modules\isexe\index.js:42:5
gyp verb `which` failed     at F:\开课吧\10-vuejs-TS\vuets\node_modules\isexe\windows.js:36:5
gyp verb `which` failed     at FSReqCallback.oncomplete (fs.js:158:21) {
gyp verb `which` failed   stack: 'Error: not found: python2\n' +
gyp verb `which` failed     '    at getNotFoundError (F:\\开课吧\\10-vuejs-TS\\vuets\\node_modules\\which\\which.js:13:12)\n' +
gyp verb `which` failed     '    at F (F:\\开课吧\\10-vuejs-TS\\vuets\\node_modules\\which\\which.js:68:19)\n' +
gyp verb `which` failed     '    at E (F:\\开课吧\\10-vuejs-TS\\vuets\\node_modules\\which\\which.js:80:29)\n' +
gyp verb `which` failed     '    at F:\\开课吧\\10-vuejs-TS\\vuets\\node_modules\\which\\which.js:89:16\n' +
gyp verb `which` failed     '    at F:\\开课吧\\10-vuejs-TS\\vuets\\node_modules\\isexe\\index.js:42:5\n' +
gyp verb `which` failed     '    at F:\\开课吧\\10-vuejs-TS\\vuets\\node_modules\\isexe\\windows.js:36:5\n' +
gyp verb `which` failed     '    at FSReqCallback.oncomplete (fs.js:158:21)',
gyp verb `which` failed   code: 'ENOENT'
gyp verb `which` failed }
gyp verb check python checking for Python executable "python" in the PATH
gyp verb `which` succeeded python E:\Python3.8\python.EXE
gyp ERR! configure error
gyp ERR! stack Error: Command failed: E:\Python3.8\python.EXE -c import sys; print "%s.%s.%s" % sys.version_info[:3];
gyp ERR! stack   File "<string>", line 1
gyp ERR! stack     import sys; print "%s.%s.%s" % sys.version_info[:3];
gyp ERR! stack                       ^
gyp ERR! stack SyntaxError: invalid syntax
gyp ERR! stack
gyp ERR! stack     at ChildProcess.emit (events.js:223:5)
gyp ERR! stack     at maybeClose (internal/child_process.js:1021:16)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:283:5)
gyp ERR! System Windows_NT 10.0.18363
gyp ERR! command "C:\\Program Files\\nodejs\\node.exe" "F:\\开课吧\\10-vuejs-TS\\vuets\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild" "--verbose" "--libsass_ext=" "--libsass_cflags=" "--libsass_ldflags=" "--libsass_library="
gyp ERR! cwd F:\开课吧\10-vuejs-TS\vuets\node_modules\node-sass
gyp ERR! node -v v12.14.1
gyp ERR! node-gyp -v v3.8.0
gyp ERR! not ok

项目使用 yarn 或者 npm 安装依赖包时,并无任何其他操作,突然出现该报错。

Win10 解决方案:
上面报错显示 node-sass 依赖包未能正常安装,推荐检查 python2.7 的环境变量是否正常配置
本质上:该原因的出现是“墙”的问题
1)解决方式(见下方拓展):安装 python2.7(版本必须是2.7),然后配置环境变量即可,环境变量的配置:1. 找到 python2.7 安装地址保存,我的电脑 -> 右键属性 -> 高级系统配置 -> 右下角环境配置 -> 下方系统变量 Path -> 双击后添加保存的地址 ,比如博主自定义安装位置为 E 盘 python2.7 , 将 E:\python2.7 添加到 Path

第三种情况

网络不好时,使用npm安装依赖包可能出现各种问题,例如最常见的:“缺少node-sass”
Win10 解决方案
1)改变npm安装依赖包的地址指向,使用命令 npm i -g cnpm --registry=https://registry.npm.taobao.org ,后续命令将npm 替换成 cnpm 进行安装
2)换一个安装依赖包的工具 npm i yarn -g,比如 yarn 就是一款相当不错的同类型工具,yarn 的命令与 npm 有些不同,可以去yarn官网查询

第四种情况

安装node.js时,npm的环境变量没有配置好(一般该情况是由于修改node.js软件默认安装地址导致的)
使用 npm config get prefix 获取到 npm 的安装地址,复制该地址配置到环境变量内,顺带一提,如果在环境变量中未看到 nodejs 的环境变量,也需要配置上
在这里插入图片描述
在这里插入图片描述

拓展:

python2 安装方法

官方下载地址(window):传送门

1)电脑是64位系统,点击下载如图所示:
在这里插入图片描述
2)python2 安装步骤

  • python-2.7.18.amd64.msi 执行文件下载到电脑之后点击安装
  • 特别要注意选上 pipAdd python.exe to Path,然后一路点“Next”即可完成安装。
  • 默认会安装到 C:\Python27 目录下,这里做修改到D盘下;
  • 若未点击,则需要手动添加环境变量:鼠标右键点击我的电脑—属性—高级系统设置—环境变量—系统变量 Path——编辑,添加 D:\python27D:\python27\Scripts,变量之间以分号 “;” 间隔
  • 安装完成在cmd命令行 输入 python,出现下图所示 >>> 表示配置成功
    在这里插入图片描述

注:D:\python27 是我的python安装目录,D:\python27\Scripts 这个文件是为 pip 文件(python 包管理工具)

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值