相信很多小伙伴都见过各种各样的Node.js项目,而里面都有一个名为package.json的文件,而这个文件究竟是干什么的呢?
简单的来说,这个文件就是对整个项目的各种情况的配置(也是介绍),下面给出一个较为完整的package.json:
{ "name": "Hello World", "version": "0.0.1", "author": "张三", "description": "第一个node.js程序", "keywords":["node.js","javascript"], "repository": { "type": "git", "url": "https://path/to/url" }, "license":"MIT", "engines": {"node": "0.10.x"},"contributors":[{"name":"李四","email":"lisi@example.com"}], "scripts": { "start": "node index.js" }, "dependencies": { "express": "latest", "mongoose": "~3.8.3", "handlebars-runtime": "~1.0.12", "express3-handlebars": "~0.5.0", "MD5": "~1.2.0" }, "devDependencies": { "bower": "~1.2.8", "grunt": "~0.4.1", "grunt-contrib-concat": "~0.3.0", "grunt-contrib-jshint": "~0.7.2", "grunt-contrib-uglify": "~0.2.7", "grunt-contrib-clean": "~0.5.0", "browserify": "2.36.1", "grunt-browserify": "~1.3.0", } }
可以看到里面有
- name:项目名称
- version:项目版本
- author:作者.
- description:项目说明
- keywords:项目关键词
- repository:项目存储在哪
- liscense:项目的版权证书
- engines:开发引擎
- contributors:贡献者
- scripts:脚本命令缩写
- dependencies:项目运行所依赖的模块
- devDependencies:项目开发所依赖的模块
其中最重要的莫过于scripts、dependencies和devDependencies了,这三个与我们日常开发息息相关。
通过npm install,npm会自动地帮我们下载好dependencies和devDependencies里面的所有依赖,
scripts则是通过npm run 脚本(如上面的npm run start,npm就会帮你运行node index.js)