Search code examples
dockerdocker-build

How to fix error "npm ERR! could not detect node name from path or package" when building simple app with docker and nodeJS?


Dockerfile

FROM node:alpine
COPY ./ ./
RUN npm install
CMD ["npm", "start"]

index.js

const express = require('express');
const app = express();
app.get('/', (req, res) => {
  res.send('Hi there');
});
app.listen(8080, () => {
  console.log("Listening on post 8080");
});

package.json

{
  "dependencies": {
    "express": "*"
  },
  "scripts": {
    "start": "node index.js"
  }
}

docker build .

npm ERR! could not detect node name from path or package

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2020-11-23T23_36_35_301Z-debug.log
The command '/bin/sh -c npm install' returned a non-zero code: 1

I cannot understand why this does not work.


Solution

  • Set a WORKDIR then copy your files to that directory:

    FROM node:alpine
    
    WORKDIR /app
    COPY ./ /app
    
    RUN npm install
    CMD ["npm", "start"]
    

    As explained in the docs:

    The WORKDIR instruction sets the working directory for any RUN, CMD, ENTRYPOINT, COPY and ADD instructions that follow it in the Dockerfile. If the WORKDIR doesn’t exist, it will be created even if it’s not used in any subsequent Dockerfile instruction.

    On npm's Github repo, a similar error was mentioned in this comment as part of the issue on npm not working when run outside a package directory. It seems related only to new versions of node and npm (node v15.x, npm v7.0.x as of the bug report):

    Fresh install of Node 15.1.0 and npm i -g npm@7.0.9, that for some (probably unrelated) reason don't work properly.
    ...

    24 verbose stack TypeError: could not detect node name from path or package
    ...
    28 verbose node v15.1.0
    29 verbose npm  v7.0.8
    30 error could not detect node name from path or package
    

    But in your case, you do have a package.json, so it's probably because the build command can't find it without setting a proper WORKDIR. So, rather than to work it around with npm, just make sure to always set a WORKDIR. It's recommended anyway as mentioned in the WORKDIR section of Docker's best practices:

    For clarity and reliability, you should always use absolute paths for your WORKDIR. Also, you should use WORKDIR instead of proliferating instructions like RUN cd … && do-something, which are hard to read, troubleshoot, and maintain.