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

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 →

My Favorite VS Code Shortcuts

I use some simple VS Code shortcuts to speed up my work.

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 →