Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Seriously debugging node.js 'Cannot find module xyz/abcd'

I have the Error: Cannot find module xyz/abcd error.

YES, the required module is installed.

According to the pseudo-code of module.require here, it should work.

I tried to dive inside require() to understand why it can't find my file. My node debugger won't step into require(). I've tried the strace / grep NOENT technique whithout success either.

Any idea how to troubleshoot a nasty require() failure ?

Note : just in case : the error comes from a file node_modules/xyz/a requiring xyz/b. It should work according to the doc.

like image 847
Offirmo Avatar asked Jan 11 '14 00:01

Offirmo


People also ask

How do I resolve Cannot find module error using node JS?

To solve the "Cannot find module" error in Node. js, make sure to install the package from the error message if it's a third party package, e.g. npm i somePackage . If you get the error with a local module, make sure to point the node command to a file that exists.

Can not find module in node JS?

To fix Cannot find module errors, install the modules properly by running a npm install command in the appropriate directory as your project's app. js or index. js file. or delete the node_modules folder and package-lock.

What is Node_debug?

NODE_DEBUG is used by built in util. debuglog . This is used by all nodejs builtin core modules and all the third party packages that decide to use it. DEBUG is used by debug module. So if you are using any package that is using this module for logging, then you need to use DEBUG .

Can not find module npm?

To solve the error "Cannot find module 'express'", install the package by running the command npm install express in the root directory of your project. If you don't have a package. json file, create one by running npm init -y . The error occurs when we try to import the express package without installing it.


1 Answers

By investigating more, I think I found a good technique :

export NODE_DEBUG=module
node my_script.js

gives such interesting traces :

looking for "/(...)/tests_init.js" in ["/home/(me)/.nave/installed/0.10.24/lib/node","/home/(me)/.node_modules","/home/(me)/.node_libraries","/home/(me)/.nave/installed/0.10.24/lib/node"]

And BTW, it shows that node is not searching at all where I thought it was. Time to investigate...


[edit] end of the story :

1) it appears that node is not treating well modules installed with npm link. require(...) sometimes fails to select files from inside a npm-linked module. I have no desire to investigate further the exact error conditions, I'll just conclude that npm link is very brittle.

2) beware of node looking for node_modules all the way into the parent dirs of current working directory ! I found that modules from parent folders of my app where sometime selected !

like image 161
Offirmo Avatar answered Oct 16 '22 02:10

Offirmo