- Web Development with MongoDB and Node.js
- Jason Krol
- 587字
- 2021-08-05 17:51:05
Installing modules using npm
The module system in Node is so powerful that consuming a third-party module written by other developers is a piece of cake. Node includes its own package manager called npm, which is a registry that currently contains over 60,000 unique modules written in Node. These modules are completely open source and available to you via a few short commands. In addition, you can release your own personal modules via npm and allow anyone in the world to use your feature!
Let's say you wanted to include the popular web framework Express in your project (the one we will be using later in this book). There are simply two steps required to download a module and use it in your code:
$ npm install express // ** file: usingnpm.js var express = require('express);
And that's it! Literally, it's that simple! From the command line of the folder where your project is located, simply execute npm install package-name
, and the package will be downloaded from npm and stored in a folder called node_modules
within your project. If you browsed to the node_modules
folder, you will find a folder for the package you installed, and within that folder, you will find the raw source code for the package itself. Once the package is downloaded, it's as simple as using require()
from within your code.
There may be times when you want to install a Node package globally, for example, when using a popular command-line build tool called Grunt.js. To install an npm package globally, simply include the -g
or --global
flag, and the module will be installed as a global executable instead. When installing npm packages globally, the source files for the package are not stored within the node_modules
folder of a specific project, but instead within a node_modules
folder in a system directory of your machine.
A really powerful feature of npm is that it allows for a quick, easy, and consistent way for other developers to boot up your code in their local environment. Node projects typically include a special file called package.json
that includes information about the project as well as a list of all npm packages that the project depends on. A developer with a copy of your local code can simply execute npm install
to have every dependency downloaded and installed locally using this file.
The npm install flag --save
or --save-dev
is required if you want the dependency you are installing to be saved to the package.json
file. If you are starting a new project and don't want to create a package.json
file by hand, you can simply execute npm init
and answer a few quick questions to get a default package.json
file quickly set up. You can leave every question blank during init
and accept the default values if you want:
$ npm init $ npm install express --save $ npm install grunt --save-dev $ cat package.json { "name": "chapter3", "version": "0.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "", "license": "ISC", "dependencies": { "express": "^3.5.1" }, "devDependencies": { "grunt": "^0.4.4" } }
Note that the dependencies
and devDependencies
sections have express
and grunt
listed. The difference between these two sections is that the dependencies
section is absolutely critical for the app to function properly. The devDependencies
section has only packages that need to be installed for a developer to use during the development of the project (such as Grunt for various build steps, testing frameworks, and so on).
- Windows Vista基礎(chǔ)與應(yīng)用精品教程
- Cybersecurity:Attack and Defense Strategies
- 鴻蒙生態(tài):開啟萬物互聯(lián)的智慧新時代
- Linux內(nèi)核完全注釋(20周年版·第2版)
- 精通Linux內(nèi)核開發(fā)
- macOS效率手冊
- Kali Linux高級滲透測試(原書第3版)
- 分布式系統(tǒng)設(shè)計實踐
- CentOS 6 Linux Server Cookbook
- Heroku Cloud Application Development
- Linux網(wǎng)絡(luò)操作系統(tǒng)項目教程(RHEL 7.4/CentOS 7.4)(第3版)(微課版)
- Docker容器技術(shù)與應(yīng)用
- Learn Quantum Computing with Python and IBM Quantum Experience
- Java EE 8 High Performance
- Linux深度攻略