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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/55099569/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 10:29:46  来源:igfitidea点击:

Node error Cannot read property 'resolve' of undefined

javascriptnode.jsreactjsnpmnode-modules

提问by zinngg

I am using reactjs and am unable to install any packages using npm install. The error message I'm getting is :

我正在使用 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

When I do npm install -verbose:

当我这样做时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 [email protected]
npm info using [email protected]
npm verb npm-session 869376d36eca27b5
npm info lifecycle [email protected]~preinstall: [email protected]
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

I am not even able to uninstall npm and reinstall it as I am getting the same error when I try to uninstall. What is the issue here and how do I fix it?

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

采纳答案by zinngg

The reason for the error was probably conflicting global packages and node versions. I uninstalled node and npm and then reinstalled them.

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

To remove nodejs and associated packages (like npm):

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

sudo apt-get purge --auto-remove nodejs

Nodejs and npm will leave files in the system which may cause issues during reinstallation. I had to remove them as well.

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

Although it is a bad practice to remove packages manually (it may cause problems with the package manager), it helped my case. Thisanswer shows how to remove nodejs and related files manually.

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

To reinstall them:

要重新安装它们:

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

回答by thadk

I got into this situation with an old version of the nnode version switcher (~v2), having switched onto Node v12. I was able to fix it by switching to a much older version of Node it had installed using n, v8 worked for me, and then upgrade the version switcher with sudo npm install -g n.

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

From there I could switch to any version of node and use npm again.

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

回答by Muntasir

So I have recently faced a very similar problem when I was trying to run a react project in my Windows 10 x64 PC. Although I installed Node.js v12.13.0 (+ npm v6.12.0) x64; I couldn't execute npm installcommand, as it would give me this same error (Cannot read property 'resolve' of undefined).

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

Then according to this comment, I uninstalled the x64 version of Node.js and then installed the x86 version. After this, I could execute the npm installcommand successfully.

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

回答by joseph navin shaji

The problem may occur if you are using node version manager(nvm) try uninstalling it , i had the same issue and i uninstalled nvm and tried it .

如果您使用节点版本管理器(nvm)尝试卸载它,则可能会出现问题,我遇到了同样的问题,我卸载了 nvm 并尝试了它。

Worked with all versions of node . (worked fine with node 10.15.3,10.16 and 12.7)

适用于所有版本的 node 。(适用于节点 10.15.3、10.16 和 12.7)

回答by n0n

The same issue occurred for me on FreeBSD after I tried to update a really outdated node installation.

在我尝试更新一个真正过时的节点安装后,我在 FreeBSD 上发生了同样的问题。

After removing node and npm packages (pkg delete node8-8.16.2) I had to manually remove /usr/local/lib/node_modulesdirectory (some npm dependencies had been left behind and that caused the problem).

删除 node 和 npm 包 ( pkg delete node8-8.16.2) 后,我必须手动删除/usr/local/lib/node_modules目录(一些 npm 依赖项已被留下,这导致了问题)。

回答by Munzer

Anyone having this issue and have scrolled this far, if you are installing node manually and you have nvminstalled then this might be the issue, don't do it manually, and don't uninstall nvm to fix the issue, just let nvm install the version for you rather than doing it manually, if you don't have nvm installed and you are lost trying upgrading/downgrading node/npm versions, then install nvmand let it handle all these things for you.

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

Use nvm install x.x.x, then nvm use x.x.x, and everything worked just fine.

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

more about nvm

更多关于 nvm