0

Trying to connect node.js and mysql in docker. Node.js code:

const express = require("express");
const app = express();
const port = process.env.PORT || 3000;
const mysql = require("mysql");

app.get("/", (req, res) => {
  res.send("<h2>Works</h2>")
})

app.listen(port, () => console.log(`listening on port ${port}`))

mysql container is running on port 3036 (I can go inside the container, select database):

CONTAINER ID   IMAGE                  COMMAND                  CREATED       STATUS       PORTS                               NAMES

d9e2fe8c95a6   node-docker_node-app   "docker-entrypoint.s…"   5 hours ago   Up 5 hours   0.0.0.0:3000->3000/tcp              node-docker_node-app_1
002105dd578c   phpmyadmin             "/docker-entrypoint.…"   5 hours ago   Up 5 hours   0.0.0.0:8080->80/tcp                node-docker_phpmyadmin_1
b8cfa7323d3b   mysql                  "docker-entrypoint.s…"   5 hours ago   Up 5 hours   0.0.0.0:3306->3306/tcp, 33060/tcp   node-docker_db_1

list of all node_modules (mysql is inside)

@sindresorhus       call-bind       decompress-response fresh           imurmurhash     keyv            nopt            range-parser        statuses        util-deprecate
@szmarczak      camelcase       deep-extend     fsevents        inherits        latest-version      normalize-path      raw-body        string-width        utils-merge
abbrev          chalk           defer-to-connect    function-bind       ini         lowercase-keys      normalize-url       rc          string_decoder      vary
accepts         chokidar        depd            get-intrinsic       ipaddr.js       lru-cache       object-inspect      readable-stream     strip-ansi      widest-line
ansi-align      ci-info         destroy         get-stream      is-binary-path      make-dir        on-finished     readdirp        strip-json-comments wrap-ansi
ansi-regex      cli-boxes       dot-prop        glob-parent     is-ci           media-typer     once            registry-auth-token supports-color      wrappy
ansi-styles     clone-response      duplexer3       global-dirs     is-extglob      merge-descriptors   p-cancelable        registry-url        to-readable-stream  write-file-atomic
anymatch        color-convert       ee-first        got         is-fullwidth-code-point methods         package-json        responselike        to-regex-range      xdg-basedir
array-flatten       color-name      emoji-regex     graceful-fs     is-glob         mime            parseurl        safe-buffer     toidentifier        yallist
balanced-match      concat-map      encodeurl       has         is-installed-globally   mime-db         path-to-regexp      safer-buffer        touch
bignumber.js        configstore     end-of-stream       has-flag        is-npm          mime-types      picomatch       semver          type-fest
binary-extensions   content-disposition escape-goat     has-symbols     is-number       mimic-response      prepend-http        semver-diff     type-is
body-parser     content-type        escape-html     has-yarn        is-obj          minimatch       process-nextick-args    send            typedarray-to-buffer
boxen           cookie          etag            http-cache-semantics    is-path-inside      minimist        proxy-addr      serve-static        undefsafe
brace-expansion     cookie-signature    express         http-errors     is-typedarray       ms          pstree.remy     setprototypeof      unique-string
braces          core-util-is        fill-range      iconv-lite      is-yarn-global      mysql           pump            side-channel        unpipe
bytes           crypto-random-string    finalhandler        ignore-by-default   isarray         negotiator      pupa            signal-exit     update-notifier
cacheable-request   debug           forwarded       import-lazy     json-buffer     nodemon         qs          sqlstring       url-parse-lax

After I add this line, save and reload the page:

try {
  const mysql = require("mysql");
} catch (error) {
  app.get("/", (req, res) => {
    res.send(`${error}`)
  })
}

I get this message:

Error: Cannot find module 'mysql' Require stack: - /app/index.js

Although mysql module is inside

P.S Network > No throttling. Firewall is off

IGentlich
  • 81
  • 2
  • 13

1 Answers1

0

Got an answer here: https://stackoverflow.com/a/30436256/6723047

rm -rf node_modules
npm install

Fixed it

IGentlich
  • 81
  • 2
  • 13