6

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?

5
  • 1
    found this: github.com/nadav-dav/rekuire but i didn't look at it. Commented May 17, 2013 at 5:27
  • If you add the parent directory of lib to your NODE_PATH environment variable, you can require it as if it were globally installed. Commented May 17, 2013 at 5:36
  • so basically, i can just set my cwd as NODE_PATH and I can require('lib')? Commented May 17, 2013 at 6:00
  • It depends on your exact situation, but try this: export NODE_PATH=$NODE_PATH:$PWD (perhaps the export is optional depending on what shell you use) Commented May 17, 2013 at 6:03
  • Duplicate stackoverflow.com/questions/15313720/… Commented May 17, 2013 at 10:24

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.
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.

Comments

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.

2 Comments

then i can't commit the code because i have node_modules in my .gitignore
-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.
-1

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

9 Comments

then i can't commit the code because i have node_modules in my .gitignore
I'm guessing you don't know what gitignore is
share your git repositry url
I don't see how that is relevant
I don't understand what you just said
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.