Oct 05, 2024
How to Build and Publish NPM Package step-by-step.
Step 1: Setup Your Project
Create a Directory:
Start by creating a directory for your project:mkdir my-npm-package
cd my-npm-packageInitialize the Package:
Initialize the project by creating apackage.json
file:npm init
Follow the prompts and provide the necessary details such as the package name, version, description, entry point (default is
index.js
), etc.
Step 2: Create Your Package Code
Write Your Code:
Create the main file for your package, for example,index.js
:// index.jsfunction sayHello(name) {
return `Hello, ${name}!`;
}
module.exports = { sayHello };Add TypeScript Support (Optional):
If you’re using TypeScript, set it up:npm install typescript --save-dev
npx tsc --initWrite your TypeScript code and compile it before publishing.
Test Your Code:
Create atest.js
file to test your package before publishing:const { sayHello } = require('./index');
console.log(sayHello('World')); // Output: Hello, World!
Step 3: Prepare for Publishing
Add
.gitignore
and.npmignore
:
Ensure that unnecessary files are not included in your package:- .gitignore (for GitHub)
- .npmignore (for npm)
Versioning:
Ensure yourpackage.json
contains the correct version number ("version": "1.0.0"
). You should update this every time you publish new changes.
Step 4: Publish to NPM
Login to NPM:
If you don’t have an NPM account, create one here. Then log in:npm login
Publish Your Package:
To publish a public package, simply run:npm publish
If your package name is already taken, you’ll need to either change the name in
package.json
or scope it by adding a username before the package name (e.g.,"name": "@yourusername/mypackage"
).Publish a Scoped Package as Public:
If you are publishing a scoped package, by default it will be private. To publish it as public, use:npm publish --access public
Step 5: Test Your Published Package
Install the Package Globally or Locally:
npm install my-npm-package
Use the Package:
Test it in another project by requiring or importing it:const { sayHello } = require('my-npm-package');
console.log(sayHello('Developer')); // Output: Hello, Developer!
Step 6: Update the Package (Optional)
When you want to release a new version of your package:
Update the Version:
Update theversion
field inpackage.json
to the new version number (e.g.,"version": "1.0.1"
).Publish the Updated Version:
npm publish
Step 7: Managing Your Package
Unpublish a Package (within 24 hours):
If you want to remove a package from NPM:npm unpublish my-npm-package --force
Depricate a Package:
If you no longer want people to use your package, you can deprecate it:npm deprecate my-npm-package@"<version>" "This version is deprecated"
That’s it! You've successfully built, published, and managed your NPM package.
OTHER POSTS
Arduino Project 2: Controlling an LED with a Button
This project demonstrates how to control an LED using a push button with an Arduino UNO. The button will act as the input, and the LED will serve as the output. By pressing the button, we will turn the LED on, and by releasing it, the LED will turn off. This experiment introduces the basic concepts of input and output control using Arduino components.
Read more →Arduino Project 1: Button State Detection
In this beginner-level Arduino project, we will utilize a simple push button to interact with the Arduino UNO board. The goal is to detect whether the button is pressed or not, with the state displayed on the Serial Monitor. This project serves as an introduction to reading digital input signals using an Arduino and understanding the basic concepts of input-output mechanisms in electronics.
Read more →How to Publish Package to NPM – Step by Step
Publishing your package to NPM (Node Package Manager) allows you to share your code with the developer community. It’s a simple process that can take your open-source contribution to the next level. This guide will walk you through publishing your first NPM package.
Read more →