From Development to Distribution: Testing and Publishing npm Packages

Muhammad Usman
4 min readMay 24, 2024

--

How to test and publish your node/npm package?

Image taken from Google

Publishing an npm package to npmjs.com is a straightforward yet powerful process that allows developers to share their libraries and tools with the broader community. By following the steps outlined in this article, you can ensure that your package is well-prepared, properly documented, and easily accessible to other developers. From setting up your package.json, creating a .npmignore file, logging into npm, to finally publishing your package, each step is crucial for a successful release.

Outcome of article:

  1. How to test your node package locally before publishing it?
  2. How to publish our node package?
  3. How to test or use our published node package in our project?

Test Node Package Locally:

Testing an npm package before publishing it is crucial to ensure that everything works correctly. Here are the steps to thoroughly test your npm package:

a. Create a Local Test Project

Create a separate directory where you can test your package.

mkdir test-project
cd test-project
npm init -y

b. Link Your Package Locally

Inside your package directory, run:

npm link

This creates a symlink globally for your package. Then, in your test project directory, run:

npm link your-package-name

Replace your-package-name with the name of your package as specified in your package.json.

c. Use Your Package in the Test Project

Create a test script in the test project to import and use your package.

// test.js
const yourPackage = require('your-package-name');
// Use your package's functionality here
yourPackage.someFunction();

Run the test script to ensure your package works as expected:

node test.js

Publish Node Package:

Publishing your npm package to npmjs.com involves several steps. Here’s a step-by-step guide on how to do this:

Step 1: Prepare Your Package

Before publishing, ensure your package is ready:

  1. Create a package.json File: If you haven’t already created a package.json file, you can do so by running:
npm init

This command will prompt you to provide details about your package.

2. Check for Required Fields: Ensure your package.json includes all necessary fields like name, version, description, main, and scripts. For example:

{
"name": "your-package-name",
"version": "1.0.0",
"description": "A brief description of your package",
"main": "index.js",
"scripts": {
"test": "mocha"
},
"author": "Your Name",
"license": "MIT"
}

3. Add a .npmignore File: To exclude unnecessary files from your package, you can create a .npmignore file. This file works like .gitignore. For example:

node_modules
test
.git
.DS_Store

Step 2: Create an Account on npmjs.com

If you don’t have an npm account, create one:

  1. Go to npmjs.com and sign up for a new account.
  2. After signing up, verify your email address if required.

Step 3: Login to npm from the Command Line

Login to your npm account using the npm CLI:

npm login

This will prompt you for your username, password, and email. Enter the credentials you used to sign up.

Step 4: Publish Your Package

Now that you are logged in, you can publish your package. Navigate to the directory containing your package and run:

npm publish

If your package name is already taken or you encounter any other errors, you may need to resolve those issues before proceeding. For example, if the package name is taken, you’ll need to choose a different name.

Step 5: Verify Your Package

After publishing, verify that your package is live by visiting https://www.npmjs.com/package/your-package-name (replace your-package-name with your actual package name).

Test Published Node Package Locally:

Following are the steps that you need to do in order to test your published node package:

1. Delete node modules from your Test Project

Delete the node_modules folder in your test project.

2. Install the published package inside your Test Project

Run the below command:

npm i <your_package_name>

After running the above command, it will make a node_modules folder and inside it, you will find your package.

c. Use Your Published Package in the Test Project

Create a test script in the test project to import and use your package.

// test.js
const yourPackage = require('your-package-name');
// Use your package's functionality here
yourPackage.someFunction();

Run the test script to ensure your package works as expected:

node test.js

Conclusion

By completing this process, you will have gained valuable experience in package management, learned how to ensure your package meets quality standards, and understood the importance of proper versioning and documentation. This knowledge not only helps in contributing to the open-source community but also enhances your skills as a proficient developer in the npm ecosystem.

If you like the article, make a clap. Please feel free to write comment here if you have any questions 😊

--

--

Muhammad Usman
Muhammad Usman

Responses (1)