Node error Cannot read property 'resolve' of undefined

36,284

Solution 1

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

To remove nodejs and associated packages (like 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.

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

To reinstall them:

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

Solution 2

I got into this situation with an old version of the n node 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.

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

Solution 3

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 install command, as it would give me this same error (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 install command successfully.

Solution 4

Anyone having this issue and have scrolled this far, if you are installing node manually and you have nvm installed 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 nvm and let it handle all these things for you.

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

more about nvm

Solution 5

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 .

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

Share:
36,284

Related videos on Youtube

zinngg
Author by

zinngg

Updated on February 25, 2022

Comments

  • zinngg
    zinngg about 2 years

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

    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 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?

    • Bambou
      Bambou about 5 years
      I already have the same issue, my problem was I've installed some packages in global on my Mac and then I linked them with npm link <name of package> so I deleted node_modules folder and made a npm i and it did work, try and let us know if it works :/
    • OriEng
      OriEng about 5 years
      Try to update npm : npm install npm -g
    • jmunsch
      jmunsch about 5 years
      In my case I had some new dependencies that I needed to pull in and forgot to run npm install to pull them in. Apologies, I know it might not answer your question, but it was the first answer I found when searching for this error.
    • sɐunıɔןɐqɐp
      sɐunıɔןɐqɐp over 3 years
  • zedd45
    zedd45 about 5 years
    for Homebrew users you can type brew remove node, and it will remove the brew'd version.
  • thadk
    thadk almost 5 years
    If anyone else is in this situation, before you fix it, you might post the errors you get from other later versions of node using n – they were slightly different from this (just as persistent) and it could help folks troubleshoot.