Ryan Cocuzzo
Ryan Cocuzzo

Reputation: 3219

Node.js Error: Cannot find module (local file)


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" ]

Upvotes: 0

Views: 2215

Answers (1)

abestrad
abestrad

Reputation: 916

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

Upvotes: 1

Related Questions