npm 的 ‘Maven‘ 行为 | packages.json

要知道项目中各处出现的package.json文件是什么?

package.json是用来管理本地依赖包的一个文件。使用package.json可以带来很多好处。
它可以作为你项目依赖包的一个说明文档。
它用来指定你项目中所使用依赖包的版本号。
它可以让你更方便地和其他开发者来分享你的项目。

package.json的组成

package.json文件中必须包含的内容有:
 

1."name"
1)all lowercase
2)one word, no spaces
3)dashes and underscores allowed

2."version"
1)in the form of x.x.x
2)follows (semver spec)[[https://docs.npmjs.com/getting-started/semantic-versioning](https://docs.npmjs.com/getting-started/semantic-versioning)]


创建一个package.json

使用npm提供的初始化工具可以方便地创建一个package.json文件

npm init


然后根据提示填写对应项目。

name: defaults to author name unless in a git directory, in which case it will be the name of the repository
version: always 1.0.0
main: always index.js
scripts: by default creates a empty test script
keywords: empty
author: whatever you provided the CLI
license: ISC
repository: will pull in info from the current directory, if present
bugs: will pull in info from the current directory, if present
homepage: will pull in info from the current directory, if present

 


也可以用初始化命令行来对一些参数进行初始化

> npm set init.author.email "d@gmail.com"
> npm set init.author.name "s"
> npm set init.license "BAC"

 

 


备注:如果项目中的package.json文件中没有任何内容,那么npm将会使用README.md中的第一行来代替。package.json文件的描述可以方便别人查阅你的项目所依赖的packages,并且极大地方便你对自己项目的管理。

指定依赖包

为了指定你在项目中应用到的依赖包,你需要在packages.json文件中将它们列出来,有以下两类依赖包需要被列出。

"dependencies":这里列出你产品的application中需要用到的依赖包;
"devDependencies":这里列出在开发和测试中所用到的依赖包。
这两个属性可以在package.json文件手动进行编辑,也可以在安装这些包的时候用命令行参数自动将她们添加在文件中。
在"dependencies"中添加一项的命令:

npm install <package_name> --save
在"devDependencies"中添加一项的命令:
npm install <package_name> --save-dev

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值