How to make a Node.js package

Introduction - What is NPM?

NPM stands for Node Package Manager , and it is a package manager for JavaScript that is primarily used to install, manage, and distribute packages or modules of code. It is an essential tool for JavaScript developers, particularly those working with Node.js, as it simplifies the process of adding and updating libraries and code in your projects. It comes by default with Node.js.

An NPM package is a module of code or collection of predefined functions that can easily be distributed for other developers to use in their own projects. It is also reusable.


Making your first NPM package

Requirements

You will need the following requirements to start creating a package:

  • Node.js: Ensure you have Node.js and npm installed on your development machine. You can download and install Node.js, from the official website: nodejs.org.
  • Text Editor or IDE: You’ll need a code editor or integrated development environment (IDE) to write and edit your package’s code. Preferably you might want to use Visual Studio Code (code.visualstudio.com), as it is used in this tutorial
  • NPM Account: To publish packages on the public NPM registry, you’ll need an NPM account. You can create one here: www.npmjs.com/signup
  • Basic knowledge on Node.js and the VS Code terminal is recommended

Initialising a project

Start by creating a folder for your project. Set the name to the preferred name of your package. I’m setting it to ‘hello-world’.

Then open this folder in Visual Studio Code and create a new terminal.

Your terminal should look like this:

Next, you will be creating a file which will tell NPM all the details of your package and link all the JavaScript files. To do this, run the following command in the terminal:

npm init

You will be prompted to enter details about your package like name, description, etc. If you are happy with the defaults it provides, then you can just press enter without entering anything to move on to the next parameter.

(Note: For the test command, git repository and keywords options, you do not have to provide anything, you can leave it blank if you wish to.)

After giving all the details, you will get a confirmation asking if the package details are fine. If they are, click Enter.

You will now see a new file called package.json in your project folder. It contains all the details you provided just now for your package. If you open it, it should look something similar to this:

{
  "name": "hello-world",
  "version": "1.0.0",
  "description": "Hello World!",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "SnakeByte",
  "license": "ISC"
}

Adding code

Create a file called ‘index.js’. This file will contain all the code and predefined functions in your package.

In my index.js file, I added the following code:

// index.js

function helloWorld() {
  console.log("Hello World!");
}

function myPackage() {
  console.log("This is my first NPM package!");
}

module.exports = {
  helloWorld,
  myPackage
};

In the above code, we define two functions, called helloWorld and myPackage, that log a message to the console. Then to make these functions usable outside this index.js file, we ‘export’ them.

You can add your own code and functions if you want. Just remember to add them to the module.exports list!

Testing the package

Testing is an important part of the development process. A developer must ensure that there are no bugs in the code before publishing it.

To test your package, you must make the package globally available so you can use it in another node.js test project. To do this, run:

npm link

Then create a new folder called test in the same parent directory of the package directory and initialise a node.js project in it. The file tree should now look something like this:

hello-world/          <-- Hello world package folder
  ├── index.js
  ├── package.json
  
test/                 <-- Test project folder
  ├── app.js
  ├── package.json

Then type the following to use the package in this test project:

npm link hello-world

Now we can import the package into our test project to see it everything works as normal. Add the following to the app.js file.

const { helloWorld, myPackage } = require("hello-world");

helloWorld();
myPackage();

Then run the test project:

node app.js

If the package works successfully, you should get the output on the console!


Publishing your package

Now you’ve reached the final stage of developing the package: publishing it!

Before you publish your package, make sure you are logged in to npm on your computer. To do this, run the following in the console:

npm login

After logging in, you must move the terminal from the test project directory back to the package directory. Run the following commands:

cd ..
cd hello-world

After you are back in the package directory, check everything and make sure it’s ready for publishing. Check that the name of your package is unique so that it can publish successfully. You can find other publishing guildines here.

And to publish, simply run:

npm publish

Done! You have now successfully made and published your first NPM package! :tada:

Full credit to SnakeByte for posting this in my blog, reposting it here!

4 Likes

Nice but I would ENCOURAGE ALL USERS not to publish just a hello world package.

hi chatgpt

Creating a Node.js package can be an exciting endeavor, and fortunately, the process is well-documented and straightforward. Here’s a simplified guide to help you get started:

  1. Initialize Your Project: Use npm init in your terminal to create a package.json file. Follow the prompts to provide information about your package, such as the name, version, and entry point.
  2. Write Your Code: Develop the functionality you want for your package. Ensure your code is modular and adheres to best practices for Node.js development.
  3. Create a Package Entry Point: Specify the main entry point for your package in your package.json file. This is the file that users will import when they use your package.
  4. Define Dependencies: If your package relies on external libraries, list them in the dependencies section of your package.json. Users can then install these dependencies when they install your package.
  5. Test Your Package: Write tests for your package to ensure its reliability. Use a testing framework like Jest and include a test script in your package.json to run the tests easily.
  6. Publish Your Package: Use the npm publish command to publish your package to the npm registry. Make sure you have an account on the npm website and are logged in.
  7. Versioning: Follow semantic versioning (SemVer) principles when updating your package. Increment the version number appropriately based on the changes you make.

Remember to document your package thoroughly, providing clear instructions on installation, usage, and any configuration options.

Best of luck with your Node.js package creation journey!

@gautam bot

@gautam another god damn bot bro what the fu-

Crazy amount of text :o

2 Likes

backlink bots again r u kidding me.

Ima just delete the link part and keep the rest