C:\Users\HP\Desktop\nodeDocker>docker logs nodedocker_node-app_1
> nodeDocker@1.0.0 dev
> nodemon -L index.js
[nodemon] 2.0.12
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node index.js`
node:internal/modules/cjs/loader:927
throw err;
^
Error: Cannot find module 'mongoose'
Require stack:
- /app/index.js
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:924:15)
at Function.Module._load (node:internal/modules/cjs/loader:769:27)
at Module.require (node:internal/modules/cjs/loader:996:19)
at require (node:internal/modules/cjs/helpers:92:18)
at Object.<anonymous> (/app/index.js:2:18)
at Module._compile (node:internal/modules/cjs/loader:1092:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1121:10)
at Module.load (node:internal/modules/cjs/loader:972:32)
at Function.Module._load (node:internal/modules/cjs/loader:813:14)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12) {
code: 'MODULE_NOT_FOUND',
requireStack: [ '/app/index.js' ]
}
[nodemon] app crashed - waiting for file changes before starting...
I have two containers
C:\Users\HP\Desktop\nodeDocker>docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
NAMES
d34a42bd5be5 nodedocker_node-app "docker-entrypoint.s…" 15 minutes ago Up 15 minutes 0.0.0.0:3000->3000/tcp, :::3000->3000/tcp nodedocker_node-app_1
b026aa82c85b mongo "docker-entrypoint.s…" 15 minutes ago Up 15 minutes 27017/tcp
nodedocker_mongo_1
But could’n find the error where did wrong!!
package.json
{
"name": "nodeDocker",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node index.js",
"dev": "nodemon -L index.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.17.1",
"mongoose": "^6.0.7"
},
"devDependencies": {
"nodemon": "^2.0.13"
}
}
index.js
const express = require("express");
const mongoose = require("mongoose");
const app = express();
mongoose
.connect("...........")
.then(()=>console.log("Successfully connected to DB"))
.catch((e)=>console.log(e));
const port = process.env.PORT || 3000;
app.get("/", (req, res)=>{
res.send("<h2>Hi There!</h2>");
});
app.listen(port, ()=>{
console.log(`Listening on port ${port}`)
});
Dockerfile
FROM node:15
WORKDIR /app
COPY package.json .
RUN npm install
ARG NODE_ENV
RUN if [ "$NODE_ENV" = "development" ]; \
then npm install; \
else npm install --only-production; \
fi
COPY . ./
ENV PORT 3000
EXPOSE $PORT
CMD ["node", "index.js"]
docker-compose.dev.yml
version: "3"
services:
node-app:
build:
context: .
args:
NODE_ENV: development
volumes:
- ./:/app
- /app/node_modules
environment:
- NODE_ENV=development
command: npm run dev
docker-compose.yml
version: "3"
services:
node-app:
build: .
ports:
- "3000:3000"
environment:
- PORT=3000
mongo:
image: mongo
environment:
MONGO_INITDB_ROOT_USERNAME: <I didnt put here>
MONGO_INITDB_ROOT_PASSWORD: <i didn put here>
volumes:
- mongo-db:/data/db
volumes:
mongo-db: