5

instead of requireing code relatively, ie starting with ./ or .., i'd like to define a module "globally". For example, take the following package structure:

/src
  /index.js
  /a.js
  /b.js
/lib
  /index.js
  ...

When in src/a.js or src/b.js, to require lib, I would have to do require('../lib') each time. This gets annoying when you start nesting more as you would have to manually resolve ../lib or ../../lib or ../../../lib.

I want to be able to do require('lib'). Can I do this? Or should I just use globals?

Jonathan Ong
  • 19,927
  • 17
  • 79
  • 118

4 Answers4

0

placing your module in node_modules dont require you to include a path or relative path

EDIT:

if you place a file named package.json inside the module directory, Node will try to parse that file and look for and use the main attribute as a relative path for the entry point. For instance, if your

./myModuleDir/package.json

file looks something like the following, Node will try to load the file with the path

./myModuleDir/lib/myModule.js

:

{
"name" : "myModule",
"main" : "./lib/myModule.js"
}

If that folder does not contain a package definition file named package.json, the package entry point will assume the default value of index.js, and Node will look, in this case, for a file under the path ./myModuleDir/index.js.

Firdous
  • 4,624
  • 13
  • 41
  • 80
  • then i can't commit the code because i have node_modules in my .gitignore – Jonathan Ong May 17 '13 at 07:01
  • -1: node_modules is not made for that purpose. Putting your code inside node_modules is very ugly and bad practice. node_modules is the place for third-party modules. – Gabriel Llamas May 17 '13 at 10:28
0

Using a non relative path to require your source files is not how node's require is intended to work! Don't try to work around this restriction by placing arbitrary code file in node_modules directory or workaround by changing the NODE_PATH environment variable.

If you want to use require without a path you should extract the required code as a node module and depend on this node module. This leads to better structured code, less complex modules, encapsulated functionality, better testability and easier code reuse.

You can include package dependencies from http or git so there is no requirement to publish node modules you use in npm. Take a look at npm dependencies for more detail.

saintedlama
  • 6,838
  • 1
  • 28
  • 46
  • the reason i asked this question was because i was looking into adding a dep as a submodule... but i quickly gave up. still interested in the answer to this question though. – Jonathan Ong May 17 '13 at 06:22
0

use module.exports in the index.js file . and place it inside the node_modules folder

Ashisha Nautiyal
  • 1,389
  • 2
  • 19
  • 39
0

if relative path annoy you and you want to use lib always in your application, you can use global variable like this.

var lib = require('./lib');
global.lib = lib;

you can set lib to global variable in your entry point. after then you can access just lib. but it's pollute global scope. so you have to use carefully.

Outsider
  • 923
  • 1
  • 9
  • 20