Say Goodbye To Relative Paths in Node.js

The story's always the same. You start out developing a simple Node application, but soon you want to start adding a few tidbits of functionality. Suddenly you're stuck in the land of relative path hell. Your paths begin to look like your fingers convulsed on the period and slash keys as you attempted to type. You start to lose track of the periods and the slashes it takes to get to the proper path. Before you know it, you're fed up with Node and ready to punch the first person who raves about the benefits of Node's event-driven model. Who cares, the code's unmaintainable. Well, that's certainly how is was for me when I started developing in Node...


Enter your saving grace, the symbolic link, or more succinctly, the symlink. The concept of a symlink is simple; the OS writes a new "file" to the location of your choosing, but the "file" is special. In practice, the "file" is simply a pointer to physical file or directory that resides elsewhere on the disk. There are a couple of ways to permanently remove this coding headache from your life with the symlink.


The first option is to use Slinker. It's a simple, configurable Node.js package that can be used to define a set of submodules (directories) that should be symlinked in your node_modules directory. It's as fun and simple as telling Slinker where to look and what to symlink:

$ npm install slinker{  
    modules: ['models', 'views'],
    modulesBasePath: __dirname

Slinker can be configured to run its magic as a post-install npm script in your package.json:

"scripts": {
    "postinstall": "node postinstall.js"

For additional details and configuration options, browse the Slinker GitHub page.

The second option is to use the native npm command npm link. This command is used to created a globally available package to your node submodule--that is, globally available on your local machine. In short, any node project on your local machine can pull this submodule down as a dependency once you've linked it to npm. But don't worry, the world can see it or pull it down as a dependency, only you. Using npm link is a simple as executing the command in your submodule.

$ cd ~/my-models
$ npm link

There are a couple small but important points to note before using npm link:

  1. The submodule directory you desire to link must contain a package.json configuration file.
  2. The name specified in the package.json file is used for linking...Not the directory name. Assuming the ~/my-models directory above had the following package.json file.
  "name": "my-models",

Now that you've linked your submodule via the npm link command, you are able to link your dependent submodules to the linked module.

$ cd ~/my-views
$ npm link my-models


Symlinking in Node is a simple but effective method the improve your workflow and reduce your code complexity. I've found this to be a quick and painless solution to a problem that every Node developer--especially new ones--have faced.