Home > Back-end >  Node import statement can't recognize ../ prefix
Node import statement can't recognize ../ prefix

Time:01-12

I am trying to use the ES Module import with a path like ../module.js and it gives me this error:

node:internal/errors:464
    ErrorCaptureStackTrace(err);
    ^

Error [ERR_MODULE_NOT_FOUND]: Cannot find module 

Here is the structure of the folders:

.
├── module.js
├── index.js
├── folder
│   └── test.js

Also here is the import and export statements

//folder/test.js
import { test } from "../module"

//module.js
export async function test(data) {
    //Do Stuff then return data
}

Obviously, I have changed some file names and removed the function code. Please let me know if this is a problem.

Thank you!

CodePudding user response:

In ECMAScript Modules, file extensions are mandatory according to the documentation, as well as directory indexes must be explicitly specified:

import { test } from "../module.js";

CodePudding user response:

module is already declared into nodejs, try changing the name of your file to something else.

  •  Tags:  
  • Related