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<https://npmjs.org/doc/link.html> from the global modules into your local modules, but that is mostly 'superficial'). Jonathan On Thu, Dec 27, 2012 at 2:44 AM, Isaac Schlueter <[email protected]> wrote: > 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 > > > On Wed, Dec 26, 2012 at 4:33 PM, Domenic Denicola > <[email protected]> wrote: > > 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. > > > > > > On Wednesday, December 26, 2012 10:12:13 AM UTC-5, > [email protected] > > wrote: > >> > >> 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 [email protected] > > To unsubscribe from this group, send email to > > [email protected] > > For more options, visit this group at > > http://groups.google.com/group/nodejs?hl=en?hl=en > > -- > 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 [email protected] > To unsubscribe from this group, send email to > [email protected] > For more options, visit this group at > http://groups.google.com/group/nodejs?hl=en?hl=en > -- 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 [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/nodejs?hl=en?hl=en
