- What is NPM
- Creating a package
- Package.json
- Installing a package
- Package-lock.json
When you write code , you will often need to use code written by other people; for example, libraries to help you work with dates, colors, servers, or almost anything else.
Instead of downloading and copying files manually, you can use a package manager.
A package manager is a tool that:
- downloads packages
- keeps track of which packages your project needs
- makes sure everyone on your team has the same versions of the packages
What is NPM
In the NodeJS world, the most popular package manager is NPM, which stands for Node Package Manager.
You get NPM automatically when you install NodeJS.
You can check if you have NPM by running this in your terminal:
npm -v
This prints the version of NPM you have, like:
10.2.1
Creating a package
In NodeJS, a package is just a directory with a
package.json file in it.Let’s create one step by step.
-
Make a new folder for your project:
mkdir my_project cd my_project -
Run this command:
npm init
This starts an interactive setup, asking you questions like the name of your project, version, description, etc.
If you don’t want to answer everything and just accept the defaults, you can use:
npm init -y
After running it, you will see a new file in your folder called:
package.json
package.json
The
package.json file is just a JSON file that stores metadata about your project.Here’s an example:
{ "name": "my_project", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "", "license": "ISC", "type": "commonjs" }
A few important fields:
name: the name of your packageversion: the current versionmain: the entry point file (likeindex.js)scripts: commands you can run (likenpm start)dependencies: lists all the packages your project depends on
Installing a package
Let’s say you want to use a certain package called
picocolors to add colors to your terminal output.You can install it by running:
npm install picocolors
You can now use it in your project. Make a
index.js file withconst pico = require('picocolors') console.log( pico.green("This text is green!") )
and try running it. The terminal should print a colored version of the text.
What did NPM do ?
It downloaded the package and stored it in a subfolder called
node_modules/- it added an entry under
dependenciesin yourpackage.json - it updated the
package-lock.jsonfile
What is
package-lock.json ?package-lock.json
This file is automatically created by NPM.
You might wonder, if we already have
package.json, why do we need another file?
Here is the reason:-
package.jsonjust says which version range of a package your project needs. Example:"dependencies": { "picocolors": "^1.1.0" }The^1.1.0means “any version that is compatible with 1.1.x”, so it’s flexible.
package-lock.json freezes the exact versions of every single package and their sub-dependencies, so that everyone who installs your project gets the exact same working setup.Why is this important?
If you work on a team, or you deploy your project to a server, or you come back to it in the future, you want to make sure it still works the same way.
If the packages have been updated and you reinstall without a lock file, you might get a slightly different version that behaves differently.
By keeping the
package-lock.json in your project, NPM will always install the exact versions listed there, ensuring that everyone has the same environment.package-lock.json locks everything to a very specific version, to make the project more reproducible on other machines.But if your package needs to be used by many people, you might instead choose not to commit it, so that NPM only finds the
package.json file and it's allowed to install automatically the latest versions that are allowed in that file.But these are things you should worry about later, once you start publishing your own code. For now, we introduced the basics of NPM just to allow you to find and use the libraries published by other developers in your projects.