简体   繁体   中英

Error when install express-generator using npm

I tried to install express-generator using the command npm install express-generator but I get the following error. I searched in the web and could not find any helpful answers for this.

npm WARN deprecated mkdirp@0.5.1: Legacy versions of mkdirp are no longer supported. Please update to mkdirp 1.x. (Note that the API surface has changed to use Promises in 1.x.)
npm ERR! Unexpected end of JSON input while parsing near '...DHZShvBYWKIIktsiBUuRD'

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\ASUS\AppData\Roaming\npm-cache\_logs\2020-07-12T10_29_10_754Z-debug.log

The WARN has nothing to do with the rest of the message. It's only that, a WARN, and it should be given to package mantainers using outdated deps, not you.

The ERR(s) is saying npm could not parse a JSON content somewhere and not much else.

Before digging into the logfile it's showing you, try cleaning your cache to cover the not uncommon case in which a cache entry is corrupted or inconsistent and npm crashes just because it's trying to be more efficient.

Given you're using window, open a console as administrator (both CMD and Powershell should work, but I don't have a windows machine atm) and run

npm cache clean --force

Alternatively, running

npm doctor

Will, among other checks, verify the cached packages and pinpoint the corrupt or inconsistent ones.

It wouldn't hurt to also update your npm version to the latest one using

npm install -g npm

When you're done, try running express-generator again.

If you are struggling with installing express-generator global version with npm but if there is a problem with the node and npm version, you might give try to downgrade your node version. In my case, the solution below worked:

Firstly install nvm (node version manager) by following the README.md instructions from here .

If it's installed properly, you can give it a try by typing simply command -v nvm . If it returned nvm it's been installed successfully.

As I said, in my case, I tried an older version (14.20.0).

1- First, install older node version nvm install 14.20.0

2- Then switch to that version by typing nvm use 14.20.0 .

2.1.- If delete-prefix related error occur, simply type nvm use --delete-prefix v14.20.0 .

3- Install express-generator global version now by typing npm install -g express-generator . After it's installed successfully you can simply switch back to the newer version as the same method as we used above nvm use 16.17.0 . (In my time, latest stable method is 16.17.0 so I switched back to that version, you can switch whatever version you want to use)

I hope this approach works for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM