How to make node.js require absolute? (instead of relative)
There's a really interesting section in the Browserify Handbook:
avoiding ../../../../../../..
Not everything in an application properly belongs on the public npm and the overhead of setting up a private npm or git repo is still rather large in many cases. Here are some approaches for avoiding the
../../../../../../../
relative paths problem.node_modules
People sometimes object to putting application-specific modules into node_modules because it is not obvious how to check in your internal modules without also checking in third-party modules from npm.
The answer is quite simple! If you have a
.gitignore
file that ignoresnode_modules
:node_modules
You can just add an exception with
!
for each of your internal application modules:
node_modules/*!node_modules/foo!node_modules/bar
Please note that you can't unignore a subdirectory, if the parent is already ignored. So instead of ignoring
node_modules
, you have to ignore every directory insidenode_modules
with thenode_modules/*
trick, and then you can add your exceptions.Now anywhere in your application you will be able to
require('foo')
orrequire('bar')
without having a very large and fragile relative path.If you have a lot of modules and want to keep them more separate from the third-party modules installed by npm, you can just put them all under a directory in
node_modules
such asnode_modules/app
:node_modules/app/foonode_modules/app/bar
Now you will be able to
require('app/foo')
orrequire('app/bar')
from anywhere in your application.In your
.gitignore
, just add an exception fornode_modules/app
:
node_modules/*!node_modules/app
If your application had transforms configured in package.json, you'll need to create a separate package.json with its own transform field in your
node_modules/foo
ornode_modules/app/foo
component directory because transforms don't apply across module boundaries. This will make your modules more robust against configuration changes in your application and it will be easier to independently reuse the packages outside of your application.symlink
Another handy trick if you are working on an application where you can make symlinks and don't need to support windows is to symlink a
lib/
orapp/
folder intonode_modules
. From the project root, do:ln -s ../lib node_modules/app
and now from anywhere in your project you'll be able to require files in
lib/
by doingrequire('app/foo.js')
to getlib/foo.js
.custom paths
You might see some places talk about using the
$NODE_PATH
environment variable oropts.paths
to add directories for node and browserify to look in to find modules.Unlike most other platforms, using a shell-style array of path directories with
$NODE_PATH
is not as favorable in node compared to making effective use of thenode_modules
directory.This is because your application is more tightly coupled to a runtime environment configuration so there are more moving parts and your application will only work when your environment is setup correctly.
node and browserify both support but discourage the use of
$NODE_PATH
.
I like to make a new node_modules
folder for shared code, then let node and require do what it does best.
for example:
- node_modules // => these are loaded from your package.json- app - node_modules // => add node-style modules - helper.js - models - user - car- package.json- .gitignore
For example, if you're in car/index.js
you can require('helper')
and node will find it!
How node_modules Work
node has a clever algorithm for resolving modules that is unique among rivalplatforms.
If you require('./foo.js')
from /beep/boop/bar.js
, node will look for ./foo.js
in /beep/boop/foo.js
. Paths that start with a ./
or ../
are always local to the file that calls require()
.
If however you require a non-relative name such as require('xyz')
from /beep/boop/foo.js
, node searches these paths in order, stopping at the first match and raising an error if nothing is found:
/beep/boop/node_modules/xyz/beep/node_modules/xyz/node_modules/xyz
For each xyz
directory that exists, node will first look for a xyz/package.json
to see if a "main"
field exists. The "main"
field defines which file should take charge if you require()
the directory path.
For example, if /beep/node_modules/xyz
is the first match and /beep/node_modules/xyz/package.json
has:
{ "name": "xyz", "version": "1.2.3", "main": "lib/abc.js"}
then the exports from /beep/node_modules/xyz/lib/abc.js
will be returned byrequire('xyz')
.
If there is no package.json
or no "main"
field, index.js
is assumed:
/beep/node_modules/xyz/index.js