Vue 项目中 NPM INSTALL 命令的问题

Posted

技术标签:

【中文标题】Vue 项目中 NPM INSTALL 命令的问题【英文标题】:Problems with NPM INSTALL command in Vue project 【发布时间】:2021-09-09 08:29:49 【问题描述】:

我有一个我买的模板,但是当我在我的电脑上安装它时,它没有安装依赖项,我运行 NPM INSTALL 命令,然后有一分钟发生错误,它谈到了 python 2 .

过去,我说的是 1 年前我安装它没有问题,但那次我有另一个版本的 node,因为 1 个月前我不得不安装 node js 的 14 版本,因为我想使用 laravel vue 在同一个项目中,它并没有让我工作。

现在我想单独运行项目并且已经拥有整个文档,我收到以下错误。

λ npm install
npm ERR! code 1
npm ERR! path C:\laragon\www\template\node_modules\node-sass
npm ERR! command failed
npm ERR! command C:\WINDOWS\system32\cmd.exe /d /s /c node scripts/build.js
npm ERR! Building: C:\Program Files\nodejs\node.exe C:\laragon\www\template\node_modules\node-gyp\bin\node-gyp.js rebuild --verbose --libsass_ext= --libsass_cflags= --libsass_ldflags= --libsass_library=
npm ERR! gyp info it worked if it ends with ok
npm ERR! gyp verb cli [
npm ERR! gyp verb cli   'C:\\Program Files\\nodejs\\node.exe',
npm ERR! gyp verb cli   'C:\\laragon\\www\\template\\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@3.8.0
npm ERR! gyp info using node@14.16.1 | win32 | 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 check python checking for Python executable "python2" in the PATH
npm ERR! gyp verb `which` failed Error: not found: python2
npm ERR! gyp verb `which` failed     at getNotFoundError (C:\laragon\www\template\node_modules\which\which.js:13:12)
npm ERR! gyp verb `which` failed     at F (C:\laragon\www\template\node_modules\which\which.js:68:19)
npm ERR! gyp verb `which` failed     at E (C:\laragon\www\template\node_modules\which\which.js:80:29)
npm ERR! gyp verb `which` failed     at C:\laragon\www\template\node_modules\which\which.js:89:16
npm ERR! gyp verb `which` failed     at C:\laragon\www\template\node_modules\isexe\index.js:42:5
npm ERR! gyp verb `which` failed     at C:\laragon\www\template\node_modules\isexe\windows.js:36:5
npm ERR! gyp verb `which` failed     at FSReqCallback.oncomplete (fs.js:183:21)
npm ERR! gyp verb `which` failed  python2 Error: not found: python2
npm ERR! gyp verb `which` failed     at getNotFoundError (C:\laragon\www\template\node_modules\which\which.js:13:12)
npm ERR! gyp verb `which` failed     at F (C:\laragon\www\template\node_modules\which\which.js:68:19)
npm ERR! gyp verb `which` failed     at E (C:\laragon\www\template\node_modules\which\which.js:80:29)
npm ERR! gyp verb `which` failed     at C:\laragon\www\template\node_modules\which\which.js:89:16
npm ERR! gyp verb `which` failed     at C:\laragon\www\template\node_modules\isexe\index.js:42:5
npm ERR! gyp verb `which` failed     at C:\laragon\www\template\node_modules\isexe\windows.js:36:5
npm ERR! gyp verb `which` failed     at FSReqCallback.oncomplete (fs.js:183:21) 
npm ERR! gyp verb `which` failed   code: 'ENOENT'
npm ERR! gyp verb `which` failed 
npm ERR! gyp verb check python checking for Python executable "python" in the PATH
npm ERR! gyp verb `which` succeeded python C:\Python39\python.EXE
npm ERR! gyp ERR! configure error
npm ERR! gyp ERR! stack Error: Command failed: C:\Python39\python.EXE -c import sys; print "%s.%s.%s" % sys.version_info[:3];
npm ERR! gyp ERR! stack   File "<string>", line 1
npm ERR! gyp ERR! stack     import sys; print "%s.%s.%s" % sys.version_info[:3];
npm ERR! gyp ERR! stack                       ^
npm ERR! gyp ERR! stack SyntaxError: invalid syntax
npm ERR! gyp ERR! stack
npm ERR! gyp ERR! stack     at ChildProcess.exithandler (child_process.js:308:12)
npm ERR! gyp ERR! stack     at ChildProcess.emit (events.js:315:20)
npm ERR! gyp ERR! stack     at maybeClose (internal/child_process.js:1048:16)
npm ERR! gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:288:5)
npm ERR! gyp ERR! System Windows_NT 10.0.19043
npm ERR! gyp ERR! command "C:\\Program Files\\nodejs\\node.exe" "C:\\laragon\\www\\template\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild" "--verbose" "--libsass_ext=" "--libsass_cflags=" "--libsass_ldflags=" "--libsass_library="
npm ERR! gyp ERR! cwd C:\laragon\www\template\node_modules\node-sass
npm ERR! gyp ERR! node -v v14.16.1
npm ERR! gyp ERR! node-gyp -v v3.8.0
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!     C:\Users\alex_\AppData\Local\npm-cache\_logs\2021-06-26T05_21_01_578Z-debug.log

我正在调查,有人告诉我在管理员模式下运行这些命令:

从 Microsoft 商店下载任何版本的 python 并执行以下命令:

npm install --global windows-build-tools
node-gyp install

我的Nodejs版本是v14.16.1

【问题讨论】:

问题是你的依赖node-sass 需要在你的计算机上安装 Python 和 C++ 构建工具。为了能够访问非 JS 工具(即 Python 等),使用了库 node-gyp 您可以使用 nvm 库(节点版本管理器)管理不同的节点版本。对于 Window Machine:github.com/coreybutler/nvm-windows 更改节点版本后,删除 node_modules 目录和 package-lock.json 然后运行 ​​npm install 我认为它的作品。谢谢。 node-gyp 安装问题是一个常见问题,您可以在此处找到官方解决方案:github.com/nodejs/node-gyp#on-windows 在未来的项目中,我将始终使用dart-sass 而不是node-sass,这不需要任何系统依赖项。 【参考方案1】:

前段时间我想使用vue-paper-dashboard时遇到了同样的问题。

当我执行npm install 时,它给了我同样的错误。做一些研究,我发现这是来自 node-sass 和我的 nodejs 版本的兼容性问题。我更新了开发依赖的版本,它工作了!

以前我的 packages.json 看起来像这样:


    //...
    "devDependencies": 
        "@vue/cli-plugin-babel": "^3.10.0",
        "@vue/cli-plugin-eslint": "^3.10.0",
        "@vue/cli-service": "^3.10.0",
        "@vue/eslint-config-prettier": "^5.0.0",
        "node-sass": "^4.8.3",
        "sass-loader": "^6.0.7",
        "vue-template-compiler": "^2.6.10"
    ,
    //...

现在看起来像这样:


    //...
    "devDependencies": 
        "@vue/cli-plugin-babel": "^4.x.x",
        "@vue/cli-plugin-eslint": "^4.x.x",
        "@vue/cli-service": "^4.x.x",
        "@vue/eslint-config-prettier": "^6.0.0",
        "node-sass": "^6.0.1",
        "sass": "^1.43.2",
        "sass-loader": "^12.2.0",
        "vue-template-compiler": "^2.6.10",
        "webpack": "^5.58.2"
     ,
     //...

不幸的是,我后来遇到了一些兼容性问题……没什么严重的。

Here see node version support policy.

更新

否则,您可以使用nvm 降级您的节点版本。 就我而言,在 Windows 上,我安装了 nvm for windows 并将节点版本从 16.0.1 降级到 14.18.1 并且运行良好!

【讨论】:

以上是关于Vue 项目中 NPM INSTALL 命令的问题的主要内容,如果未能解决你的问题,请参考以下文章

Vue 创建脚手架项目报 command failed: npm install –loglevel error

Vue 创建脚手架项目报 command failed: npm install –loglevel error

解决在部署前端Vue框架项目时,使用npm install和yarn install安装插件报错

解决在部署前端Vue框架项目时,使用npm install和yarn install安装插件报错

npm 在安装 Vue脚手架命令(npm install -g @vue/cli)时报错安装报错--“Unexpected end of JSON input while parsing near&#

vue项目之npm install 的chromedriver