How to Publish Your First NPM Package: A Beginner's Tutorial

Search for a command to run...

No comments yet. Be the first to comment.
In the dynamic world of web development, ensuring your website stands out to search engines is as crucial as the development itself. Next.js, a React framework, offers powerful features for building highly performive and SEO-friendly websites. Here, ...

The recent Google I/O Connect event in Bangalore was a treasure trove of exciting announcements for web developers. With a focus on streamlining workflows and empowering developers to build more feature-rich applications, the event showcased a range ...

Node.js (version 14 or higher) installed on your system
NPM (version 6 or higher) installed on your system
A GitHub account (optional but recommended)
A code editor or IDE of your choice
Open your terminal or command prompt and navigate to the directory where you want to create your package.
Run the following command to create a new directory for your package:
mkdir my-package
Navigate into the newly created directory:
cd my-package
Run the following command to initialize a new NPM package:
npm init
Follow the prompts to fill in the required information:
package name: The name of your package (e.g., my-package).
version: The initial version of your package (e.g., 1.0.0).
description: A brief description of your package.
entry point: The main file of your package (e.g., index.js).
test command: The command to run your tests (e.g., npm test).
git repository: The URL of your GitHub repository (optional).
keywords: Keywords related to your package (e.g., javascript, utility).
author: Your name and email address.
license: The license under which your package is released (e.g., MIT).
Create a new file called index.js in the root of your package directory:
touch index.js
Open the index.js file in your code editor and add the following code:
function greet(name) { console.log(Hello, ${name}!); }
module.exports = greet;
Save the file.
Create a new file called test.js in the root of your package directory:
touch test.js
Open the test.js file in your code editor and add the following code:
const greet = require('./index');
describe('greet', () => { it('should print a greeting message', () => { const message = 'Hello, John!'; console.log = jest.fn(); greet('John'); expect(console.log).toHaveBeenCalledWith(message); }); });
Save the file.
Open your package.json file and add the following script:
"scripts": {
"build": "echo 'Building package...'"
}
Run the following command to build your package:
npm run build
Create an NPM account if you haven't already.
Run the following command to login to your NPM account:
npm login
Follow the prompts to enter your NPM username, password, and email address.
Run the following command to publish your package:
npm publish
Follow the prompts to confirm that you want to publish your package.
Make changes to your package code.
Update the version number in your package.json file.
Run the following command to publish the updated package:
npm publish
To use your newly published package in another project, run the following command:
npm install my-package
Then, in your JavaScript file, import and use the package:
const greet = require('my-package');
greet('John'); // Output: Hello, John!
That's it! You've successfully created and published an NPM package.