'`npm` busted - how to get past module-not-found, unexpected-token errors

I've installed Node and NPM using nvm-windows.

When I tried to run npm install, I got an error Unexpected token '.', then a path to a complete log of the run.

I checked the complete log, and unexpected token came from a file that contained only the text ../workspace/libnpmfund

That looks a Unix symlink on Windows.

I tried deleting the file and re-installing libnpmfund

npm i -g libnpmfund

But the output I get from that is Cannot find module 'libnpmfund'.

I tried changing what version of node and npm I was using, to no avail.

How can I fix this? How did I get into this mess?

(FYI there were a bunch of such symlinks and I deleted them all intending to re-install them correctly one by one.)



Solution 1:[1]

Under macOS (and apparently this will work under Windows as well), what I did was basically to remove everything under node_modules, delete package.json and package-lock.json, and start from scratch.

It seems that when npm is seriously confused to the point that it stops finding its own modules (you cannot install anything since npm is broken...), the 'best' alternative seems to be to do everything from the beginning.

This doesn't seem to be a real solution, but eventually just a workaround — a quick fix that may be helpful to others having encountered the same issue who attempted to figure out a more obvious solution...

Solution 2:[2]

I am using nvm for windows and had the following installed.

Node 15.0.1 and Npm 7.0.3

For some reason, I updated npm to 8.8.0, which then complained that it requires Node 16 or higher.

I used nvm to install the latest version of Node, which was Node 18.1.0, which includes Npm 8.8.0 and I switched over to use this version via nvm.

This is where I ran into the same error, I couldn't update npm or install anything with it.

I managed to resolve the issue by copying all the npm related files from my v15 folder (...\AppData\Roaming\nvm\v15.0.1) over to the v18 folder (...\AppData\Roaming\nvm\v18.1.0).

Files Copied: npm, npm.cmd, npx, npx.cmd and \node_modules\npm (entire npm folder)

After that, I was good to go again.

I realise that not everyone is going to have a working copy of npm available but I guess the point is that replacing the version of npm that was installed along with node via nvm worked for me.

Obviously npm is broken so try installing it via a script or downloading a zip/tgz.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Gwyneth Llewelyn
Solution 2 Vazmo