美文网首页
3-使用package.json

3-使用package.json

作者: Aries_苏世 | 来源:发表于2016-07-04 14:50 被阅读0次

1.package.json是什么

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

2.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)]

3.创建一个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 "wombat@npmjs.com"
> npm set init.author.name "ag_dubs"
> npm set init.license "MIT"

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

4.指定依赖包

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

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

在"devDependencies"中添加一项的命令:

npm install <package_name> --save-dev

相关文章

网友评论

      本文标题:3-使用package.json

      本文链接:https://www.haomeiwen.com/subject/wcbcjttx.html