close
close
Error: Could not find module in node. Fixed

Error: cannot find module Occurs when you attempt to load a nonexistent module nodeeither via ECMAScript modules (ESM) or CommonJS module systems.

Error: Cannot find module in node. Explained

Error: cannot find module Occurs in Node when you try to load a non-existent module. The error can also be triggered by a number of other reasons including:

  • You are trying to import a third-party package that you have not yet installed.
  • Importing a local module with the wrong path.
  • Run a node script in terminal but the script is not there.
  • Use Node standard libraries with TypeScript that are not yet installed.
  • The module’s package.json points to a non-existent entry file.

The error usually looks like this in the console:

node:internal/modules/cjs/loader:936
  throw err;
  ^

Error: Cannot find module '/var/www/scripts/app.js'
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
    at Function.Module._load (node:internal/modules/cjs/loader:778:27)
    at Function.executeUserEntryPoint (as runMain) (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:17:47 {
  code: 'MODULE_NOT_FOUND',
  requireStack: ()
}

Why does “Error: Module cannot be found” occur?

The “Module cannot be found” error can occur when you try the following:

  • Also import a third-party package that you have not yet installed npm or yarn.
  • Import a local module but the path is incorrect.
  • Run a node script in the terminal, but the script doesn’t exist or the path is incorrect.
  • Use Node standard libraries typescriptbut you don’t have that installed @types/node Package.
  • The module package.json has a main field that points to an entry file that doesn’t exist.

First, let’s look at what a module is and how modular systems work.

In a modular system, modules are the building blocks of an application. Node implements ESM and CommonJS module systems to allow you to organize your code as reusable components.

Modules allow you to expose only the public interface of your components and keep the internal functionalities private. This happens with a module.exports for CommonJS modules, or export for ES modules.

The following code is an example of a module that has a function to determine whether a number is odd or not:

function isOdd(number) {
return number % 2 !== 0
}

// Make isOdd available to other scripts (and other modules)
module.exports = isOdd

And so we import it into another script called index.js:

// index.js 
const add = require('./isodd.js') 

console.log(isOdd(3)) 
// output: true

All modules you use have an ESM or CommonJS export declaration.

How do modular systems find modules?

If the identifier was passed to require() or import() is a reference to a file, it begins with /, ../, ./etc. in your file system, Node loads it from the appropriate path. Otherwise it looks in the installed modules node_modules Directory.

If the module is not found, this will be triggered cannot find module Mistake.

More about software engineeringTypeError: Cannot call ‘Str’ object in Python

How to fix error: Module cannot be found in Node.js

There are several scenarios where this error can occur. Let’s explore each one.

Third Party Packages

If you’re trying to import a third-party module and get this error and are sure the spelling is correct, it’s probably not the case have installed the package still.

Imagine this is the code:

const axios = require('axios');

// Make an HTTP request with Axios

When I run this on my computer this is what I get cannot find module Error because I haven’t installed it yet. In my project directory where my package.json I would have to install it axios package with npm or yarnsomething like this:

npm install axios

Local modules

Imagine you have a bunch of utility functions stored in one JavaScript File with the name utils.js. You want to import a function called getUrl() from it utils.js into your script:

const { getUrl } = require('utils')

// Do something here ...

But if you run npm run buildthe build fails:

ERROR in ./app/index.js 30:0-30
Module not found: Error: Can't resolve 'utils' in '/var/www/app'

Since utils is not a reference to a local file, my bundler webpack assumes it is in one node_modules Directory. The build fails because the utils Module is not an installed package.

So what I need to do here is make it an absolute path by adding ./ to my ID:

const { getUrl } = require('./utils.js')

// Do something here ...

Problem solved.

Repair the case

Sometimes the error occurs because the Letter case is out. Filenames on Mac and Windows are case-insensitive by default. That means ./Utils And ./utils Both work on Mac and Windows where you develop the app. However, on a Linux file system where filenames are case-sensitive, this can cause problems.

Run a node script

Another scenario reported by developers is running a script using the Node command. However, the corresponding file cannot be found, probably due to a typo in the name or path.

Always check that the path is correct and the script exists. You can always use Terminal’s autocomplete feature by typing the initial letters and pressing Tab to have the path complete for you.

node ./scriptName.js

Node and TypeScript

If you code in TypeScript and import a built-in Node module like fsmaybe you can get one Error: Cannot find module 'fs’to.

You can fix the problem by installing it @types/node:

npm install @types/node --save-dev

Even though all the packages are inside node_modules or @types all enclosing folders are included in your compilation. However, it is worth making sure that the node is added at the bottom compileOptions.types:

{
  compilerOptions: {
    types: (node, jest, express)
  }
}

Missing entry file in package.json: Each module has an entry file named index.js by default unless changed in the module package.json about the main Field.

{
  name: utils
}

If you decide to change that Copy main field to another entry file, e.g main.jsyou need to make sure the file exists. If you rename or move this file in the future, remember to update it package.jsonto.

Missing entry file in package.json was reported by several users on Stack Overflow.

Importing a module from the global node_modules or a separate directory

Sometimes you may need to use a globally installed package in your development environment.

When you try to load a globally installed module into your project, you may see the following: cannot find module Mistake.

A workaround for this problem is to use npm Link command. If the package is installed globally, all you need to do is run the following command in your project directory:

npm link package-name

npm creates a symbolic link from the name of the globally installed package to your project’s node_modules directory, as if you had installed it with it npm install package-name.

If the module you want to use is a local file located somewhere different in your file system than the global one node_modules. For example, if you develop a module, you must do so in a two-step process:

First you need to change the directory in the terminal to the directory where the module is located and run that npm Link (without parameters). This creates a symlink in the global context node_modules which points to your local package.

npm link

Next, change the directory to your project directory where you want to import the module and run the following command:

npm link package-name

This creates a symbolic link from the name of the globally installed package to the node_modules Directory of your project.

Now you can use the package as if it were an installed third-party package.

A tutorial to fix the error: Module cannot be found in Node.js. | Video: Future technology

More about software engineeringHow to fix TypeError: “Int” object is not callable in Python

What happens if the “Module cannot be found” error persists?

If none of the above solutions worked for you, you may be dealing with corrupt or incomplete installations. In this case, you can take the following steps:

Delete node_module Directory:

rm -rf node_modules

Delete package-lock.json:

rm -f package-lock.json

Delete that npm Cache:

npm cache clean --force

Reinstall the packages:

npm install

This will perform a reinstall of all dependencies listed in your package.json File.

What does the error mean: Module cannot be found in node?

Error: cannot find module means you are trying to load a non-existent module into Node. The error looks like this:

node:internal/modules/cjs/loader:936
  throw err;
  ^

Error: Cannot find module '/var/www/scripts/app.js'
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
    at Function.Module._load (node:internal/modules/cjs/loader:778:27)
    at Function.executeUserEntryPoint (as runMain) (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:17:47 {
  code: 'MODULE_NOT_FOUND',
  requireStack: ()
}

What causes the error: Module cannot be found in node?

There are some common causes that trigger the error: Module cannot be found in Node. This includes:

  • You are trying to import a third-party package that you have not yet installed.
  • Importing a local module with the wrong path.
  • Run a node script in terminal but the script is not there.
  • Use Node standard libraries with TypeScript that are not yet installed.
  • The module package.json refers to a non-existent entry file.

Leave a Reply

Your email address will not be published. Required fields are marked *