I am sorry for the newbie question, but I have been figuring out this error, I have an Express app and I am trying to run it as a docker container. I've used this Dockerfile:
FROM node:14.17.0-alpine
COPY /src /nodejs
WORKDIR /nodejs
ADD package*.json ./
RUN npm install
CMD [ "node", "app.js" ]
and this is my package.js file :
{
"name": "server",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"start": "node app.js",
"dev": "nodemon app.js -e js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.17.1",
"express-rate-limit": "^5.1.3",
"nodemon": "^2.0.3",
"prom-client": "^12.0.0",
"puppeteer": "^10.4.0"
}
}
I'm using windows 10 and the command I used to deploy the container is :
docker run --name express-api -d -p 4000:4000 docker-express-app
the error that I get after executing the above command, in power shell:
internal/modules/cjs/loader.js:888
throw err;
^
Error: Cannot find module '/nodejs/app.js'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:885:15)
at Function.Module._load (internal/modules/cjs/loader.js:730:27)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
at internal/main/run_main_module.js:17:47 {
code: 'MODULE_NOT_FOUND',
requireStack: []
}
any idea on this issue is appreciated.
P.S: my project hierarchy is like so :
https://i.stack.imgur.com/ptlkI.png
CodePudding user response:
You didn't COPY the app.js file in your container.
WORKDIR /nodejs
COPY src .
COPY app.js .
ADD package*.json ./
RUN npm install
CMD [ "node", "app.js" ]
This should fix it.
I think a more easier way could be to make a dockerignore file and copy everything. Another problem is that you're using ADD instead of COPY. You just need to copy your files in the container and I think COPY would suffice as ADD has some extra functionality which can be confusing.