Re: [nodejs] require() MODULE_NOT_FOUND - yet it does exist

1,121 views
Skip to first unread message

José F. Romaniello

unread,
Dec 26, 2012, 7:29:00 PM12/26/12
to nod...@googlegroups.com
How does module_file looks like? There are some rules, for instance,
if its in the same folder it should start with ./, like
require('./somefile')

2012/12/26, dar...@darrenwhitlen.com <dar...@darrenwhitlen.com>:
> Hi,
>
> Trying to simply require() in a .js file seems to be failing for me here,
> raising a MODULE_NOT_FOUND error.
> However - a fs.readFileSync() on the file does work.
>
> My current failing debug code:
> // Fails
> require(require('path').resolve(module_file));
>
> // Works
> require('fs').readFileSync(require('path').resolve(module_file), 'UTF8');
>
> Since the readFileSync() call works I can rule out any file/directory
> permissions leaving me with absolutely no ideas left as to why require()
> cannot find the file?
>
> Anyone have any debugging pointers to share?
>
> Darren
>
> --
> Job Board: http://jobs.nodejs.org/
> Posting guidelines:
> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> You received this message because you are subscribed to the Google
> Groups "nodejs" group.
> To post to this group, send email to nod...@googlegroups.com
> To unsubscribe from this group, send email to
> nodejs+un...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en?hl=en
>

--
Enviado desde mi dispositivo móvil

Domenic Denicola

unread,
Dec 26, 2012, 7:33:48 PM12/26/12
to nod...@googlegroups.com
I don't believe `require` works with absolute paths, like those generated by path.resolve. (Could be wrong.) If I'm correct, then you may find path.relative to be a useful method.

Also note that of course path.resolve without a second argument will resolve relative to process.cwd(), not relative to __filename. This is rarely what you want.

Isaac Schlueter

unread,
Dec 26, 2012, 7:44:57 PM12/26/12
to nodejs
Yeah, share the actual example code.

require() works fine with absolute paths.

$ cat > b.js
console.log('hello from b', __filename)
^D
$ cat > a.js
require(require('path').resolve('b.js'))
^D
$ node a.js
hello from b /Users/isaacs/dev/js/node-master/b.js

Jonathan Chayce Dickinson

unread,
Dec 27, 2012, 3:43:40 AM12/27/12
to nod...@googlegroups.com
In a nutshell you need to have the .js file in a directory node_modules either in the directory that contains the .js that is 'requiring', or a ancestor directory. For example, assuming the file that is 'requiring' lives at: /users/thatguy/dev/foo/bar/baz/myscript.js, the following paths would be checked for require('fob').

  • /users/thatguy/dev/foo/bar/baz/node_modules/fob.js
  • /users/thatguy/dev/foo/bar/baz/node_modules/fob/index.js
  • /users/thatguy/dev/foo/bar/node_modules/fob.js
  • /users/thatguy/dev/foo/bar/node_modules/fob/index.js
  • /users/thatguy/dev/foo/node_modules/fob.js
  • /users/thatguy/dev/foo/node_modules/fob/index.js
  • /users/thatguy/dev/node_modules/fob.js
  • /users/thatguy/dev/node_modules/fob/index.js
  • /users/thatguy/node_modules/fob.js
  • /users/thatguy/node_modules/fob/index.js
  • /users/node_modules/fob.js
  • /users/node_modules/fob/index.js
  • /node_modules/fob.js
  • /node_modules/fob/index.js
Note that you can override the 'index.js' with a package.json: http://nodejs.org/api/modules.html#modules_folders_as_modules

What I suspect may be catching you off-guard is that you assume that global modules are checked as well. The name is 'global modules' entirely misleading, it's more along the lines of 'global node binaries'. When you install a package with -g npm creates a script that invokes it and nothing more (allowing you to e.g. 'jake build' from anywhere). 'require' does not look in the global modules (you can get npm to symlink from the global modules into your local modules, but that is mostly 'superficial').

Jonathan

Jonathan Chayce Dickinson

unread,
Dec 27, 2012, 3:46:06 AM12/27/12
to nod...@googlegroups.com
Also, as far as Windows goes, require does work with absolute paths (I am using it): that may be a platform inconsistency though - anyone care to check on a *nix?

Jonathan

Martin Cooper

unread,
Dec 27, 2012, 12:10:53 PM12/27/12
to nod...@googlegroups.com
On Thu, Dec 27, 2012 at 12:43 AM, Jonathan Chayce Dickinson <jonath...@gmail.com> wrote:
In a nutshell you need to have the .js file in a directory node_modules either in the directory that contains the .js that is 'requiring', or a ancestor directory. For example, assuming the file that is 'requiring' lives at: /users/thatguy/dev/foo/bar/baz/myscript.js, the following paths would be checked for require('fob').

All of that is bypassed, though, if the value passed to require() is an absolute path. With an absolute path, the target can be anywhere, and no search / path walk is required.

--
Martin Cooper
 

Reply all
Reply to author
Forward
0 new messages