Downloading and Installing Local Packages:
Installing Unscoped Packages:
Unscoped packages are public packages. They can be downloaded and installed by everyone.
Command-line:
npm install <package_name>
This command will create a ‘node_modules’ directory if it does not exist already and download the package to that directory.
If the user needs a different version than the latest one, use the following command:
npm install <package_name>@<version_number>
Note: npm installs the latest version of the package that satisfies the semver rule declared in package.json. If the package.json file is not present in the local directory, the latest version of the package is installed.
Installing Scoped Public Packages:
Scoped public packages are public packages with scope. This means that anyone can install them as long as the scope name is referenced during the installation.
Command-line:
npm install @scope/package-name
Installing Private Packages:
Private packages can only be installed by those who have read access to the package. Scope name has to be referenced during installation, as the private packages are always scoped. .
Command-line:
npm install @scope/private-package-name
Global Packages:
Global packages are stored in a particular place in the user’s computer regardless of where the installation command is executed.
Packages should be installed globally when it provides an executable command that the user can execute from the shell, and it can be reused among different projects.
The global installation allows the code in the package to be used as a set of tools in the computer.
Downloading and Installing Global Packages:
To download and install a package globally, run the following command.
npm install -g <package_name>
Checking npm local and global packages:
After installation, we have to check if the package has been installed locally or globally.
To check the locally installed packages and their dependencies, go to the project folder in the terminal and run the command:
npm list
To view the list of packages at the top level of the tree:
npm list --depth==0
To check for a specific package:
npm list <package_name>
To check if a package has been globally installed, go to the project folder in the terminal and run the command:
npm list -g
The above command prints all the globally installed packages.
To view the list of packages at the top level of the tree:
npm list -g --depth==0
To check for a specific package:
npm list -g <package_name>
Frequently Asked Questions:
1. What does a local machine mean?
Ans-> The computer that the user currently uses is termed a local machine. All the stuff that the user downloads locally is kept in the local machine.
2. What does working locally mean?
Ans-> Working locally means that the programs are executed in the machine used by the user currently.
3. How can we install all the required packages at once?
Ans-> We can install all the required packages by using the following command:
npm install
4. What is the npm registry?
Ans-> It is a public collection of packages of open-source code of node.js. Npm is configured to use npm public registry at https://registry.npmjs.org by default.
5. How do you know that the package has been installed?
Ans-> Check that the ‘node_modules’ directory exists and contains the directory for the package installed.
ls node_modules
Key Takeaways:
In this blog, we covered npm local and global packages in detail. We discussed the installation steps of npm local and global packages, their usage, and methods to check if the package installation is local or global.
Readers, if you liked this blog, you can learn more about Node.js from here.
Recommended Readings:
NPX vs NPM
wget command in linux
If you are preparing for your next web development interview, check out the blogs, 25 CSS Interview Questions, and 30 JavaScript interview questions.