Home > database >  Node js shows error when trying to run database js script
Node js shows error when trying to run database js script

Time:12-24

I have no idea why my code isnt working. Here:

import mysql from './node_modules';

var con = mysql.createConnection({
  host: "secret",
  user: "secret",
  password: "secret"
});

con.connect(function(err) {
  console.log("Connected!");
});

and i get in the log after typing "node filename.js"

node:internal/modules/cjs/loader:959
  throw err;
  ^

Error: Cannot find module '/home/setra/Desktop/code/javascript/tests/test.js'
    at Module._resolveFilename (node:internal/modules/cjs/loader:956:15)
    at Module._load (node:internal/modules/cjs/loader:804:27)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:17:47 {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}


Help please?

I've tried every online thing.

CodePudding user response:

Make sure that test.js file is in your work directory. Cannot find module means it can't find that file.

CodePudding user response:

to import mysql correctly you should import it like :

const mysql = require('mysql');
  • Related