How to solve "Error [ERR_MODULE_NOT_FOUND] "

How to solve "Error [ERR_MODULE_NOT_FOUND] "

ยท

1 min read

Hello and welcome!

Are you facing the "Error [ERR_MODULE_NOT_FOUND]" error while trying to import a module in your Node.js application? Don't worry, in this article, we'll guide you through the steps to solve this error and get your application up and running again.

Check The Syntax Of import Or export :

This is the correct format of code:

// import syntax
import  dataString from "./model/user.js";
import { userRouter } from "./router/users.js";

// export syntax
export { router as userRouter } 
export default dataString

Check Package.json file for type:"module":

Make sure you have specify "type" : "module"

{
//check this ๐Ÿ‘‡
  "type": "module",
  "dependencies": {
    "bcrypt": "^5.1.0",
    "cors": "^2.8.5",
    "express": "^4.18.2",
    "jsonwebtoken": "^9.0.0",
    "mongoose": "^7.0.5"
  }
}

By default it is "type" : " commonjs" you have to add "type" : "module" in package.json file.

Make sure your Path you have pass is correct:

~ crud_app
    ~ src
        ~ models
            ~ dataString

import dataString from "./src/models/dataString

If it is still not solved?

make sure that you are not mixing the use of require() and import statements in your application. Stick to one syntax throughout your application.

By following these steps, you should be able to resolve the "Error [ERR_MODULE_NOT_FOUND]" error and get back to coding in no time. Happy coding!

ย