Oct 05, 2024

How to Build and Publish NPM Package step-by-step.

Step 1: Setup Your Project

  1. Create a Directory:
    Start by creating a directory for your project:

    mkdir my-npm-package
    cd my-npm-package
  2. Initialize the Package:
    Initialize the project by creating a package.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

  1. Write Your Code:
    Create the main file for your package, for example, index.js:

    // index.jsfunction sayHello(name) {
    return `Hello, ${name}!`;
    }

    module.exports = { sayHello };
  2. Add TypeScript Support (Optional):
    If you’re using TypeScript, set it up:

    npm install typescript --save-dev
    npx tsc --init

    Write your TypeScript code and compile it before publishing.

  3. Test Your Code:
    Create a test.js file to test your package before publishing:

    const { sayHello } = require('./index');
    console.log(sayHello('World')); // Output: Hello, World!

Step 3: Prepare for Publishing

  1. Add .gitignore and .npmignore:
    Ensure that unnecessary files are not included in your package:

    • .gitignore (for GitHub)
    • .npmignore (for npm)
  2. Versioning:
    Ensure your package.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

  1. Login to NPM:
    If you don’t have an NPM account, create one here. Then log in:

    npm login
  2. 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").

  3. 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

  1. Install the Package Globally or Locally:

    npm install my-npm-package
  2. 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:

  1. Update the Version:
    Update the version field in package.json to the new version number (e.g., "version": "1.0.1").

  2. 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

Use npm scripts to Simplify Daily Tasks

Running long terminal commands over and over? Automate them with simple npm scripts in your package.json.

Read more →

Learn Faster with Browser DevTools Tricks

Your browser is more powerful than you think. Chrome/Edge/Firefox DevTools can save you tons of time debugging and experimenting directly in the browser.

Read more →

Fix "Email in this signature doesn't match the committer email" on GitHub

Pushed a signed GPG commit only to get a warning that the signature is unverified because of an email mismatch? Here is how to sync your GPG identity with Git.

Read more →

How to Set Up Verified GPG Commit Signing on GitHub

Tired of seeing the "Unverified" badge on your GitHub commits? Setting up a GPG key signs your commits cryptographically, proving to your team (and future employers) that the code actually came from you.

Read more →