This post was originally published on my blog
Packages, packages everywhere, big or small pieces of code that make development way easier and fast. Sometimes we use a prepared package developed by other members of the community, and sometimes we create a package for ourselves. Maybe you are working in a company and want to create an npm package for your team or simply you are just trying to make a piece of code reusable in more than one project. It's a good choice but there's a question. How can I use and install that package without putting it up on the npm public registry? Let's figure it out.
Create your first NPM package
Starting a new package is just as easy as starting a new javascript project. we are going to call our package "legendary" Open your terminal and run this command to create a folder for our legendary package and go into that directory:
mkdir legendary && cd legendary
Now we initialize a new package.json
by running this command:
npm init
You will be asked some questions like the package name, version, license and etc. Answer them and then go to the next step. The result will be a package.json
with contents similar to this:
{
"name": "legendary",
"version": "1.0.0",
"description": "A legendary package",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Moein Hosseini",
"license": "ISC"
}
Write package code and add a private field
Now it's time to add some code to our library. As you saw on our package.json
file, it will be named index.js
because that's our main file. The code can be a simple function like this:
export default function () {
console.log('Hello from legendary package.')
}
Now we should remember to add the private
field in package.json
file. When you set it to true
. Npm will refuse to publish it on npm and prevents your code from being published accidentally on the npm public registry.
{
...,
"private": true,
...
}
Private npm registry
If you have more than one packages and more than one user you might need to have a private npm registry. There are some options out there like the npm proxy registry that you can publish your private packages on a private server.
Publish package on a private registry
To publish your package in a private registry you must have a user on it and log in using the npm command-line interface
npm login --registry=https://mysecretregistry.com
At this point you are logged into your private registry and can publish your package but what do we mean by our package? What files should be included in this library? You can specify them by adding a field named files
in the package.json
file. For my legendary package, I have index.js
.
{
...,
"files": [
"index.js",
],
...
}
Finally to publish your npm package use the below command. This command publishes the package with the specified version number in the package.json
file.
npm publish --registry=https://mysecretregistry.com
Install package from private registry
Installing a package from private is just like the regular ones with specifying the registry address:
npm install legendary --registry=https://mysecretregistry.com
Static package files
You always don't need a private registry. It cost's a lot and also you don't want to put it on the registry while you are the only user for that package. So there is a better way. Just run the following command to generate a static compressed file that can be installed by npm:
npm pack
The result will be a file named legendary-1.0.0.tgz
. Take this file to any directory you want and install it by just giving the file address to npm:
npm install /some/dir/legendary-1.0.0.tgz
Now import your legendary library and use it in as many projects as you want.
Top comments (0)