DevOps | Cloud | Analytics | Open Source | Programming





How To Fix - "Error: Cannot Find Module" in Node.js ?



In this post, we will see How To Fix - "Error: Cannot Find Module" in Node.js. Sample error


module.js:123
  throw err;


Error: Cannot find module 'XXXX'
   code: 'MODULE\_NOT\_FOUND',

 

Primitive Checks:

  • Check the package.json file at the project root. Does it have the "module" listed ?
  • If yes, then check whether you have installed that module dependency or not. If node is not able to find that module dependency, it would throw that error.
 

  • To check the list of npm user-installed packages, we will use below command in the current working directory (where the npm is installed).

$ cd <project\_directory\_path>
$ npm list

  If you want to list the globally installed packages, use below command in the project directory -


$ cd <project\_directory\_path>

$ npm -g list

  If you don't want the listing of dependencies, use below command -


$ npm list -g –depth= 0

To check whether a specific package is installed or not, use below command -


$ npm list -g \[package-name\]

 

  • To confirm that package install worked correctly, in your module directory, verify that a "node_modules" directory exists. It should contain a directory for the package(s), which were already installed.
   

Solution 1:

If you are done with the above steps and verified that the specific package is not installed, you can go ahead and install the package.  

  • Install using npm package manager. This will create the node_modules directory in your current directory (if one doesn't exist yet) and will download the package to that directory.
  For unscoped package which are always public - i.e. can be searched, downloaded, and installed by anyone.


$ npm install <package\_name>

  For scoped package i.e. scope name is referenced during installation


$ npm install @scope/package-name

  For Private packages - accessible only to the granted users


npm install @scope/private-package-name

 

  • Install packages using yarn package manager
  If you’re starting a project from scratch, use below to create the Yarn-specific files. This adds a package.json config file and a yarn.lock file to your dir. package.json contains config and list of module dependencies. The yarn.lock file locks the dependencies to specific versions to make it consistent.


$ yarn init

$ yarn install

$ yarn add package-name

   

Solution 2:

If above solution doesn't fix your issue, you could retry deleting "node_modules" folder,  package-lock.json & yarn.lock


rm -rf node\_modules
rm package-lock.json
rm yarn.lock

  Hope this helps to solve the error.  

Other Interesting Reads -

   


node cannot find module local file,error: cannot find module local file,error: cannot find module 'node:url',error: cannot find module 'typescript,cannot find module require stack,error: cannot find module 'react,error \[err\_module\_not\_found\]: cannot find module,cannot find module even though it is there,node.js error handling,node.js error cannot find module,node.js error object,node.js error handling best practices,node.js error log file,node.js error 2503,node.js error getaddrinfo enotfound,node.js error handling try catch,node.js error code 2503,node.js not working