package.json文件快速入門詳解
写在前面
相信入门nodejs或者npm的同学会对package.json这个文件有疑惑,对这个文件的作用不是很清晰,但搭建自己的博客每每用到node,npm这个文件又必不可少。
npm在package.json文件中管理项目的依赖项以及项目的元数据。
node执行js中require的时候,也会根据package.json中的依赖项查找。
package.json在npm中用的比较多,我的github博客就是基于npm管理搭建的,在项目根目录下有package.json这个文件,如图
package.json
package.json是什么
每个项目的根目录下面,一般都有一个package.json文件,定义了这个项目所需要的各种模块,以及项目的配置信息(比如名称、版本、许可证等元数据)。npm install命令根据这个配置文件,自动下载所需的模块,也就是配置项目所需的运行和开发环境。
下面是一个最简单的package.json文件,只定义两项元数据:项目名称和项目版本。
{ "name" : "xxx", "version" : "0.0.0",
}package.json文件就是一个JSON对象,该对象的每一个成员就是当前项目的一项设置。比如name就是项目名称,version是版本(遵守“大版本.次要版本.小版本”的格式)。
package.json配置说明
下面就以我的博客项目的package.json文件的配置作一个简单的说明,分为必须字段和可选字段
{ "name": "wblearn-blog", "title": "Wblearn Blog", "author": "Wblearn <[email protected]>", "version": "1.7.0", "homepage": "https://wblearn.github.io", "repository": { "type": "git", "url": "https://github.com/wblearn/wblearn.github.io"
}, "bugs": "https://github.com/wblearn/wblearn.github.io/issues", "devDependencies": { "grunt": "~0.4.5", "grunt-contrib-less": "~0.11.4", "grunt-contrib-watch": "~0.6.1", "grunt-banner": "~0.2.3", "grunt-contrib-uglify": "~0.5.1"
}, "scripts": { "preview": "cd _site; python -m SimpleHTTPServer 8020", "py3view": "cd _site; python3 -m http.server 8020", "watch" : "grunt watch & npm run preview & jekyll serve -w", "py3wa" : "grunt watch & npm run py3view & jekyll serve -w", "boil" : "git push boilerplate boilerplate:master", "push" : "git push origin master --tag", "cafe" : "git co gitcafe-pages; git merge master; git push gitcafe gitcafe-pages:gitcafe-pages --tag; git co master;"
}
}必须字段
1.Name
项目名称
全部小写,没有空格,可以使用下划线或者横线
2.Version
项目版本号
x.x.x 的格式
符合“语义化版本规则”
可选字段
1.title
标题
2.author
author是一个人,contributors是一组人。格式设置如下:
{ “name” : “wblearn”
, “email” : “[email protected]”
, “url” : “[https://wblearn.github.io](https://wblearn.github.io)” }也可以像我博客中的格式一样:
"author": "Wblearn <[email protected]>",
3.homepage
项目url主页
4.repository
用于指示代码存放的位置。
"repository": { "type": "git", "url": "https://github.com/wblearn/wblearn.github.io"
}>"repository": { "type": "svn", "url": "https://github.com/wblearn/wblearn.github.io"
}5.bugs
问题追踪系统的URL或邮箱地址;npm bugs用的上。比如我的
"bugs": "https://github.com/wblearn/wblearn.github.io/issues"
6.devDependencies
指定项目开发所需要的模块,如果只需要下载使用某些模块,而不下载这些模块的测试和文档框架,放在这个下面比较不错。
"devDependencies": { "grunt": "~0.4.5", "grunt-contrib-less": "~0.11.4", "grunt-contrib-watch": "~0.6.1", "grunt-banner": "~0.2.3", "grunt-contrib-uglify": "~0.5.1"
}7.scriptsobject
Key是生命周期事件名,value是在事件点要跑的命令。参考npm-scripts。
scripts指定了运行脚本命令的npm命令行缩写,比如push指定了运行npm run push时,所要执行的命令。
下面的设置指定了npm run preview、npm run watch、npm run push、npm run cafe时,所要执行的命令。
"scripts": { "preview": "cd _site; python -m SimpleHTTPServer 8020", "py3view": "cd _site; python3 -m http.server 8020", "watch" : "grunt watch & npm run preview & jekyll serve -w", "py3wa" : "grunt watch & npm run py3view & jekyll serve -w", "boil" : "git push boilerplate boilerplate:master", "push" : "git push origin master --tag", "cafe" : "git co gitcafe-pages; git merge master; git push gitcafe gitcafe-pages:gitcafe-pages --tag; git co master;"
}其它字段
1.Dependencies
指示当前包所依赖的其他包。
{ "dependencies" :
{ "foo" : "1.0.0 - 2.9999.9999"
, "bar" : ">=1.0.2 <2.1.2"
, "baz" : ">1.0.2 <=2.3.4"
, "boo" : "2.0.1"
, "qux" : "<1.0.0 || >=2.3.1 <2.4.5 || >=2.5.2 <3.0.0"
, "asd" : "http://asdf.com/asdf.tar.gz"
, "til" : "~1.2"
, "elf" : "~1.2.3"
, "two" : "2.x"
, "thr" : "3.3.x"
}
}版本格式可以是下面任一种:
ersion完全匹配>version大于这个版本>=version大于或等于这个版本<version<=version~version非常接近这个版本^version与当前版本兼容1.2.xX代表任意数字,因此1.2.1, 1.2.3等都可以http://...Unix系统下使用的tarball的URL。*任何版本都可以""任何版本都可以version1 - version2等价于>=version1 <=version2.range1 || range2满足任意一个即可git...Git地址user/repo
2.License
授权方式,如果是使用一个普遍的license,比如BSD-3-Clause或MIT,直接使用:
{ "license" : "BSD-3-Clause" }3.main
main字段指定了加载的入口文件,require('moduleName')就会加载这个文件。这个字段的默认值是模块根目录下面的index.js。
4.Configobject
Config对象中的值在Scripts的整个周期中皆可用,专门用于给Scripts提供配置参数。
5.Keywords
字符串数组。人们使用 npm search 搜索时发现你的项目
6.Description
必须是字符串。npm search的时候会用到。
7.Bin
bin项用来指定各个内部命令对应的可执行文件的位置。很多的包都会有执行文件需要安装到PATH中去。
这个字段对应的是一个Map,每个元素对应一个{ 命令名:文件名 }。{ "bin" : { "npm" : "./cli.js" } }
8.Engines
engines字段指明了该模块运行的平台,比如 Node 的某个版本或者浏览器
既可以指定node版本:{ "engines" : {"node" : ">=0.10.3 <0.12" } }
也可以指定npm版本:{ "engines" : {"npm" : "~1.0.20" } }
写在最后
对于学习nodejs或者npm的同学,详细了解package.json的一些字段不管是搭建自己的博客或者项目都有好处,当然,以上只列出package.json文件的部分主要字段,如果还想了解更多,可以参考阮一峰的package.json文件或者package.json字段全解。
作者:wblearn
链接:https://www.jianshu.com/p/bcd085349391
共同學習,寫下你的評論
評論加載中...
作者其他優質文章
