Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

error 'MODULE NOT FOUND', Cannot find module

I am trying to resole the pathing issues:

this is what my files structure look like:
/www/html/

  • /database/
  1. databaseConnection.js
  • /scripts/
  1. LoginPageScript.js
  • server.js
  • index.html

in the database js file, the following code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

const mysql = require('mysql2');

const pool = mysql.createPool({
  connectionLimit: 10,
  host: process.env.DB_HOST,
  port: process.env.DB_PORT,
  user: process.env.DB_USER,
  password: process.env.DB_PASSWORD,
  database: process.env.DATABASE
});


pool.getConnection((err, connection) => {
  if (err) {
    console.error('Error connetcting to database: ', err);
    return;
  }
  console.log('Connection to database succefully established!');
});

module.exports = { pool };

exports pool module, I assume

now i am trying to import pool into another script, in this case LoginPageScript.js, using:

const { pool } = require('../database/databaseConnection.js');

but the error thrown is:

Cannot find module '../database/databaseConnection'
Require stack:
- /var/www/html/scripts/LoginPageScript.js
- /var/www/html/server.js
    at Module._resolveFilename (node:internal/modules/cjs/loader:956:15)
    at Module._load (node:internal/modules/cjs/loader:804:27)
    at Module.require (node:internal/modules/cjs/loader:1022:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.<anonymous> (/var/www/html/scripts/LoginPageScript.js:3:18)
    at Module._compile (node:internal/modules/cjs/loader:1120:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1174:10)
    at Module.load (node:internal/modules/cjs/loader:998:32)
    at Module._load (node:internal/modules/cjs/loader:839:12)
    at Module.require (node:internal/modules/cjs/loader:1022:19) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/var/www/html/scripts/LoginPageScript.js',
    '/var/www/html/server.js'
  ]
}

I tried experimenting with different pathing like:

  1. /database/databaseConnection.js
  2. ./database/databaseConnection.js
  3. ../database/databaseConnection.js

none seem to be working

>Solution :

Try traversing via dot annotation to databaseConnection.js file.

And try using:

const { pool } = require('./database/databaseConnection.js');
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading