简体   繁体   中英

Node js shows error when trying to run database js script

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.

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

to import mysql correctly you should import it like:

const mysql = require('mysql');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM