- JavaScript:Moving to ES2015
- Ved Antani Simon Timms Narayan Prusty
- 573字
- 2021-07-09 19:07:44
npm
The npm is the package manager used by Node to distribute modules. The npm can be used to install, update, and manage modules. Package managers are popular in other languages such as Python. The npm automatically resolves and updates dependencies for a package and hence makes your life easy.
Installing packages
There are two ways to install npm packages: locally or globally. If you want to use the module's functionality only for a specific Node project, you can install it locally relative to the project, which is default behavior of npm install
. Alternatively, there are several modules that you can use as a command-line tool; in this case, you can install them globally:
npm install request
The install
directive with npm
will install a particular module—request
in this case. To confirm that npm install
worked correctly, check to see whether a node_modules
directory exists and verify that it contains a directory for the package(s) that you installed.
As you start adding modules to your project, it becomes difficult to manage the version/dependency of each module. The best way to manage locally installed packages is to create a package.json
file in your project.
A package.json
file can help you in the following ways:
- Defining versions of each module that you want to install. There are times when your project depends on a specific version of a module. In this case, your
package.json
helps you download and maintain the correct version dependency. - Serving as a documentation of all the modules that your project needs.
- Deploying and packaging your application without worrying about managing dependencies every time you deploy the code.
You can create package.json
by issuing the following command:
npm init
After answering basic questions about your project, a blank package.json
is created with content similar to the following:
{ "name": "chapter9", "version": "1.0.0", "description": "chapter9 sample project", "main": "app.js", "dependencies": { "request": "^2.65.0" }, "devDependencies": {}, "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [ "Chapter9", "sample", "project" ], "author": "Ved Antani", "license": "MIT" }
You can manually edit this file in a text editor. An important part of this file is the dependencies
tag. To specify the packages that your project depends on, you need to list the packages you'd like to use in your package.json
file. There are two types of packages that you can list:
dependencies
: These packages are required by your application in productiondevDependencies
: These packages are needed only for development and testing (for example, using the Jasmine node package)
In the preceding example, you can see the following dependency:
"dependencies": { "request": "^2.65.0" },
This means that the project is dependent on the request
module.
Note
The version of the module is dependent on the semantic versioning rules—https://docs.npmjs.com/getting-started/semantic-versioning.
Once your package.json
file is ready, you can simply use the npm install
command to install all the modules for your projects automatically.
There is a cool trick that I love to use. While installing modules from the command line, we can add the --save
flag to add that module's dependency to the package.json
file automatically:
npm install async --save npm WARN package.json chapter9@1.0.0 No repository field. npm WARN package.json chapter9@1.0.0 No README data async@1.5.0 node_modules/async
In the preceding command, we installed the async
module with the normal npm
command with a --save
flag. There is a corresponding entry automatically created in package.json
:
"dependencies": { "async": "^1.5.0", "request": "^2.65.0" },
- Kali Linux Web Penetration Testing Cookbook
- Apache ZooKeeper Essentials
- Practical Windows Forensics
- MySQL數(shù)據(jù)庫(kù)管理與開(kāi)發(fā)(慕課版)
- Symfony2 Essentials
- 快速入門(mén)與進(jìn)階:Creo 4·0全實(shí)例精講
- Django 3.0應(yīng)用開(kāi)發(fā)詳解
- Building Dynamics CRM 2015 Dashboards with Power BI
- Mastering Gephi Network Visualization
- .NET 4.0面向?qū)ο缶幊搪劊簯?yīng)用篇
- Python預(yù)測(cè)分析實(shí)戰(zhàn)
- SQL Server 2012 數(shù)據(jù)庫(kù)應(yīng)用教程(第3版)
- 每個(gè)人的Python:數(shù)學(xué)、算法和游戲編程訓(xùn)練營(yíng)
- Design Patterns and Best Practices in Java
- C#多線(xiàn)程編程實(shí)戰(zhàn)