As I far as I know,
theWORKDIR don't have to be created by yourself.
Here's the documentation for WORKDIR.
Afterwards you don't have to copy by hand to the specific folder,
because after WORKDIRcommand the copy command would copy the files for you.Therefore I suggest you to use the following Dockerfile:
FROM node:alpine
WORKDIR /usr/yourapplication-name
COPY package.json .
RUN npm install\
&& npm install typescript -g
COPY . .
RUN tsc
CMD ["node", "./dist/server.js"]
As a tiny tipp: I would use typescript as a dependency in my and then just use the following file:package.json
FROM node:alpine
WORKDIR /usr/yourapplication-name
COPY package.json .
RUN npm install
COPY . .
RUN tsc
CMD ["node", "./dist/server.js"]
EDIT 2:
This issue has been resolved. If your package-lock.json file is corrupted, you may be able to fix it with the utility fix-has-install-script.
Original Answer:
Use (or add npm ci to your package-lock.json file, or delete .dockerignore in your local environment before building). The why is answered here.package-lock.json
EDIT 1:
Here's what I believe is going on. Disclaimer, I'm not an expert on or nodejs -- in fact I'm something of a novice. And all of this is conjecture based on some experiments.npm
is not linking the binaries for the dev dependencies via sym links in npm because the node_modules/.bin file has gotten into a corrupted state where the (prod) dependencies are in lockfileVersion 2 format, and the dev dependencies are still in lockfileVersion 1 format.package-lock.json
Note: Making a bunch of assumptions here.
Your local host using using npm 6, and the docker container is using npm 7.
Because of this, the existing is in package-lock.json which doesn't include a lockfileVersion: 1 section for dependencies that have binaries. Version 2 does save the bin section, which bin: must use to determine what binaries to install/link.npm
When you run the production dependency install (e.g. ), using NODE_ENV=production npm install version 7, npm is upgrading the version of your npm to package-lock.json, part of this includes saving lockfileVersion: 2 sections for the dependencies that install binaries. Importantly, it updates only the production dependencies. Now the bin: file is corrupted because it claims to be in version 2 format, but all the dev dependencies are either still in version 1 or at least don't have the package-lock.json section correctly applied.bin:
When you now try to install your dev dependencies, sees that the npm is in package-lock.json, so it assumes that the dev dependencies have been upgraded as well (but they haven't been, or at least not correctly). It doesn't find the lockfileVersion: 2 sections because they don't exist and so it doesn't link the binaries to the bin: directory.node_modules/.bin/
You can perform a minimum reproduction of it using this :Dockerfile
FROM node:14 as npm6
WORKDIR /app
# Create a node project using npm 6 and install a dev dependency
# that contains a binary.
RUN npm init --yes && \
npm install --save-dev typescript
FROM node:15 as npm7
COPY --from=npm6 /app/package*.json /app/
WORKDIR /app
# Install production dependencies, then all dependencies. This should
# link the binaries for typescript in (e.g. tsc) under node_modules/.bin.
RUN npm install -g npm@7.10.0 && \
npm install --production && \
npm install
# Causes error, tsc not found.
CMD ["npx", "-c", "tsc --version"]
I couldn't find an existing bug ticket so I created one here. Perhaps it will get fixed.