crear-publicar-paquete-npm

Creation and Publication of NPM Packages

  • 3 min

In this tutorial, we will see how to create and publish our own NPM packages, whether in a public or private registry.

As we have seen in the course, NPM packages are the fundamental unit of reusable code in the JavaScript/Node.js ecosystem.

So it is very likely that at some point you will want to create your own NPM packages and upload them to either a public or private repository.

Moreover, it is something very useful and straightforward. So let’s get to it 👇.

How to create an NPM package

To create our own package, we start by creating a Node.js project. For this, create a new folder for your NPM package.

mkdir my-package
cd my-package

And we initialize the project with

npm init

Once your project is initialized, we can start writing the code for your library. For example, create a file named index.js (or whatever name you choose) and add your code,

function greeting(name) {
  return `Hello, ${name}!`;
}

module.exports = greeting;

This is a very simple example, but you can create much more complex packages that include various functionalities and classes.

Configure the package.json file

Now we need to configure the package.json file, which contains all the information about your package and its dependencies.

{
  "name": "my-package",
  "version": "1.0.0",
  "description": "A simple example package that says hello",
  "main": "index.js",
  "scripts": {
    "test": "echo \"No tests specified\""
  },
  "author": "Your name",
  "license": "MIT"
}
  • name: Name of your package (must be unique on NPM)
  • version: The version of your package
  • main: The main file of your package (for example, index.js).
  • dependencies: The dependencies required for your package to work (if any).
  • scripts: The scripts you can run with NPM, such as tests or project builds.

Test the package locally

Before publishing your package, it is generally a good idea to test that it works (just to avoid any issues). To test your package locally, you can install it directly from the folder where you created it.

Create a new project (outside the folder where you created the package), and install the project from the folder by doing:

npm install /path/to/your/package

This will install your package as a local dependency. Then, you can test it in the code of this project:

const greeting = require('my-package');
console.log(greeting('World'));

This is not very clean. Use it only to test that the package works correctly, but not as a production solution.

Publish the package on NPM

Once you are sure everything works correctly, we can publish it on NPM to make it available for download.

First, if you do not have an NPM account, create one at https://www.npmjs.com/signup. Then, log in from the terminal with:

npm login

Once you have logged in (username, email, and password), you can publish your package using the following command:

npm publish

This will upload your package to NPM and it will be available for others to install with:

npm install my-package