quite new to docker so I do not know where my configs are lacking, so I have a NodeJS TypeScript project that I am trying to dockerize.
The problem faced
- When I run
docker build -t server .
it runs successfully building everything - Now when I run
docker run -p 4000:5000 server
I get the following error below
node:internal/modules/cjs/loader:928
throw err;
^
Error: Cannot find module '/dist/server.js'
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:925:15)
at Function.Module._load (node:internal/modules/cjs/loader:769:27)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12)
at node:internal/main/run_main_module:17:47 {
code: 'MODULE_NOT_FOUND',
requireStack: []
}
So here is my Dockerfile:
FROM node
WORKDIR /usr/app
COPY package.json ./
RUN npm i
COPY . .
RUN npm run build
COPY .env ./dist/
WORKDIR /dist
EXPOSE 5000
CMD node server.js
If this will also be helpful to decipher the problem, here is also my tsconfig.json
{
"compilerOptions": {
"module": "commonjs",
"outDir": "dist",
"baseUrl": ".",
"moduleResolution": "Node",
"esModuleInterop": true,
"allowJs": true,
"skipLibCheck": true,
"allowSyntheticDefaultImports": true,
"alwaysStrict": true,
"forceConsistentCasingInFileNames": false,
"target": "ES6",
"strict": true,
"noImplicitAny": true
},
"exclude": [
"node_modules"
],
"include": [
"src/**/*"
]
}
Finally here is my package.json scripts and main:
"main": "dist/server.js",
"scripts": {
"build": "tsc -b",
"start": "node dist/server.js",
"dev": "nodemon --exec ts-node src/server.ts"
},