Node JS file cannot import a JS class

465 views Asked by At

Can someone help me with a node JS problem? Recently i was manipulating routes and then, i needed to insert a function inside a route (method post). Although, i can import this function in every single file of the project, with the exception of one, the route file. I can't import the function in the route JS file, and i can't find why.

Here it goes the code. Also, i have already tried to put "js" on file's final.

import Router from "express";
import { UsuarioController } from "../controllers/userController";

const router = Router()

router.post('/cadastrarusuario', function (req, res) {
    UsuarioController.cadastrarUsuario()
})

export default router;

Here it goes the error...

[nodemon] restarting due to changes...
[nodemon] starting `node server.js`
node:internal/errors:464
    ErrorCaptureStackTrace(err);
    ^

Error [ERR_MODULE_NOT_FOUND]: Cannot find module 'C:\Users\peran\Desktop\TRACK-VISION-main\site\src\controllers\userController' imported from C:\Users\peran\Desktop\TRACK-VISION-main\site\src\routes\userRoute.js
    at new NodeError (node:internal/errors:371:5)
    at finalizeResolution (node:internal/modules/esm/resolve:418:11)
    at moduleResolve (node:internal/modules/esm/resolve:983:10)
    at defaultResolve (node:internal/modules/esm/resolve:1080:11)
    at ESMLoader.resolve (node:internal/modules/esm/loader:530:30)
    at ESMLoader.getModuleJob (node:internal/modules/esm/loader:251:18)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:79:40)
    at link (node:internal/modules/esm/module_job:78:36) {
  code:

Here is the project file structure

File structure

Basically, i'm trying to reference a JS class that is on the userController.js file.

Here is the VSCode vision

enter image description here

It should work like this

enter image description here

1

There are 1 answers

9
cmc On

There seems to be a problem with the path you gave. Have you made sure the directory ../controllers exists? Sounds like maybe you need an extra ../ in your import statement. Where are the other files in which you successfully imported the module located?