Guide publish JavaScript package on NPM

Tram Ho

NPM (Node Package Manager) is a tool to create and manage Javascript programming libraries for Node.js. Developers develop NPM packages and publish them to NPM’s server for use and sharing with the community. You often use the command npm install <packageName> to install libraries from NPM, right? Have you ever wondered if you can write your own packages to reuse, share with colleagues and contribute to the developer community? In this article, I will share with you the steps to publish a javascript package with NPM.

(Note: The knowledge you should have before refer to the article information: Install and use NPM (basic), GIT knowledge (basic), Javascript knowledge (basic). is the premise for you to easily access the content presented next).

Step 1: Set up an NPM account

Sign up for an npm account here . This is where we will push packages and manage them.

Step 2: Create Repository git

Create repository at github . Your package needs to be in a pulic repository. This will be the page to help us manage the source code and documentation for the package.

Step 3: Create folder package

You need to create a directory that is the name of the package you want to write. The structure inside the directory of a package is very simple with the following 2 files:

  • json: declare information about package
  • js: write in a module structure, export components. When require package is to perform require this file.

Step 4: Create the package

For everyone to understand quickly, here we will try to make a simple package about random troops in the game Empire (AOE). I chose this example because the number of people who know this game is quite large. You can preview the demo and source code here:

4.1. Create file package.json

First we create directory aoerandom and file package.json to store configuration information of the package. Quick initialization with the command npm init -y , we immediately have the file package.json with the following content:

  • Name: is the name of the package you want to set. By default it is taking the name of the directory containing the package.json . file
  • Version: the name of the current version of the package. Default is 1.0.0
  • Description: information, introduction to the package
  • Main: that is the file written in the export structure. Default is index.js. When you require package, you require this file
  • Scripts: defines the local commands of the package
  • Keywords: keywords related to the package, making it easy to find your package. Keyword will help more people know about your package.
  • License: the license of the package.

With this package, my structure will be as follows:

The main homepage is the github link of the package.

4.2 Contents of the program

Next, I create an index.js file with the following content:

Pretty easy to understand example, right? And with the input data are the types of troops in the game. We have the following two functions:

  • getCountryRandom: Issue a random piece. Used in solo play, one person will randomize first, then the next person will automatically choose counters. For example: A: Greek, then B: choose Egyptian.
  • getTeamRandom: Randomize depending on the number of players. Use in team play.

4.3. Install Packages globally (command)

When installing a global package with the -g parameter, we will use the package’s commands:

In order for NPM to understand the command, we need to continue declaring it with package.json as follows

Detail:

4.4. Testing locally before publishing

Before publishing the package, test it out by publishing it locally with the command npm link .

The result from the command returns:

Next, we test random troops for 1 match 4-4 corresponding to 8 players

Results returned:

Pretty good cards for team 1, right? Let’s try it a second time and see.

Conversely, if you want to remove it, simply with the command

Step 5: Publish package to NPM

First, you will need to login to npm.

Next, let’s push the package up to share with everyone. If this is your first time publishing a package on NPM, you will need to confirm with your registered email address.

So you have successfully published the package. Now, try to visit npm’s website again to check if your package has appeared or not. Package when published will look like this: LINK

Step 6: Using package

Now we can use the package we just made to generate a random AOE page. The settings to use are as follows:

You can use the package now

We see that writing and publishing a package with NPM is fast and simple, right? As a programmer, surely everyone has a lot of favorite source code to share, so why not create a package with your own stamp today?

Some useful links:

Share the news now

Source : Viblo