How to share Node.js code between projects using NPM

nairihar
6 min readDec 10, 2018

--

I will discuss the NPM package manager in this article and give some valuable tips.

In the scope of this article, we will cover the following topics

  • What is NPM
  • How to publish a library to NPM
  • How to maintain big projects using own NPM libraries

What is NPM

NPM is a package manager for JavaScript programming language. It is the default package manager for the JavaScript runtime environment Node.js. It consists of a command line client, also called npm, and an online database of public and paid-for private packages, called the npm registry.

// Google

So npm is a package manager/package registry with many stored libraries (published by different users) that other users can download and use. When we install Node.js, we see that the npm command line interface also has been installed automatically.

There are other package managers, which provide mainly the same functionalities as npm.
Just npm is the most significant package manager in the world compared with other package managers, even with the package managers for other languages.

Let's try to install a library and dive deeper.

npm install lodash

With this command, npm downloads the library calledlodash.
Downloaded library stored under the node_modules folder, which is in the exact location where we ran the command.

As I have mentioned, users publish libraries. Then we can publish our library too.

Let's try to create and publish our small library like lodashso others can use it too.

How to publish a library to NPM

First of all, we should create an account in npm. Then we should log in from the command line interface.

npm login

After successful login, we can start the process.

Imagine we have this small functionality/lib we want to publish in the npm registry.

We need to create a package.json file which will describe some information about our library.

Create it using this command

npm init

After answering all questions, we will have package.json a file like this:

Depending on the npm client version, generated file can be a little different.

It's essential to check the npm registry to be sure that there is no other package with the same name. Otherwise, we can't publish this package.

For our example, we need to check this URL:
https://www.npmjs.com/package/library-like-lodash-for-article

If we try to open this URL, then we will see an existing package because I have already published it in npm :))

Choose another name and try again.

https://www.npmjs.com/package/LIB_NAME

In case it returns a 404 page, then it's perfect. It means there is no package with the name which we have chosen.

We can add more information (i.e. keywords, license, description, etc.) about our package inside thepackage.json file. More about that read here.

And the final important thing that we need to do before publishing is adding theREADME.md file. We should include some library descriptions and provide details for the supporting functionalities.
NPM will show all the information here on the main page of the package.

https://github.com/nairihar/running-across-npm-article/tree/master/package

If we have some files which we don't want to publish in npm, then we can add them into the .npmignore file, which works like .gitignore.

There are ignored paths and files by default, so there's no need to add them to the .npmignore file. You can find the list of the ignored filenames and paths in NPM's documentation.

Check my Github repository for examples.

Decisive time, let's publish the library.

npm publish

Yuhu, the library has just been published successfully.
https://www.npmjs.com/package/library-like-lodash-for-article

Let's install it.

Create a new folder and install the library.

npm install Library_Name

It's time to require the library and test it.
To test this library, we can use the existing example from README.
https://www.npmjs.com/package/library-like-lodash-for-article

Now try to open thenode_modules folder and find the installed library.
You will see all the files from the library which we have just published.

OK, we have just successfully published the first version.
Let's think about future package updates.

Imagine we need to make some changes to the library.

As we can see, the isNumber function is new.

Now we should update README.md (optional) and the package/library version in thepackage.json file.

"version": "1.0.1",

Publish the library's new changes to npm using the same command before.

npm publish

Change the version before publishing. Otherwise, you will get an error similar to this one:

You cannot publish over the previously published versions: X.X.X.

After successful publishing, we can check the library's npm page. NPM will change the latest version number, and if we try to install it again, it will download the newest version. We can also specify a fixed version of the package we want to use.

npm install Library_Name@1.0.0

About semantic versioning, you can read it here.

How to maintain big projects using own NPM libraries

Sometimes we need to create packages only for internal usage (i.e. for a company or a custom project). We can create a lib folder inside the project's root directory. Then add all the custom packages inside the lib folder and push all this to the same source control/repository.

Or we can create a private repository and register them in the package.json file using the repo URL, like this:

"lodash": "https://github.com/lodash/lodash.git"

But this solution isn't good enough since we need to maintain the package versioning. It's better to publish the packages separately into a registry.

Private packages

Fortunately, npm provides a way to publish private packages.
But it's paid service. There are also other package managers too which provide the same service for free (i.e. Github packages).

With the paid version of NPM, unique users can see and work with the libraries.

Symlink

We can create a symlink of the package in the node_modules folder.
Run this command in the example project directory, where you have tested your published library.

npm link /Users/nairihar/libs/Library_Folder

This command will create a symlink in the project's node_modules folder.
And now, when you try to run the project, the library you linked will be required/loaded from the local directory you have just specified.
It's a perfect way to work with several own libraries and debug them without using the original npm libraries. So whenever your changes are ready, you can publish them and specify the new versions into your project.

Otherwise, for every small change, you need to publish a new library version, update it in the project, install and test it and so on.
DON'T EVEN THINK ABOUT IT!

For more about symlinks, read here.

Summary

Everyone can use npm to publish their packages for public usage.
But for private usage, we should use a paid service or another package registry (i.e. Verdaccio, Github, etc.) which can be free.

I will suggest using Github because your project repository is probably stored there, so it would be easier for you to work on your modules in the same system where you work on your projects. Otherwise, you can choose NPM.

We can split our big projects into multiple small projects using packages.
It helps us also in microservice architectures where we have multiple services. We can write small libraries for our projects; then, the libraries will be general for all the projects (i.e. API, Core, etc.).

Thank you, feel free to ask any questions or tweet me @nairihar

Also follow my “JavaScript Universe” newsletter on Telegram: @javascript

--

--

nairihar

Sharing valuable insights on the latest tools and trends in JavaScript. nairi.dev