“Cannot Find Module ‘#'” Error In node.js – How To Fix?

Cannot find module ‘#’ error in node.js

In Node.js, thousands of libraries and modules have been shared by developers to perform a specific function. It helps new projects to avoid rewriting the essential components. Below the post is the best solution for “cannot find module ‘#'” error in Node.js

The cause of this error

Sometimes, npm will throw an error saying Cannot find module ‘#’. If you are building a project and Node.js application return a message.

For example with “box2d.ts” module:

Output:

"Could not find module “box2d.ts"

Maybe your folder project hasn’t installed “box2d.ts” module package. 

How to fix error “cannot find module ‘#'” in node.js?

First, you can ensure that your module is installed or running the correct file in the correct directory.

Method 1: Check the module has installed by “npm list” command

Step 1: “npm list” method

Check your folder project has been installed by npm list. To check for all locally installed ‘box2d.ts’ and their dependencies, navigate to the project folder in your terminal and run ‘npm list’ command. 

Below is the ‘box2d.ts‘ package installed. Your project may not have this module installed if you do not see it.

Hasn’t the module been installed?

If your module has not been in this listing tree, your project may not have this module installed. Let’s install it by this command:

Step 2: Install “#” module package

Terminal/ PowerShell

npm install --save-dev box2d.ts
  • “npm install” directly install your package. More information about ‘npm install’ in here.
  • “–save-dev” include the third-party package in your project’s dependencies.
  • “box2d.ts” the package will install on the project (You can change this by your module package)

After running this command, this module will be added to the folder “node_module” and you can use it on your project.

If still an error? Let’s try the ‘–unsafe-perm’.

For example:

Terminal/ PowerShell

npm install --save-dev --unsafe-perm box2d.ts

Sometimes you may encounter a command that does not run because of a permission error. Use sudo to try:

Terminal/ PowerShell

sudo npm install --save-dev --unsafe-perm box2d.ts

The error may not be resolved, so let’s delete all ‘node_modules’ and ‘package-lock.json’ files and re-install. Follow the below command:

Terminal/ PowerShell

//  delete all package-lock.json and node_modules
rm -f package-lock.json
rm -rf node_modules

// clean all cache of npm
npm cache clean --force

// re-install npm
npm install

Method 2: Re-install npm and “#” module

After that, re-install your package and let’s try on your project.

Terminal/ PowerShell

npm install --save-dev x

“#” module will install and restart VS Code or your IDE.

If you still get this error, let’s check the ‘package.json’ and ensure the “#” module in ‘devDependencies’ has been installed.

package.json

{
  "devDependencies": {
	    "box2d.ts": "^1.0.2"
   }
}

Or try to use the command below to install the latest “#” module version:

Terminal/ PowerShell

npm install --save-dev box2d.ts@latest

“#” package will be contained in the “devDependencies” object of the package.json file instead of in your project’s dependencies.

If this error still gets an error message? Maybe your version “#” module is not compatible with Node.js version. Check them on the official Node.js.

Summary

We hope you enjoy our article about “Cannot find module ‘#’” error in Node.js. If you have any questions or problems, please comment below. We will respond as possible. Thank you for your reading!

Maybe you are interested:

Leave a Reply

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