Many program languages have some sort of package manager associated with them. These package managers help developers when they need to install external libraries and tools for building servers, testing code, and much more. Node’s de facto package manager is NPM.
Node Package Manager (NPM) is a command line tool that installs, updates or uninstalls Node.js packages in your application . It is also an online repository for open-source Node.js packages. The node community around the world creates useful modules and publishes them as packages in this repository.
NPM is included with Node.js installation. After you install Node.js, verify NPM installation by writing the following command in terminal or command prompt.
npm -v
If you have an older version of NPM then you can update it to the latest version using the following command.
npm install npm -g
To access NPM help, write npm help in the command prompt or terminal window.
npm help
NPM performs the operation in two modes: global and local.
From StackOverFlowThe main goal, as you touched upon, is automated dependency and package management. This means that you can specify all of your project’s dependencies inside your package.json file, then any time you (or anyone else) needs to get started with your project they can just run npm install and immediately have all of the dependencies installed.
On top of this, it is also possible to specify what versions your project depends upon to prevent updates from breaking your project.
Being able to access a library of code that has already been used will allow you to implement your idea much faster. Also, having the dependencies managed by npm makes it much easier to share your code.
It is definitely possible to manually download your libraries, copy them into the correct directories, and use them that way. However, as your project (and list of dependencies) grows, this will quickly become time-consuming and messy. It also makes collaborating and sharing your project that much more difficult.
When starting a new project, in which you will be using npm to manage your modules, you’ll need to execute
npm init
This command will walk you through initializing a project. The interactive prompt will ask a few questions about what you are working on. After you’ve finished the process of initializing your project using the Node Package Manager, node.js created a package.json file in your project’s root direcotry similar to this one:
{
"name": "project-name",
"version": "0.0.1",
"description": "Project Description",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "the repositories url"
},
"author": "your name",
"license": "N/A"
}
This file will be updated when adding further dependencies during the development process which means when you install any package, it will be listed in the package.json file.
You can generate the package.json without having it ask any questions by running this command :
npm init -y
Pro tip: You will want to create a package.json wherever you are creating a new project. If you’re unsure where it should go, think of it like git — wherever you run git init, you likely want to run npm init.
Packages, or libraries, are bits of code that are available for reuse.JS Packages & Dependencies
It is a JSON file that lives in the root directory of your project. Your package.json holds important information about the project. It contains human-readable metadata about the project (like the project name and description) as well as functional metadata like the package version number and a list of dependencies required by the application. For more details check :
Once you have a package.json, it’s time for you to install some packages. There are a few different ways you can install, and knowing the difference between them can be one of the determining factors for deploying or sharing a project easily!
Use the following command to install any third party module in your local Node.js project folder.
For example, the following command will install ExpressJS into MyNodeProj folder.
if you look in the root of your project folders, you’ll notice a new folder named node_modules. This is where the code for express was added. All the modules installed using NPM are installed under this node_modules folder.
Add Dependency into package.json:
Use npm install <module_name> --save
, the –save argument will add dependency entry into package.json of your application.
For example, the following command will install ExpressJS in your application and also adds dependency entry into the package.json.
After installing express, the package.json file looks like this:
{
"name": "project-name",
"version": "0.0.1",
"description": "Project Description",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "the repositories url"
},
"author": "your name",
"license": "N/A",
"dependencies": {
"express": "^4.17.1"
}
}
Notice the addition of the dependencies. All the packages that are installed using the –save flag, will be added there.
NPM can also install packages globally so that all the node.js application on that computer can import and use the installed packages. NPM installs global packages into /<User>/local/lib/node_modules
folder.
Apply -g
in the install command to install package globally. For example, the following command will install ExpressJS globally.
To update the package installed locally in your Node.js project, navigate the command prompt or terminal window path to the project folder and write the following update command.
The following command will update the existing ExpressJS module to the latest version.
Use the following command to remove a local package from your project.
The following command will uninstall ExpressJS from the application.
Development dependencies are intended as development-only packages, that are unneeded in production(when an application is deployed). For example testing packages, webpack or Babel.
When you add the -D
flag, or --save-dev
, you are installing it as a development dependency.which adds it to the devDependencies list.
Mocha is a testing framework.
To install Mocha as a development dependency, run the following command npm install –save-dev chai. After the installation, the package.json looks as follows:
{
"name": "npm-test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.17.1"
},
"devDependencies": {
"chai": "^2.4.5"
}
}
Notice the new section, devDependencies in your package.json file. All packages installed with the –save-dev flag will be added there.
As mentioned before, the node_modules/ folder is the folder where all packages required by your JavaScript project are downloaded and installed. This folder is commonly excluded from a remote repository because it has a large size and you shouldn’t add code that you didn’t write to the repository.
Rather than including the node_modules/ folder, you should have the packages required by your project listed in package.json file-When someone else clones your repository, they will simply need to run npm install and it will install all the packages that are a part of your package.json- and ignore the node_modules/ folder using the .gitignore file. you need to create the .gitignore file in your project root folder and add the following line to it:
node_modules/
This will ignore node_modules/ and disallow it from being pushed up.
In this section you can find a lot of helpful links to other content. This is a supplemental material for you if you want to dive deeper into some concepts.
videos: