Node.js后端开发 - 基础篇 #17 package.json 文件

文章目录

前言

package.json文件是什么?

package.json文件是怎么生成的?

npm init 命令

dependencies 字段( npm install --save express )

devDependencies 字段( npm install --save-dev gulp )

scripts 字段

npm run start 命令

npm install 命令


前言

上一篇文章我们介绍了nodejs的包管理器NPM、web框架express、以及国内的一些镜像文件,具体可参考: Node.js后端开发 - 基础篇 #16 包管理器 NPM  这篇文章我们将介绍nodejs的package.json 文件,那么package.json 文件是什么呢?

package.json文件是什么?

上篇文章我们已经讲了包管理器NPM,我们写一个项目可能会用到很多很多的包,这些包可能是一些开源的框架或是一些工具之类的,我们每使用一个包就会把它给安装下来,然后它会放到一个目录中就是node_modules目录。

那么我们一个项目可能很大,那么这些包就会很多,有时候我们安装过的包,我们都有可能忘记,自己其实已经安装过了,我们的项目这么大,有没有一种机制来记录我们安装过那些包呢?

这时候nodejs提供了一种机制叫做package.json,它是一个文件,它可以记录所有安装过的包的名称、还有版本信息,这样你把自己当前项目迁移到别的地方去的时候,你也能够知道它到底安装了什么包。

因为node_modules目录我们一般是不会放到svn、git版本控制中的,因为这个目录的内容一般比较大,它又不是源码的一部分,我们只要知道安装过的包的名字就可以了,我们根本不需要node_modules目录里面的内容。

例如,你现在有一个同事,得到你当前开发的项目代码,它是不需要node_modules目录下的内容的,它只需要这些安装过的包名称就可以了,那他拿到项目代码之后,直接在他的电脑中安装就可以了,因为项目代码中有package.json文件,它记录了所有安装过的包的名称、还有版本信息。那么package.json文件,又是怎么生成的呢?

package.json文件是怎么生成的?

npm init 命令

npm提供了一个命令叫npm init,npm init 其实这个命令是用来初始化一个项目的,如果你写nodejs项目的话,你可能首先会运行这个命令来初始化这个项目,有时候你写一些前端的应用项目,也会用到这个命令。

现在我们在终端输入命令看看效果,它首先会问一些问题,让你回答一些信息,如:包名、版本号、描述信息、启动开始的文件、测试的命令、git仓库的地址、关键字、作者、协议等,我们都回车就行了,这些信息我们都是可以改的,如下:

$ npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.

See `npm help json` for definitive documentation on these fields
and exactly what they do.

Use `npm install ` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
package name: (hello-nodejs) 
version: (1.0.0) 
description: 
entry point: (app.js) 
test command: 
git repository: 
keywords: 
author: 
license: (ISC) 
About to write to /Users/luminal/Desktop/nodejs/hello-nodejs/package.json:

{
  "name": "hello-nodejs",
  "version": "1.0.0",
  "description": "hello world",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node server.js"
  },
  "author": "",
  "license": "ISC"
}


Is this OK? (yes) 

它现在自动生成了一个文件package.json,我们打开看看效果:

{
  "name": "hello-nodejs",
  "version": "1.0.0",
  "description": "hello world",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node server.js"
  },
  "author": "",
  "license": "ISC"
}

dependencies 字段( npm install --save express )

现在我们项目要用到web框架express,我们执行命令:npm install --save express,如下

$ npm install --save express
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN [email protected] No repository field.

+ [email protected]
added 50 packages from 37 contributors and audited 126 packages in 42.189s
found 0 vulnerabilities

执行上面命令以后,这时候package.json文件变成,如下:

{
  "name": "hello-nodejs",
  "version": "1.0.0",
  "description": "hello world",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node server.js"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.17.1"
  }
}

即多了dependencies字段:它是用来记录所有安装包的一些信息,dependencies字段里面是express安装包的名称、版本信息。

devDependencies 字段( npm install --save-dev gulp 

我们再来执行命令:npm install --save-dev gulp,如下

$ npm install --save-dev gulp

> [email protected] install /Users/luminal/Desktop/nodejs/hello-nodejs/node_modules/fsevents
> node install

node-pre-gyp WARN Using needle for node-pre-gyp https download 
[fsevents] Success: "/Users/luminal/Desktop/nodejs/hello-nodejs/node_modules/fsevents/lib/binding/Release/node-v64-darwin-x64/fse.node" is installed via remote
npm WARN [email protected] No repository field.

+ [email protected]
added 378 packages from 238 contributors and audited 6616 packages in 185.343s
found 0 vulnerabilities

执行上面命令以后,这时候我们再来看看package.json文件变成,如下:

{
  "name": "hello-nodejs",
  "version": "1.0.0",
  "description": "hello world",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node server.js"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.17.1"
  },
  "devDependencies": {
    "gulp": "^4.0.2"
  }
}

即多了devDependencies字段:就是开发环境依赖的一些包,它会把这些包信息记录在devDependencies字段里面。

scripts 字段

npm run start 命令

package.json文件里面的内容,我们都是可以重新修改的!我们来运行一个项目启动的命令:npm run start

$ npm run start

> [email protected] start /Users/luminal/Desktop/nodejs/hello-nodejs
> node server.js

我们查看package.json文件,npm run start 对应的就是scripts字段里面的 "start": "node server.js",我们可以把 node server.js  修改为 node app.js ,也就是修改了我们启动的入口文件,如下:

$ npm run start

> [email protected] start /Users/luminal/Desktop/nodejs/hello-nodejs
> node app.js

其实scripts字段里面就是一些脚本,这时候启动的入口文件就是aap.js了。这个有什么好处呢?

你新来的同事有可能把你的代码下载下来之后,他根本不知道怎么启动这个项目,那你把这些脚本启动的命令放到scripts字段里面,这时候就非常清晰、一目了然!因为启动目录的方式有很多种,入口文件也未必叫aap.js,如果你不写明白,人家都不知道你的人口文件是那个。

npm install 命令

package.json文件记录所有安装过的包的名称、还有版本信息,那么另一个人得到这些文件,怎么把这些安装包给下载下来、安装到项目中呢?我们可以执行 npm install 命令,它就会把 package.json文件记录的包给安装下来!

你可能感兴趣的:(nodejs后端开发,node.js,json,npm)