npm install failing inside of Docker container

  docker, npm, npm-install

I’m trying to get npm install working inside of Docker container(s) on my system…

I’ve tried the following containers:

  • nikolaik/python-nodejs:python3.8-nodejs12
  • nikolaik/python-nodejs:python3.8-nodejs12-alpine
  • minideb:stretch (installed with nvm)

And they all have the same error. When I try to install an npm package, it will have an error that looks something like this:

# npm install -g typescript                                                                                                                                                                                      
npm ERR! Unexpected end of JSON input while parsing near '...ipt":"next","tsd":"la'                                                                                                                                

npm ERR! A complete log of this run can be found in:                                                                                                                                                               
npm ERR!     /root/.npm/_logs/2020-04-09T03_42_16_003Z-debug.log

If I run something like npm cache clean --force, and run the install command again, it’ll fail at some other point in parsing the JSON.

The funny thing is that sometimes if it’s a really small package it’ll install it without any issues:

# npm install padleft                                                                                                                                                                                   [12/1883]
npm WARN saveError ENOENT: no such file or directory, open '/package.json'
npm WARN notsup Unsupported engine for [email protected]: wanted: {"node":"6.3.1","npm":"3.10.6"} (current: {"node":"12.14.1","npm":"6.13.4"})
npm WARN notsup Not compatible with your version of node/npm: [email protected]
npm WARN enoent ENOENT: no such file or directory, open '/package.json'
npm WARN !invalid#1 No description
npm WARN !invalid#1 No repository field.
npm WARN !invalid#1 No README data
npm WARN !invalid#1 No license field.

+ [email protected]
added 1 package from 1 contributor and audited 2 packages in 1.472s
found 0 vulnerabilities

It almost seems like it’s failing if it can’t install something if it doesn’t download super fast. I’ve tried different versions of node/npm with nvm and it all does the same thing. On my host machine, npm works fine. Just having issues within all the Docker containers. I just had it fail with an nvm installed version on another Docker container based on Ubuntu.

Source: StackOverflow