3

When I start a dockerized Node.js testapp with

sudo docker-compose up

I get the following error:

Starting testapp_web_1 ... done
Attaching to testapp_web_1
web_1  |
web_1  | > [email protected] start /usr/app
web_1  | > node index.js
web_1  |
web_1  | internal/modules/cjs/loader.js:613
web_1  |     throw err;
web_1  |     ^
web_1  |
web_1  | Error: Cannot find module 'mongodb'
web_1  | Require stack:
web_1  | - /usr/app/index.js
web_1  |     at Function.Module._resolveFilename (internal/modules/cjs/loader.js:610:15)
web_1  |     at Function.Module._load (internal/modules/cjs/loader.js:526:27)
web_1  |     at Module.require (internal/modules/cjs/loader.js:666:19)
web_1  |     at require (internal/modules/cjs/helpers.js:16:16)
web_1  |     at Object.<anonymous> (/usr/app/index.js:4:21)
web_1  |     at Module._compile (internal/modules/cjs/loader.js:759:30)
web_1  |     at Object.Module._extensions..js (internal/modules/cjs/loader.js:770:10)
web_1  |     at Module.load (internal/modules/cjs/loader.js:628:32)
web_1  |     at Function.Module._load (internal/modules/cjs/loader.js:555:12)
web_1  |     at Function.Module.runMain (internal/modules/cjs/loader.js:826:10)
web_1  | npm ERR! code ELIFECYCLE
web_1  | npm ERR! errno 1
web_1  | npm ERR! [email protected] start: `node index.js`
web_1  | npm ERR! Exit status 1
web_1  | npm ERR!
web_1  | npm ERR! Failed at the [email protected] start script.
web_1  | npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
web_1  |
web_1  | npm ERR! A complete log of this run can be found in:
web_1  | npm ERR!     /root/.npm/_logs/2019-05-04T15_34_04_615Z-debug.log
testapp_web_1 exited with code 1

The project structur is as follows

- testapp
--- docker-compose.yml
--- dockerfile
--- src
----- index.js
----- package.json

index.js

'use strict';

const MongoClient = require('mongodb').MongoClient;
const express = require('express');

// Constants
const PORT = 8080;

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

app.listen(PORT);
console.log(`Running on Port:${PORT}`);

package.json

{
  "name": "testapp",
  "version": "0.0.1",
  "description": "Testapp",
  "author": "hi there",
  "main": "index.js",
  "scripts": {
    "start": "node index.js"
  },
  "dependencies": {
    "express": "^4.16.4",
    "mongodb": "^3.2.3"
  }
}

dockerfile

FROM node:12-alpine

WORKDIR /usr/app

COPY package*.json ./

RUN npm install

COPY . .

EXPOSE 8080

docker-compose.yml

version: '3'
services:
  web:
    build: .
    command: npm start
    volumes:
      - ./src:/usr/app/
      - /usr/app/node_modules
    ports:
      - 80:8080

I have alread read this stackoverflow thread but I couldn't solve this issue. I'm getting this error no matter what module I'm trying to use: mongodb, spdy, etc. Locally without docker the testapp is working. But I don't know what I'm doing wrong with docker. Can anyone help me?

1
  • Did you fairly recently add the dependency in your package.json file? The volumes: declaration prevents the node_modules tree from updating. Commented May 4, 2019 at 22:03

2 Answers 2

2

Have volume folder mapping for node_modules and ensure it has mongo folder copied / created

version: '3'
services:
  web:
    build: .
    command: npm start
    volumes:
      - ./src:/usr/app/
      - ./src/node_modules:/usr/app/node_modules
    ports:
      - 80:8080

Ref:https://morioh.com/p/42531a398049/containerizing-a-node-js-application-for-development-with-docker-compose

Sign up to request clarification or add additional context in comments.

1 Comment

Thank's, this solved my issue. One more question: Why do I need to create the folder ./src/node_modules on my docker host with mongodb folder in it? I have already defined the dependencies in the package.json. Shouldn't it be installed automatically due to the RUN npm install command in the dockerfile?
0

You need to drop & rebuild the anonymous volume holding the node_modules for the updates to package.json to take effect.

docker-compose rm -v
# then:
docker-compose build
docker-compose up

You will only have to do this for changes to node_modules, since the other volume is linked to your ./src/, and will reflect changes on the host.

Comments

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.