Creating and publishing an npm package involves preparing your project, configuring it properly, and publishing it to the npm registry for public use.

- Initialize the project using npm init and configure the package.json file properly.
- Add your module code, test it, and ensure proper versioning and documentation.
- Login using npm login and publish the package with npm publish.
Step-by-Step Guide to Building and Publishing an npm Package
It explains the complete lifecycle of an npm package, from initial setup and module creation to publishing, version management, and maintenance.
1. Setup a Project
Setting up a project is required before doing anything.
- Install Node.js
- Create an npm account.

- Logging in to the npm account using npm login

2. Initializing a module
To initialize a module, go to the terminal/command-line and type npm init and answer the prompts.

- Set the version to 0.0.0 initially and choose a proper entry point (commonly src/index.js).
- Provide repository details, keywords, author, and license (can be updated later in package.json).
- Add "type": "module" in package.json to enable ES6 import/export.
- Include a README.md and maintain a clean project structure for better usability and documentation.

3. Building a module
This phase is the coding phase. If you have any experience in using NPM modules, you would know that NPM modules expose methods that are then used by the project. A typical control flow is:

Implement a simple function that adds two numbers in the npm module.
File Name: index.jsÂ
export const gfgFns = {
add : function addTwoNums (num1, num2 ) {
return (num1 + num2) ;
}
}
- index.js serves as the entry point of the npm module.
- gfgFns is an exported object containing the add method to add two numbers.
- The object is exported using export const gfgFns, allowing it to be imported in other files.
4. Publishing a module
After completing the module, publish it using npm publish. Before publishing, check if the package name is available by running npm search packagename, as duplicate names cannot be published.
npm search packagenameIf your package name is usable, it should show something like the image below.

If your module name already exists, go to the package.json file of the npm module project and change the module name to something else.
Now after checking the name availability, go to command-line/terminal and do the following:
npm publish
- Create a new project directory and run npm init to initialize the Node.js project and generate package.json.
- Install the published package using npm install gfgnpmpkgupload to add it as a project dependency.

- Run the application using node filename.js to verify that the installed module is properly imported and functioning as expected.

5. Updating and managing versions
As a package evolves, versioning is required to track bug fixes, minor updates, and major releases; npm supports semantic versioning through controlled version increments (patch, minor, and major).
Versioning and publishing code: NPM allows us to version our module on the basis of semantic-versioning. There are three types of version bumps that we can do, ie, patch, minor, and major. For example, if the current module version is 0.4.5:
# Minor version increments reset the patch version to 0
# Major version increments reset both minor and patch versions to 0
# 0.4.5 to 0.4.6
npm version patch
# 0.4.5 to 0.5.0
npm version minor
# 0.4.5 to 1.0.0
npm version major
When we run the above command, the version number in the package.json file is automatically updated as well.Â
Note: If the module is being re-published without bumping up the version, the NPM command line will throw an error. For example, look at the below image.

- Version bump required before publishing: npm throws an error if npm publish is executed without incrementing the version number.
- Version downgrade not allowed: A published version cannot be decreased (e.g., 0.1.2 cannot revert to 0.1.1).
- Updating for users: When a new version is published, users can install the latest version by running, npm install <packagename>
A package dependent on other packages: During npm package development, it is common to include external dependencies required for functionality, which are installed and managed through npm and recorded in the dependencies section of package.json.
- Install the required external dependencies in the npm module project using npm install <package-name>.
- The installed packages are automatically added under the "dependencies" section in package.json.
npm install packagename1[ packagename2]- Ensure dependencies are listed under "dependencies" in package.json, as they are included during publishing.
- Bump the version if required and publish the package using npm publish.
> npm version minor
npm publish
- The above procedure should execute successfully, and the result should be available to see in the npm registry website like below:

Building a more complex module: Develop an npm module that reads a text file, extracts numerical values, computes their sum, and outputs the result to the console.Â
- Initialize the new project using:
npm init -y- Install the published module:
npm install gfgnpmpkgupload- Import the installed module into the project to utilize its functionality.

The datafiles should contain a numFile.txt that has the numbers that have to be added and displayed in the console.
// numFile.txt - sum = 55
1 2 3 4 5 6 7 8 9 10
To consume this numFile.txt, we will have a gfgapp.js that will do the actual addition.

To test this, go to command-line and run
node gfgapp.js
NPM module boilerplate: Preconfigured npm module boilerplates can be generated using Yeoman to streamline project scaffolding.
- Use the Yeoman Generator Search to find an npm module boilerplate.
- Select a suitable generator based on your preferred technology stack.
Unpublishing an NPM package: An npm package can be unpublished within 72 hours of its initial release using npm unpublish <package-name> and after 72 hours removal requires contacting the npm registry support.
npm unpublish packageNameExample: Use the published package to add two numbers.Â
Filename: app.jsÂ
import GFGFns from 'gfgnpmpkgupload';
console.log(GFGFns.add(4, 5));
Output:
