I have a project structured as follows:
In my auth.js file, I’m trying to import connect.js using the following import statement:
import { db } from "../connect.js";
However, despite double-checking the relative paths and ensuring the correct file structure, I’m still getting the “Cannot find module” error.
I’ve double-checked the file structure and import statements.
I’ve restarted nodemon and my Node.js application.
Here is the entire error:
Node.js v18.16.1
[nodemon] app crashed - waiting for file changes before starting...
[nodemon] restarting due to changes...
[nodemon] starting `node index.js`
node:internal/errors:490
ErrorCaptureStackTrace(err);
^
Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/Users/Valeria/Desktop/social-project/api/connect.js' imported from /Users/Valeria/Desktop/social-project/api/controllers/auth.js
at new NodeError (node:internal/errors:399:5)
at finalizeResolution (node:internal/modules/esm/resolve:326:11)
at moduleResolve (node:internal/modules/esm/resolve:945:10)
at defaultResolve (node:internal/modules/esm/resolve:1153:11)
at nextResolve (node:internal/modules/esm/loader:163:28)
at ESMLoader.resolve (node:internal/modules/esm/loader:838:30)
at ESMLoader.getModuleJob (node:internal/modules/esm/loader:424:18)
at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:77:40)
at link (node:internal/modules/esm/module_job:76:36) {
code: 'ERR_MODULE_NOT_FOUND'
}
Node.js v18.16.1
[nodemon] app crashed - waiting for file changes before starting...
Here is my connect.js
import mysql from "mysql";
export const db = mysql.createConnection({
host:"localhost",
user:"root",
password:"",
database:"social"
});
Here is my auth.js
import { db } from "../connect.js";
import bcrypt from "bcrypt.js";
export const register = (req, res)=>{
const q = "SELECT * FROM users WHERE username = ?"
db.query(q, [req.body.username], (err,data)=>{
if(err) return res.status(500).json(err);
if(data.length) return res.status(409).json("User already exists");
const salt = bcrypt.genSaltSync(10);
const hashedPassword = bcrypt.hashSync(req.body.password, salt);
const q = "INSERT INTO users (`username`, `email`,`password`, `name`) VALUES (?)";
const values = [req.body.username,req.body.email, hashedPassword,req.body.name];
db.query(q, [values], (err, data) => {
if (err) return res.status(500).json(err);
return res.status(200).json("Registration successful");
});
});
};
export const login = (req, res)=>{
}
export const logout = (req, res)=>{
}
Any assistance or insights into resolving this issue would be greatly appreciated. Thank you!
I’m testing my project through Insomnia and receiving
Error: Couldn’t connect to server
http://localhost:8800/api/auth/register
My JSON:
{
"username":"test",
"email":"[email protected]",
"password":"123456",
"name":"John Doe"
}