<p>You need to remove the volumes all together. Files are copied by your Dockerfile build:</p>

<pre><code>version: '3'
services:
  web:
    build: .
    command: npm start
    ports:
      - 80:8080
</code></pre>

CC BY-SA 4.0
2019-05-08T09:06:43.927Z
2019-05-08T09:06:43.927Z
Mihai
<pre><code>    volumes:
      - /usr/app/node_modules
</code></pre>

<p>This volume could be overwriting what you have installed during the <code>RUN npm install</code> stage of the Dockerfile. As far as I can see you're not doing any installation when starting the container, so the contents of the mount are empty. If you remove that mount, the <code>node_modules</code> path will be populated with what was installed during the <code>docker build</code> (as defined by the Dockerfile) phase.</p>

CC BY-SA 4.0
2019-05-05T23:27:03.817Z
2019-05-05T23:27:03.817Z
user2640621
<p>When I start a dockerized Node.js testapp with </p>

<pre><code>sudo docker-compose up
</code></pre>

<p>I get the following error:</p>

<pre><code>Starting testapp_web_1 ... done
Attaching to testapp_web_1
web_1  |
web_1  | &gt; testapp@0.0.1 start /usr/app
web_1  | &gt; 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.&lt;anonymous&gt; (/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! testapp@0.0.1 start: `node index.js`
web_1  | npm ERR! Exit status 1
web_1  | npm ERR!
web_1  | npm ERR! Failed at the testapp@0.0.1 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
</code></pre>

<p>The project structur is as follows</p>

<pre><code>- testapp
--- docker-compose.yml
--- dockerfile
--- src
----- index.js
----- package.json
</code></pre>

<p><strong>index.js</strong></p>

<pre><code>'use strict';

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

// Constants
const PORT = 8080;

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

app.listen(PORT);
console.log(`Running on Port:${PORT}`);
</code></pre>

<p><strong>package.json</strong>  </p>

<pre><code>{
  "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"
  }
}
</code></pre>

<p><strong>dockerfile</strong></p>

<pre><code>FROM node:12-alpine

WORKDIR /usr/app

COPY package*.json ./

RUN npm install

COPY . .

EXPOSE 8080
</code></pre>

<p><strong>docker-compose.yml</strong></p>

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

<p>I have alread read this <a href="https://stackoverflow.com/questions/30043872/docker-compose-node-modules-not-present-in-a-volume-after-npm-install-succeeds">stackoverflow thread</a> 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?</p>

<docker><node.js>
Docker Node.js Error: Cannot find module
Jens
# Docker Node.js Error: Cannot find module

(Asked by: username_0 on 2019-05-04)

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  | > testapp@0.0.1 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. (/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! testapp@0.0.1 start: `node index.js`
web_1  | npm ERR! Exit status 1
web_1  | npm ERR!
web_1  | npm ERR! Failed at the testapp@0.0.1 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?

**Tags:** docker, node.js


---

## 2 answers

### Answer by username_1 on 2019-05-08. Score: 0 | Accepted answer

You need to remove the volumes all together. Files are copied by your Dockerfile build:
version: '3'
services:
  web:
    build: .
    command: npm start
    ports:
      - 80:8080


---

### Answer by username_2 on 2019-05-05. Score: 0

volumes:
      - /usr/app/node_modules

This volume could be overwriting what you have installed during the RUN npm install stage of the Dockerfile. As far as I can see you're not doing any installation when starting the container, so the contents of the mount are empty. If you remove that mount, the node_modules path will be populated with what was installed during the docker build (as defined by the Dockerfile) phase.


---

