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"},
    "bugs":{"url":"http://path/to/bug","email":"[email protected]"},
    "contributors":[{"name":"李四","email":"[email protected]"}],
    "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一起组成了唯一的,必须的字段,改其中一个,另一个应同时改变

version

 和version一起组成了唯一的,必须的字段,改其中一个,另一个应同时改变

description

项目描述,字符串

script

scripts指定了运行脚本命令的npm命令行缩写,比如start指定了运行npm run start时,所要执行的命令。

下面的设置指定了npm run preinstallnpm run postinstallnpm run startnpm run test时,所要执行的命令。

"scripts": {
    "preinstall": "echo here it comes!",
    "postinstall": "echo there it goes!",
    "start": "node index.js",
    "test": "tap test/*.js"
}

命令行运行npm run dev,则执行dev的后边的脚本

传参: 用 -- 标明。如 --mode development  mode模式为development

变量: 通过 npm_package_前缀,npm脚本能拿到package.json里边的字段

"scripts": {
    "dev": "cross-env NODE_ENV=development webpack-dev-server --colors --progress --mode development",
    "build": "cross-env NODE_ENV=production webpack --progress --colors -p --mode production"
  }

main

main字段指定了模块的入口文件,require('moduleName')就会加载这个文件。这个字段的默认值是模块根目录下面的index.js

就是说,如果你的模块名叫"foo",用户安装了它,并且调用了 require("foo"),则这个main字段指定的模块的导出对象会被返回。

这应该是一个相对于包根目录的模块标识。

对于大部分模块来说,main字段除了指定一个主入口文件以外没什么其他用处了。

devDependencies和dependencies

dependencies字段指定了项目运行所依赖的模块,devDependencies指定项目开发所需要的模块。

它们都指向一个对象。该对象的各个成员,分别由模块名和对应的版本要求组成,表示依赖的模块及其版本范围。

这些东西会在根目录执行npm link或者npm install的时候初始化,并可以像其他npm配置参数一样管理。

peerDependencies

用来供插件指定其所需要的主工具的版本。有时,你的项目和所依赖的模块,都会同时依赖另一个模块,但是所依赖的版本不一样。比如,你的项目依赖A模块和B模块的1.0版,而A模块本身又依赖B模块的2.0版

{
  "name": "chai-as-promised", "peerDependencies": { "chai": "1.x" } }

上面代码指定,安装chai-as-promised模块时,主程序chai必须一起安装,而且chai的版本必须是1.x。如果你的项目指定的依赖是chai的2.0版本,就会报错。

config

用于添加命令行的环境变量。

{
  "name" : "foo",
  "config" : { "port" : "8080" },
  "scripts" : { "start" : "node server.js" }
}

然后,在server.js脚本就可以引用config字段的值

http
  .createServer(...)
  .listen(process.env.npm_package_config_port)

用户执行npm run start命令时,这个脚本就可以得到值。

同时,用户也可以改变这个值

$ npm config set foo:port 80

style

style指定供浏览器使用时,样式文件所在的位置。样式文件打包工具parcelify,通过它知道样式文件的打包位置。

"style": [
  "./node_modules/tipso/src/tipso.css"
]

猜你喜欢

转载自www.cnblogs.com/yminskyline/p/9101987.html