@tops_praful/my-npm-package
v1.0.1
Published
This guide explains how to create, test, and publish an NPM package. Follow these steps to get your package up and running.
Downloads
1
Readme
Creating and Deploying an NPM Package
This guide explains how to create, test, and publish an NPM package. Follow these steps to get your package up and running.
Step 1: Set Up Your Environment
Install Node.js: Download and install Node.js from Node.js official website. NPM is included with Node.js.
Log in to NPM:
npm loginProvide your username, password, and email associated with your NPM account. If you don't have an account, create one on npmjs.com.
Step 2: Create Your Package
Create a New Folder:
mkdir my-npm-package cd my-npm-packageInitialize Your Package:
npm initFollow the prompts to fill out
package.json. Use-yto skip prompts and use default values:npm init -yWrite Your Code: Create a main file for your package (e.g.,
index.js):// index.js module.exports = function greet(name) { return `Hello, ${name}!`; };
Step 3: Prepare for Publishing
Add a
.gitignoreFile: Create a.gitignorefile to exclude unnecessary files:node_modules/ .envTest Your Package Locally: Link your package to test it before publishing:
npm linkIn another project, link your package:
npm link my-npm-packageVersioning: Update the version of your package using semantic versioning:
npm version [patch|minor|major]patch: For bug fixes (e.g.,1.0.0->1.0.1)minor: For new features (e.g.,1.0.0->1.1.0)major: For breaking changes (e.g.,1.0.0->2.0.0)
Step 4: Publish Your Package
Check Package Name Availability:
npm search <package-name>Publish: Run the following command to publish your package:
npm publishFor scoped packages (e.g.,
@yourname/mypackage), publish with:npm publish --access public
Step 5: Use Your Package
Install your package in another project:
npm install my-npm-packageImport and use it in your code:
const greet = require('my-npm-package'); console.log(greet('World')); // Output: Hello, World!
Additional Tips
Automated Testing: Use testing libraries like Jest or Mocha to ensure your package works correctly.
Documentation: Include a
README.mdfile with instructions for users to understand and use your package.Updating Your Package: After making changes, update the version:
npm version [patch|minor|major]Then, publish again:
npm publish
