项目缺少 node_modules 目录但 npm 安装失败

Project missing node_modules dir but npm intall fails

我的项目缺少 node_modules 目录。我试过 运行 npm install,但它失败了:

...
npm WARN deprecated core-js@2.6.11: core-js@<3.3 is no longer maintained and not recommended for usage due to the number of issues. Because of the V8 engine whims, feature detection in old core-js versions could cause a slowdown up to 100x even if nothing is polyfilled. Please, upgrade your dependencies to the actual version of core-js.
npm ERR! code 1
npm ERR! path C:\users\me\IdeaProjects\portal\cp-frontend\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:\users\me\IdeaProjects\portal\cp-frontend\node_modules\node-gyp\bin\node-gyp.js rebuild --verbose --libsass_ext= --libsass_cflags= --libsass_ldflags= --libsass_library=
npm ERR! Building the projects in this solution one at a time. To enable parallel build, please add the "/m" switch.
npm ERR! Build started 10/1/2021 3:15:35 PM.
npm ERR! Project "C:\users\me\IdeaProjects\portal\cp-frontend\node_modules\node-sass\build\binding.sln" on node 1 (default targets).
npm ERR! ValidateSolutionConfiguration:
npm ERR!   Building solution configuration "Release|x64".
npm ERR! Project "C:\users\me\IdeaProjects\portal\cp-frontend\node_modules\node-sass\build\binding.sln" (1) is building "C:\users\me\IdeaProjects\portal\cp-frontend\node_modules\node-sass\build\binding.vcxproj.metaproj" (2) on node 1 (default targets).
...

我试过了

npm install --global --production windows-build-tools@4.0.0

以管理员身份进入powershell,然后退出并以管理员身份重新进入powershell,然后尝试npm install,但仍然出现此错误。

有什么想法吗?我试过谷歌搜索,建议是 npm install --global --production windows-build-tools@4.0.0(没有 @4.0.0 也失败了)。

查看 packages.json 文件,它列出了这些依赖项。我不确定这是否有帮助。

{
  "name": "customer-portal-frontend",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^4.2.4",
    "@testing-library/react": "^9.3.2",
    "@testing-library/user-event": "^7.1.2",
    "antd": "^4.1.0",
    "autoprefixer": "^9.7.6",
    "axios": "^0.19.2",
    "classnames": "^2.2.6",
    "crypto-js": "^4.0.0",
    "dayjs": "^1.8.23",
    "node-sass": "^4.13.1",
    "normalize.css": "^8.0.1",
    "numeral": "^2.0.6",
    "query-string": "^6.12.1",
    "react": "^16.12.0",
    "react-app-polyfill": "^1.0.6",
    "react-dom": "^16.12.0",
    "react-ga": "^2.7.0",
    "react-helmet": "^6.1.0",
    "react-hook-form": "^5.3.1",
    "react-intercom": "^1.0.15",
    "react-responsive-tabs": "^3.3.0",
    "react-router-dom": "^5.1.2",
    "react-scripts": "^3.4.1",
    "reactn": "^2.2.6",
    "tailwindcss": "^1.2.0",
    "victory": "^34.1.3"
  },
  "scripts": {
    "start": "npm run tailwind:css && react-scripts start",
    "build": "npm run tailwind:css && react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject",
    "tailwind:css": "tailwind build src/tailwind.src.css -c tailwind.js -o src/tailwind.css"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      "ie 11",
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ],
    "development": [
      "ie 11",
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}

如果我在下一级目录中使用 npm start,我会弹出一个关于(但是执行 npm start 可能还为时过早,因为没有 node_modules 目录并且 npm 安装失败)

Script: c:\Users\me\IdeaProjects\portal\cp-frontend\tailwind.js
Line: 32
Char: 7
Error: expected identifier, string or number
Code: 800A0404
Source: Microsoft JScript compilation error

更新 1:

Ran npm install node-gyp in the frontend dir and see similar errors as npm install.  I can run that fine in the backend dir. I don't know why this dir fails.  I have windows command prompt open as administrator in both cases..not sure if that would matter.

npm ERR! code 1
npm ERR! path C:\Users\me\IdeaProjects\portal\cp-frontend\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:\Users\me\IdeaProjects\portal\cp-frontend\node_modules\node-sass\node_modules\node-gyp\bin\node-gyp.js rebuild --verbose --libsass_ext= --libsass_cflags= --libsass_ldflags= --libsass_library=
npm ERR! Building the projects in this solution one at a time. To enable parallel build, please add the "/m" switch.
npm ERR! Build started 10/4/2021 8:35:00 AM.
npm ERR! Project "C:\Users\me\IdeaProjects\portal\cp-frontend\node_modules\node-sass\build\binding.sln" on node 1 (default targets).
npm ERR! ValidateSolutionConfiguration:
npm ERR!   Building solution configuration "Release|x64".
npm ERR! Project "C:\Users\me\IdeaProjects\portal\cp-frontend\node_modules\node-sass\build\binding.sln" (1) is building "C:\Users\me\IdeaProjects\portal\cp-frontend\node_modules\node-sass\build\binding.vcxproj.metaproj" (2) on node 1 (default targets).
npm ERR! Project "C:\Users\me\IdeaProjects\portal\cp-frontend\node_modules\node-sass\build\binding.vcxproj.metaproj" (2) is building "C:\Users\me\IdeaProjects\portal\cp-frontend\node_modules\node-sass\build\src\libsass.vcxproj" (3) on node 1 (default targets).
npm ERR! C:\Users\me\IdeaProjects\portal\cp-frontend\node_modules\node-sass\build\src\libsass.vcxproj(20,3): error MSB4019: The imported project "C:\Microsoft.Cpp.Default.props" was not found. Confirm that the path in the <Import> declaration is correct, and that the file exists on disk.

...

更新2: 尝试了 npm 缓存清理 --force。然后 npm install sass,但它仍在尝试执行 npm install node-sass.

npm ERR! code 1
npm ERR! path C:\Users\me\IdeaProjects\portal\cp-frontend\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:\Users\me\IdeaProjects\portal\cp-frontend\node_modules\node-gyp\bin\node-gyp.js rebuild --verbose --libsass_ext= --libsass_cflags= --libsass_ldflags= --libsass_library=
npm ERR! Building the projects in this solution one at a time. To enable parallel build, please add the "/m" switch.

删除当前文件夹 node_modules 和 运行 npm 作为普通用户,非 administrator/root.

尽可能避免 运行 npm 命令,如 Administrator/Root,这是一种不鼓励的做法,因为第三方的命令可能会被执行。