节点错误无法读取未定义的属性“解析”

IT技术 javascript node.js reactjs npm node-modules
2021-05-07 11:35:24

我正在使用 reactjs 并且无法使用npm install. 我收到的错误消息是:

npm ERR! Cannot read property 'resolve' of undefined

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/stash/.npm/_logs/2019-03-11T10_07_30_264Z-debug.log

当我这样做时npm install -verbose

npm info it worked if it ends with ok
npm verb cli [ '/usr/local/bin/node',
npm verb cli   '/usr/local/bin/npm',
npm verb cli   'install',
npm verb cli   '-verbose' ]
npm info using npm@6.7.0
npm info using node@v11.10.1
npm verb npm-session 869376d36eca27b5
npm info lifecycle proj-lite@1.0.0-alpha.6~preinstall: proj-lite@1.0.0-alpha.6
npm timing stage:loadCurrentTree Completed in 22ms
npm timing stage:loadIdealTree:cloneCurrentTree Completed in 1ms
npm timing stage:loadIdealTree:loadShrinkwrap Completed in 1231ms
npm timing stage:rollbackFailedOptional Completed in 1ms
npm timing stage:runTopLevelLifecycles Completed in 2661ms
npm verb stack TypeError: Cannot read property 'resolve' of undefined
npm verb stack     at regFetch (/usr/local/lib/node_modules/npm/node_modules/npm-registry-fetch/index.js:76:23)
npm verb stack     at fetchPackument (/usr/local/lib/node_modules/npm/node_modules/pacote/lib/fetchers/registry/packument.js:42:10)
npm verb stack     at packument (/usr/local/lib/node_modules/npm/node_modules/pacote/lib/fetchers/registry/packument.js:20:10)
npm verb stack     at getManifest (/usr/local/lib/node_modules/npm/node_modules/pacote/lib/fetchers/registry/manifest.js:22:10)
npm verb stack     at manifest (/usr/local/lib/node_modules/npm/node_modules/pacote/lib/fetchers/registry/manifest.js:13:10)
npm verb stack     at Object.manifest (/usr/local/lib/node_modules/npm/node_modules/pacote/lib/fetchers/registry/index.js:17:12)
npm verb stack     at Object.Fetcher#manifest [as manifest] (/usr/local/lib/node_modules/npm/node_modules/genfun/lib/genfun.js:15:38)
npm verb stack     at manifest (/usr/local/lib/node_modules/npm/node_modules/pacote/lib/fetch.js:23:18)
npm verb stack     at pinflight (/usr/local/lib/node_modules/npm/node_modules/pacote/manifest.js:24:12)
npm verb stack     at /usr/local/lib/node_modules/npm/node_modules/promise-inflight/inflight.js:29:24
npm verb stack     at Promise._execute (/usr/local/lib/node_modules/npm/node_modules/bluebird/js/release/debuggability.js:313:9)
npm verb stack     at Promise._resolveFromExecutor (/usr/local/lib/node_modules/npm/node_modules/bluebird/js/release/promise.js:483:18)
npm verb stack     at new Promise (/usr/local/lib/node_modules/npm/node_modules/bluebird/js/release/promise.js:79:10)
npm verb stack     at _inflight (/usr/local/lib/node_modules/npm/node_modules/promise-inflight/inflight.js:28:25)
npm verb stack     at /usr/local/lib/node_modules/npm/node_modules/promise-inflight/inflight.js:22:14
npm verb stack     at tryCatcher (/usr/local/lib/node_modules/npm/node_modules/bluebird/js/release/util.js:16:23)
npm verb cwd /var/www/html/lite-proj/lite-proj
npm verb Linux 4.4.0-142-generic
npm verb argv "/usr/local/bin/node" "/usr/local/bin/npm" "install" "-verbose"
npm verb node v11.10.1
npm verb npm  v6.7.0
npm ERR! Cannot read property 'resolve' of undefined
npm verb exit [ 1, true ]
npm timing npm Completed in 3862ms

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/stash/.npm/_logs/2019-03-11T10_12_15_921Z-debug.log

我什至无法卸载 npm 并重新安装它,因为我在尝试卸载时遇到了同样的错误。这里有什么问题,我该如何解决?

4个回答

我使用旧版本的n节点版本切换器 (~v2)进入这种情况,切换到节点 v12。我能够通过切换到n使用sudo npm install -g n.

从那里我可以切换到任何版本的节点并再次使用 npm。

错误的原因可能是全局包和节点版本冲突。我卸载了 node 和 npm,然后重新安装了它们。

删除 nodejs 和相关的包(如 npm):

sudo apt-get purge --auto-remove nodejs

Nodejs 和 npm 会在系统中留下文件,这可能会导致重新安装时出现问题。我也不得不删除它们。

尽管手动删除包是一种不好的做法(这可能会导致包管理器出现问题),但它对我的情况有所帮助。答案显示了如何手动删除 nodejs 和相关文件。

要重新安装它们:

sudo apt-get update
sudo apt-get install nodejs
sudo apt-get install npm

因此,当我尝试在 Windows 10 x64 PC 中运行 React 项目时,我最近遇到了一个非常相似的问题。虽然我安装了 Node.js v12.13.0 (+ npm v6.12.0) x64;我无法执行npm install命令,因为它会给我同样的错误 ( Cannot read property 'resolve' of undefined)。

然后根据这个评论,我卸载了x64版本的Node.js,然后安装了x86版本。在此之后,我可以npm install成功执行命令。

任何遇到此问题并已滚动到这里的人,如果您手动安装节点并且已经nvm安装,那么这可能是问题所在,不要手动执行此操作,也不要卸载 nvm 来解决问题,只需让 nvm 安装如果您没有安装 nvm 并且在尝试升级/降级 node/npm 版本时迷失了方向,请安装nvm并让它为您处理所有这些事情。

使用nvm install x.x.x, then nvm use x.x.x,一切正常。

更多关于 nvm