依赖安装问题

Mar 13, 2024Work

项目开发环境的依赖包如下:

{
// ...
  "devDependencies": {
    "@vue/cli-plugin-babel": "~4.4.0",
    "@vue/cli-plugin-eslint": "~4.4.0",
    "@vue/cli-service": "~4.4.0",
    "babel-eslint": "^10.1.0",
    "chai": "^4.3.4",
    "eslint": "^6.8.0",
    "eslint-plugin-vue": "^7.17.0",
    "fibers": "^5.0.0",
    "node-sass": "^4.9.0",
    "sass": "^1.38.2",
    "sass-loader": "^10.2.0",
    "vue-template-compiler": "^2.6.14",
    "webpack-bundle-analyzer": "^3.9.0"
  },
//  ...
}

{
// ...
  "devDependencies": {
    "@vue/cli-plugin-babel": "~4.4.0",
    "@vue/cli-plugin-eslint": "~4.4.0",
    "@vue/cli-service": "~4.4.0",
    "babel-eslint": "^10.1.0",
    "chai": "^4.3.4",
    "eslint": "^6.8.0",
    "eslint-plugin-vue": "^7.17.0",
    "fibers": "^5.0.0",
    "node-sass": "^4.9.0",
    "sass": "^1.38.2",
    "sass-loader": "^10.2.0",
    "vue-template-compiler": "^2.6.14",
    "webpack-bundle-analyzer": "^3.9.0"
  },
//  ...
}

设置 node、python 版本

主要是安装 node-sassfilbers 的问题,最开始是这种报错,怀疑是 node、python 版本的问题,后来下载了 python2,在项目根目录设置 env.sh.bashrc 文件,设置 python 别名:

alias python="/c/Python27/python.exe" # 根据实际安装位置
alias python3="/c/Python311/python.exe" # 根据实际安装位置
alias python="/c/Python27/python.exe" # 根据实际安装位置
alias python3="/c/Python311/python.exe" # 根据实际安装位置

再在终端运行 source env.sh,更新当前终端的环境变量。

因为许多包是老版本,我使用最新的 node 项目无法运行,因为同事使用 ^16.15.1 没问题,我也换成了这个。

设置构建工具路径

控制好版本以后,多次删除 node_modules 文件夹以及清除 npm 缓存,依然报错,根据经验贴,执行:

npm config set msbuild_path "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\MSBuild.exe" -g
npm config set msbuild_path "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\MSBuild.exe" -g

到此,终于解决所有问题。

注:

  • MSBuildMicrosoft 的一种项目构建工具,它是 Visual Studio 构建系统的一部分。在编译和建立基于 .NET Framework 的应用程序(C#, VB.NET 以及 C++等)。
  • node-sass 依赖的 node-gyp 是一个用于编译 Node.js 原生模块的跨平台工具包,依赖于 Windows 平台上的本地代码(C++),这些代码的构建和编译通常使用 MSBuild 工具。当在 Windows 系统上安装此类 Node.js 模块的时候,npm 需要知道 MSBuild 工具的具体位置才能正确地进行编译和构建,这就需要事先进行 MSBuild 路径设定操作。
  • 因此,通过 npm config set msbuild_path 来设置 MSBuild 的路径,就是为了能让 npm 在执行这些需要编译的模块时能找到正确的构建工具,进而顺利地完成安装操作。没有这个路径的话,可能会在安装许多需要本地编译的 Node.js 模块时遇到问题。