How to solve npm install error “npm ERR! code 1”

Viewed 124111

I'm trying to install Gulp.js and when I write npm install I get this issue:

npm ERR! code 1
npm ERR! path D:\www\wegrow\node_modules\node-sass
npm ERR! command failed
npm ERR! command C:\Windows\system32\cmd.exe /d /s /c node scripts/build.js
npm ERR! Building: C:\Program Files (x86)\nodejs\node.exe D:\www\wegrow\node_modules\node-gyp\bin\node-gyp.js rebuild --verbose --libsass_ext= --libsass_cflags= --libsass_ldflags= --libsass_library=
npm ERR! gyp info it worked if it ends with ok
npm ERR! gyp verb cli [
npm ERR! gyp verb cli   'C:\\Program Files (x86)\\nodejs\\node.exe',
npm ERR! gyp verb cli   'D:\\www\\wegrow\\node_modules\\node-gyp\\bin\\node-gyp.js',
npm ERR! gyp verb cli   'rebuild',
npm ERR! gyp verb cli   '--verbose',
npm ERR! gyp verb cli   '--libsass_ext=',
npm ERR! gyp verb cli   '--libsass_cflags=',
npm ERR! gyp verb cli   '--libsass_ldflags=',
npm ERR! gyp verb cli   '--libsass_library='
npm ERR! gyp verb cli ]
npm ERR! gyp info using node-gyp@3.8.0
npm ERR! gyp info using node@16.1.0 | win32 | ia32
npm ERR! gyp verb command rebuild []
npm ERR! gyp verb command clean []
npm ERR! gyp verb clean removing "build" directory
npm ERR! gyp verb command configure []
npm ERR! gyp verb check python checking for Python executable "python2" in the PATH
npm ERR! gyp verb `which` failed Error: not found: python2
npm ERR! gyp verb `which` failed     at getNotFoundError (D:\www\wegrow\node_modules\which\which.js:13:12)
npm ERR! gyp verb `which` failed     at F (D:\www\wegrow\node_modules\which\which.js:68:19)
npm ERR! gyp verb `which` failed     at E (D:\www\wegrow\node_modules\which\which.js:80:29)
npm ERR! gyp verb `which` failed     at D:\www\wegrow\node_modules\which\which.js:89:16
npm ERR! gyp verb `which` failed     at D:\www\wegrow\node_modules\isexe\index.js:42:5
npm ERR! gyp verb `which` failed     at D:\www\wegrow\node_modules\isexe\windows.js:36:5
npm ERR! gyp verb `which` failed     at FSReqCallback.oncomplete (node:fs:194:21)
npm ERR! gyp verb `which` failed  python2 Error: not found: python2
npm ERR! gyp verb `which` failed     at getNotFoundError (D:\www\wegrow\node_modules\which\which.js:13:12)
npm ERR! gyp verb `which` failed     at F (D:\www\wegrow\node_modules\which\which.js:68:19)
npm ERR! gyp verb `which` failed     at E (D:\www\wegrow\node_modules\which\which.js:80:29)
npm ERR! gyp verb `which` failed     at D:\www\wegrow\node_modules\which\which.js:89:16
npm ERR! gyp verb `which` failed     at D:\www\wegrow\node_modules\isexe\index.js:42:5
npm ERR! gyp verb `which` failed     at D:\www\wegrow\node_modules\isexe\windows.js:36:5
npm ERR! gyp verb `which` failed     at FSReqCallback.oncomplete (node:fs:194:21) {
npm ERR! gyp verb `which` failed   code: 'ENOENT'
npm ERR! gyp verb `which` failed }
npm ERR! gyp verb check python checking for Python executable "python" in the PATH
npm ERR! gyp verb `which` succeeded python C:\Python39\python.EXE
npm ERR! gyp ERR! configure error
npm ERR! gyp ERR! stack Error: Command failed: C:\Python39\python.EXE -c import sys; print "%s.%s.%s" % sys.version_info[:3];
npm ERR! gyp ERR! stack   File "<string>", line 1
npm ERR! gyp ERR! stack     import sys; print "%s.%s.%s" % sys.version_info[:3];
npm ERR! gyp ERR! stack                       ^
npm ERR! gyp ERR! stack SyntaxError: invalid syntax
npm ERR! gyp ERR! stack
npm ERR! gyp ERR! stack     at ChildProcess.exithandler (node:child_process:326:12)
npm ERR! gyp ERR! stack     at ChildProcess.emit (node:events:365:28)
npm ERR! gyp ERR! stack     at maybeClose (node:internal/child_process:1067:16)
npm ERR! gyp ERR! stack     at Socket.<anonymous> (node:internal/child_process:453:11)
npm ERR! gyp ERR! stack     at Socket.emit (node:events:365:28)
npm ERR! gyp ERR! stack     at Pipe.<anonymous> (node:net:661:12)
npm ERR! gyp ERR! System Windows_NT 10.0.19042
npm ERR! gyp ERR! command "C:\\Program Files (x86)\\nodejs\\node.exe" "D:\\www\\wegrow\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild" "--verbose" "--libsass_ext=" "--libsass_cflags=" "--libsass_ldflags=" "--libsass_library="
npm ERR! gyp ERR! cwd D:\www\wegrow\node_modules\node-sass
npm ERR! gyp ERR! node -v v16.1.0
npm ERR! gyp ERR! node-gyp -v v3.8.0
npm ERR! gyp ERR! not ok
npm ERR! Build failed with error code: 1

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\wbalu\AppData\Local\npm-cache\_logs\2021-05-05T10_38_29_779Z-debug.log

I have formatted my computer. Everything was working before. And now when I try to install it, that's what pops up. I have tried reinstalling Node.js, but it didn't help. What should I do?

11 Answers

If your Node.js version is very recent, try downgrading. The stable version 14.16.1 worked.

Downgrading Node.js is one option, but I don't like the idea of downgrading Node.js just because one package throws an error. In my opinion, it would be a better approach to find out which package is causing the error and then fix it.

The quick solution

In the line after npm ERR! code 1, you should be able to see npm ERR! path.... In my case the path indicates that there is a problem with the node-sass npm package:

npm ERR! code 1
npm ERR! path /home/gignu/GitHub/Movie-Subtitles/node_modules/node-sass
npm ERR! command failed

Now remove this npm package from package.json and see if the installation works. If it works you've found the culprit! In my case, all I had to do to fix it was to upgrade node-sass to the latest version.

// package.json with the outdated dependency

"node-sass": "^4.14.1",
// package.json with the updated dependency

"node-sass": "^7.0.1",

The slow solution

If the previous solution doesn't work for you I would go with @Andrew Fair's solution:

I took out the entire dependencies portion in my package.json file and just started working my way through installing one at a time.

To do this, simply copy the whole dependencies section and devDependencies section from package.json and paste it into a text editor as a backup. Then delete all your dependencies and try adding back one dependency at a time. You can also add back several dependencies at a time if you have a lot of them. Each time, run npm i and see if it throws an error. Once you've found out which dependency is causing the error, try to upgrade that dependency to a newer version and then run npm i --force!

If that doesn't work, try replacing the bad npm package with another one or look on their GitHub repository for solutions.

Try

npm cache clean --force

Delete folder node_modules and file package-lock.json.

npm i

I ran into this issue as well. After looking over my setup, I saw nothing wrong and deleted the cache and deleted package-lock and still no success.

This was an older website and I tried using update, etc. to no avail.

So I took out the entire dependencies portion in my package.json file and just started working my way through installing one at a time.

It works!

Apparently almost all of the versions were a major update behind which was too much for it to update.

When I try to run the npm install command in an older project, I faced the same issues and I resolved it by updating the dependencies in package.json to the latest versions by following these steps:

  1. delete folder node_modules folder and file package-lock.json

  2. install npm-check-updates globally, to update all packages to a new major version

    run npm install -g npm-check-updates

  3. run ncu -u to update dependencies in package.json to latest version

    Note: you can use npm update, but npm update doesn't update to latest version

  4. run npm install

Maybe try yarn install. It works for me.

I had faced the same issue after upgrading Angular. So I tried multiple steps to resolve it. Some of the steps are mentioned below.

  1. try with deleting package-lock.json file and node_module folder then run

    npm cache clean --force
    

    And try npm i again to install packages. If it is still not working, check step 2.

  2. Check the Node.js version and npm version to see if both versions are compatible. In my case, I was using Node.js v14.15.0 and npm v8.1.6, so I uninstalled the npm with the below command and installed Node.js again to make the Node.js and npm versions in sync.

    npm uninstall npm -g
    

And this works for me.

Try cnpm. It works for me.

npm install -g cnpm --registry=https://registry.npmmirror.com
cnpm install

Try using:

npm install -g firebase-tools@7.7.0

instead of:

npm install -g firebase-tools

Try

npm install --python=python2.7.

It should work.

Alternatively, --python=path to your Python executable

npm install -g npm@latest

seemed to solved it for me

Related