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.