16

When I am running the command npm install npm@latest -g I am getting below error :-

npm WARN tar zlib error: unexpected end of file
npm ERR! cb() never called!

npm ERR! This is an error with npm itself. Please report this error at:
npm ERR!     <https://github.com/npm/npm/issues>

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\dk\AppData\Roaming\npm-cache\_logs\2018-04-10T03_25_52_880Z-debug.log

i googled it and tried so many things,nothing worked.

F11
  • 3,703
  • 12
  • 49
  • 83

4 Answers4

40

To anyone stumbling upon this question, if you are facing the same error message on npm install, then npm install --no-package-lock solved it for me.

As suggested in the referenced Github issue in Mohit Mutha's comment above, this is especially true if the command is ran in a CI/CD pipeline, or in my case, in Docker.

EDIT: Reason being is that the package-lock.json file already exists in your Docker image or CI pipeline

Full details

Anas Tiour
  • 1,344
  • 3
  • 17
  • 33
  • 1
    Confirming positive use of the flag on gitlab's pipeline for an ng v6 project. – 4F2E4A2E Feb 19 '20 at 17:11
  • 1
    in my case ( jenkins CI ), workspace was corrupted. cleaning the workspace helped. – chandu Nov 02 '20 at 21:01
  • 2
    People should know what they are disabling when calling `--no-package-lock`, don't you think? – Dimitri Kopriwa Dec 30 '20 at 22:42
  • "The --no-package-lock argument will prevent npm from creating a package-lock.json file. When running with package-lock's disabled npm will not automatically prune your node modules when installing." [Source](https://docs.npmjs.com/cli/v6/commands/npm-install) – Anas Tiour Jan 01 '21 at 14:54
  • 1
    One of the reasons for this issue is that you have package-lock.json file in your project directory. So, you may have to delete the package-lock.json file from the directory and then run the `npm install` command. Hope it will work for you. – Yisal Khan Apr 05 '21 at 05:33
  • 1
    DUDE, i could put million vote ups for this answer. You saved me a lot of time. Thanks!!! – bksi May 18 '21 at 08:35
  • 1
    Version 7.x.x of npm doesn't have the `--no-package-lock` option. – Diego Alberto Zapata Häntsch May 26 '21 at 17:59
  • Was happening to me inside docker, weird thing was that on my machine with a newer version of docker everything was building smoothly, but on the server with a slightly outdated docker the build was failing with this error. package-lock.json didn't get cached, it was pulled from the github repo. Running `npm cache clean --force` before `npm install` fixed the issue just fine :) – steak_Overcooked Nov 23 '21 at 06:21
  • 1
    We have a Gitlab CI and confirm this works. – angelokh Dec 02 '21 at 08:44
12

Our team encountered this error in our CI pipeline. However, the top answer of using --no-package-lock actually causes npm to also not use a present package-lock.json, which is definitely not the desired behavior for CI. Instead, using npm ci is now the recommended way to install in CI since it will use the existing package-lock (and nothing else).

Justin Dehorty
  • 1,383
  • 1
  • 15
  • 26
6

Solved by running

sudo npm cache clean --force

and after that deleting package-lock.json and node_modules

and then performing

npm install 
ashad
  • 351
  • 4
  • 13
0

FYI I have gone through DOZENS of these responses for the exact same ask posted here again and again in SO. I think most of responses are valid. But in my case the actual issue was the CORPORATE PROXY not npm itself.

In my Dockerifle I had to explicitly add http-proxy and https-proxy to my npm config prior to running the npm install and it worked. FYI you need to tell npm config about your proxy or it wont download dependencies and this is what was throwing the cb() never called error - this works in both local (terminal) and docker containers.

...
RUN npm config set http-proxy http://mycompanyproxy.mycompany.com:8099
RUN npm config set https-proxy http://mycompanyproxy.mycompany.com:8099
RUN npm install
EXPOSE 3000
CMD ["node", "server"]
Lester Gray
  • 157
  • 11