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!
Full credit to SnakeByte for posting this in my blog, reposting it here!