0

I am following Node.js's "Intro to Docker" tutorial and, when I run npm start, the project works. When I run docker run (options), the build is generated, but I'll find the error below in the logs. The project is bare-bones, simple, and straight-forward, I'm not sure what I'm missing here. I've gotten a very similar error in production earlier (to Heroku, without Docker), where local runs look good and live deploys get a similar error.

I'm not sure if I'm using something outdated, but I updated npm, docker, and am not sure what else could be.

Any help is appreciated!


Error:

internal/modules/cjs/loader.js:969

throw err;

^


Error: Cannot find module '/usr/src/app/server.js'

at Function.Module._resolveFilename (internal/modules/cjs/loader.js:966:15)

at Function.Module._load (internal/modules/cjs/loader.js:842:27)

at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)

at internal/main/run_main_module.js:17:47 {

code: 'MODULE_NOT_FOUND',

requireStack: []

}

Directory:

enter image description here

package.json:

{
  "name": "SampleProject",
  "version": "1.0.0",
  "description": "Node.js on Docker",
  "author": "First Last <[email protected]>",
  "main": "server.js",
  "scripts": {
    "start": "node server.js",
    "dock": "docker run -p 1234:1234 -d <My>/<Info>"
  },
  "dependencies": {
    "core-util-is": "^1.0.2",
    "express": "^4.17.1"
  }
}

Dockerfile

    # I'm using Node.js -> Import it's image
FROM node:12

# Create app directory
WORKDIR /usr/src/app

# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY package*.json ./


RUN npm install
# If you are building your code for production
# RUN npm ci --only=production

# Run on port 1234
EXPOSE 1234


CMD [ "node", "server.js" ]

server.js

'use strict';

const express = require('express');

// Constants
const PORT = 1234;
const HOST = '0.0.0.0';

// App
const app = express();
app.get('/', (req, res) => {
  res.send('Hello World');
});

app.listen(PORT, HOST);
console.log(`Running on http://${HOST}:${PORT}`);

Run:

npm run dock

Note:

I've also cleaned out the project by running the following:

rm -rf node_modules package-lock.json && npm install && npm start

RESOLVED:


Dockerfile

    # I'm using Node.js -> Import it's image
FROM node:12

# Create app directory
WORKDIR /usr/src/app

# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY package*.json ./


RUN npm install
# If you are building your code for production
# RUN npm ci --only=production

# * BEGIN SOLUTION *
# Bundle app source    
COPY . .
# *  END SOLUTION  *

# Run on port 1234
EXPOSE 1234


CMD [ "node", "server.js" ]
2
  • I think you are only copying package*.json files...you need to also copy the project files like app.js inside Docker. Commented Jul 1, 2020 at 20:51
  • @Rash I updated the dockerfile to contain COPY . . as per @abestrad 's response. I believe that should cover the same territory Commented Jul 1, 2020 at 21:02

1 Answer 1

1

I think you are missing the following important part, should be placed after you have RUN npm install:

To bundle your app's source code inside the docker image, use the COPY instruction:

# Bundle app source
COPY . .

And to force the execution of each step in the Dockerfile,

docker build --no-cache
Sign up to request clarification or add additional context in comments.

5 Comments

I tried this, didn't seem to change the outcome, I updated the Dockerfile
Try this time to build with --no-cache to force the execution of each step/instruction in the Dockerfile.
I'm not sure if this resolved the problem. After build & run, the process marks as "Running" (a good sign), but when I open it in a browser (from the Docker dashboard), there is no response. In an npm start run, there is a "Hello World" response.
That's your hello world, res.send('Hello World');
I'm going to add the --no-cache to the answer for the benefit of future readers,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.