DEV Community

Mayowa Ojo
Mayowa Ojo

Posted on

Utilizing symbolic links in your Node.js projects

image from unsplash

Photo by Paul Esch-Laurent on Unsplash

A symbolic link(or symlink) is used to describe any file that contains a reference to some other file or directory which can be in the form of a relative or absolute path. In a way, you can say a symlink is a shortcut file. If you haven't heard of symlinks before, this can significantly speed up your development process and aid productivity so keep reading to learn how. 😁

N.B: Creating symlinks is not a concept specific to Node.js or npm, in fact, it's existed since the 1970s in computer science.

One good use case of symbolic links is during your development process. You normally extract repeated functions and methods or blocks of code into its own module to enable reusability, but you remember you've used the same functionality in a different project and you don't want to copy/paste code. What are your options?

  • import the file from your other project. This could work but you'd have to always use the relative/absolute path and your code can break if there are any changes made in your filesystem.

  • publish your reusable code to npm and install it in your new project. I think you'd agree that this is unnecessary overhead.

This is where symlinks can be useful. Creating a symlink will make your module available globally and you can access it from anywhere in your filesystem. What's even more awesome is you can make your file an executable and have it mapped to a command you can run from your terminal to execute this file.

So how do you create symlinks? npm makes this seamless. It's as easy as typing npm link from the root directory where your module is located(i.e where your package.json is). This will create a symlink between the global directory where your node_modules is located and the local directory where you ran this command.

Now, in your package.json, add the following:


"bin": {
  "<package name>": "<relative path to your file/module"
}

<package name> is whatever you chose to name your file/module. Note that this is what npm will use to locate it. This is also what you'll use to execute the file from the terminal. If for example, you're trying to expose a file named cli.js in a folder called utils, which is in the src folder your path would be ./src/utils/cli.js.

The next step is to use this in your other project. Simply go to that directory and run npm link <package name>. Npm will essentially install that package in your new project and you can import it just like any other npm package without bothering about resolving the path.

Lastly, if you want to run this file as an executable, add the following to the top of the file:

#!/bin/env node

We're essentially telling the system that the JavaScript file interpreter should be usr/bin/env node. This is the path to your node executable and can vary in your system so take note. You can now execute this file from the terminal using just the package name.

P.S: If you run into errors executing your file, check the file permissions in case it's not executable by default. To make it executable, run chmod +x <filename>

Top comments (1)

Collapse
 
samjin profile image
Sam

awesome.