title: "Error: Cannot find module 'mysql2/promise'" when running "docker-compose up"
body: <p>I want to use docker compose to connect a node program, in one container, to a MySQL database, in another container. The database seems to start fine, but my index.js file throws an error: <code>Cannot find module 'mysql2/promise'</code></p>
  <p>I've tried installing different packages with npm, and even edited various lines in my package.json file in desperation, but to no avail; always the same error. Here are the relevant files, and the output of the commands that work so far.</p>
  <p><strong>$ npm install mysql2</strong></p>
  <pre><code>npm WARN simplyanything@1.0.0 No repository field.
  npm WARN simplyanything@1.0.0 No license field.
  
  + mysql2@2.3.3
  updated 1 package and audited 101 packages in 3.302s
  found 0 vulnerabilities
  </code></pre>
  <p><strong>$ sudo npm install mysql2-promise</strong></p>
  <pre><code>+ mysql2-promise@0.1.4
  updated 1 package and audited 101 packages in 3.772s
  found 0 vulnerabilities
  </code></pre>
  <p><strong>package.json</strong></p>
  <pre><code>{
      &quot;name&quot;: &quot;simplyanything&quot;,
      &quot;version&quot;: &quot;1.0.0&quot;,
      &quot;scripts&quot;: {
          &quot;start&quot;: &quot;node index.js&quot;
      },
      &quot;description&quot;: &quot;Actions party game&quot;,
      &quot;dependencies&quot;: {
          &quot;express&quot;: &quot;^4.17.1&quot;,
          &quot;mysql2&quot;: &quot;^2.3.3&quot;,
          &quot;mysql2-promise&quot;: &quot;^0.1.4&quot;,
          &quot;socket.io&quot;: &quot;^4.4.1&quot;
      },
      &quot;author&quot;: &quot;Chris DeHaan&quot;
  }
  </code></pre>
  <p><strong>index.js</strong></p>
  <pre><code>const express = require('express');
  let app = express();
  let http = require('http').createServer(app);
  const io = require('socket.io')(http, {pingTimeout: 60000});
  
  app.use(express.static('public'));
  app.get('/', (req, res) =&gt; { res.sendFile(__dirname + '/index.html'); });
  http.listen(3000, () =&gt; { console.log('listening on *:3000'); });
  
  const mysql = require('mysql2/promise');
  const pool = mysql.createPool({
      connectionLimit : 100,
      host: process.env.MYSQL_HOST,
      user: process.env.MYSQL_USER,
  [.... and so on]
  </code></pre>
  <p><strong>Dockerfile</strong></p>
  <pre><code>FROM node:latest
  WORKDIR /sa/
  COPY package.json .
  RUN npm install
  COPY . .
  </code></pre>
  <p><strong>docker-compose.yml</strong></p>
  <pre><code>version: '3.8'
  services: 
      web:
        build:
            context: .
        env_file: ./.env
        command: npm start
        volumes: 
            - .:/sa/
            - /sa/node_modules
        ports:
            - $NODE_LOCAL_PORT:$NODE_DOCKER_PORT
        depends_on: 
            - mysqldb
        environment: 
            MYSQL_HOST: mysqldb
      mysqldb:
        image: mysql
        env_file: ./.env
        environment: 
            MYSQL_ROOT_PASSWORD: $MYSQL_ROOT_PASSWORD
            MYSQL_DATABASE: $MYSQL_DATABASE
        ports:
            - $MYSQL_LOCAL_PORT:$MYSQL_DOCKER_PORT
        volumes:
            - mysql:/var/lib/mysql
            - mysql_config:/etc/mysql
  volumes:
      mysql:
      mysql_config:
  </code></pre>
  <p><strong>.env</strong></p>
  <pre><code>MYSQL_USER=simplyanythingUser
  MYSQL_ROOT_PASSWORD=[Well, you don't need to know this]
  MYSQL_DATABASE=simplyanything
  MYSQL_LOCAL_PORT=3306
  MYSQL_DOCKER_PORT=3306
  
  NODE_LOCAL_PORT=3000
  NODE_DOCKER_PORT=3000
  </code></pre>
  <p><strong>$ sudo docker build -t sa .</strong></p>
  <pre><code>Sending build context to Docker daemon  9.526MB
  Step 1/5 : FROM node:latest
   ---&gt; e6bed6a65a54
  Step 2/5 : WORKDIR /sa/
   ---&gt; Using cache
   ---&gt; 3da61e5a5928
  Step 3/5 : COPY package.json .
   ---&gt; Using cache
   ---&gt; 1e7bbeaaa894
  Step 4/5 : RUN npm install
   ---&gt; Using cache
   ---&gt; 52f36e54d698
  Step 5/5 : COPY . .
   ---&gt; e0a50567567b
  Successfully built e0a50567567b
  Successfully tagged sa:latest
  </code></pre>
  <p><strong>$ node index.js</strong></p>
  <pre><code>Debugger listening on ws://127.0.0.1:44273/a1b49bfc-83e7-47dd-ba53-64a8df19ccc9
  For help, see: https://nodejs.org/en/docs/inspector
  Debugger attached.
  listening on *:3000
  </code></pre>
  <p>(and it works in the browser at this point)</p>
  <p><strong>$ sudo docker-compose up</strong></p>
  <pre><code>Starting sa_mysqldb_1 ... done
  Starting sa_web_1     ... done
  Attaching to sa_mysqldb_1, sa_web_1
  mysqldb_1  | 2022-02-08 08:31:21+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.28-1debian10 started.
  mysqldb_1  | 2022-02-08 08:31:21+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
  mysqldb_1  | 2022-02-08 08:31:21+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.28-1debian10 started.
  mysqldb_1  | 2022-02-08T08:31:21.660916Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.28) starting as process 1
  mysqldb_1  | 2022-02-08T08:31:21.747461Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
  web_1      | 
  web_1      | &gt; simplyanything@1.0.0 start
  web_1      | &gt; node index.js
  web_1      | 
  mysqldb_1  | 2022-02-08T08:31:23.587278Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
  web_1      | node:internal/modules/cjs/loader:936
  web_1      |   throw err;
  web_1      |   ^
  web_1      | 
  web_1      | Error: Cannot find module 'mysql2/promise'
  web_1      | Require stack:
  web_1      | - /sa/index.js
  web_1      |     at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
  web_1      |     at Function.Module._load (node:internal/modules/cjs/loader:778:27)
  web_1      |     at Module.require (node:internal/modules/cjs/loader:999:19)
  web_1      |     at require (node:internal/modules/cjs/helpers:102:18)
  web_1      |     at Object.&lt;anonymous&gt; (/sa/index.js:13:15)
  web_1      |     at Module._compile (node:internal/modules/cjs/loader:1097:14)
  web_1      |     at Object.Module._extensions..js (node:internal/modules/cjs/loader:1149:10)
  web_1      |     at Module.load (node:internal/modules/cjs/loader:975:32)
  web_1      |     at Function.Module._load (node:internal/modules/cjs/loader:822:12)
  web_1      |     at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12) {
  web_1      |   code: 'MODULE_NOT_FOUND',
  web_1      |   requireStack: [ '/sa/index.js' ]
  web_1      | }
  web_1      | 
  web_1      | Node.js v17.4.0
  mysqldb_1  | 2022-02-08T08:31:25.086275Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
  mysqldb_1  | 2022-02-08T08:31:25.086445Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
  mysqldb_1  | 2022-02-08T08:31:25.277625Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
  mysqldb_1  | 2022-02-08T08:31:25.354311Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /var/run/mysqld/mysqlx.sock
  mysqldb_1  | 2022-02-08T08:31:25.355558Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.28'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  MySQL Community Server - GPL.
  </code></pre>
  <p>I'm really stuck because everything it needs seems to be installed, and it works when I run it on the command line with <code>node index.js</code> so obviously there's something I'm missing in setting up my container, or getting compose to connect them. Any advice would be much appreciated. Cheers.</p>