欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

npm中package.json

程序员文章站 2022-03-03 16:56:48
...

npm中package.json

通常我们使用npm init命令来创建一个npm程序时,会自动生成一个package.json文件。package.json文件会描述这个NPM包的所有相关信息,包括作者、简介、包依赖、构建等信息,格式是严格的JSON格式。

常用命令

npm i --save packageName 安装依赖包
npm i --save-dev packageName
npm i [email protected] 安装指定版本的包,版本号用@符号连接

属性介绍

  • name
    name和version是package.json中最重要的两个字段,也是发布到NPM平台上的唯一标识,如果没有正确设置这两个字段,包就不能发布和被下载。
  • version
    包的版本号。如"1.0.0"。
  • license
    包的开源协议名称。
  • main
    包的入口文件。
  • repository
    包的仓库地址。
"repository": {
    "type": "git",
    "url": "git+https://github.com/rainnaZR/es6-react.git"
  },
  • scripts
    通过设置这个可以使NPM调用一些命令脚本,封装一些功能。
"scripts": {"start": "babel-node src/pages/index.js",
    "build": "webpack --config config/webpack.config.js",
    "watch": "webpack-dev-server --config config/webpack.config.js --hot --inline --progress"
  }

比较有用

  "scripts": {
    "clean": "rimraf dist",
    "compile": "better-npm-run compile",
    "lint": "eslint src tests server",
    "lint:fix": "npm run lint -- --fix",
    "start": "better-npm-run start",
    "dev": "better-npm-run dev",
    "portal": "better-npm-run portal",
    "test": "jest --config jest.config.js",
    "test:dev": "npm run test -- --watch",
    "deploy": "better-npm-run deploy",
    "deploy:dev": "better-npm-run deploy:dev",
    "deploy:prod": "better-npm-run deploy:prod",
    "codecov": "cat coverage/*/lcov.info | codecov",
    "i18n": "better-npm-run i18n"
  },
  • dependencies
    指定依赖的其它包,这些依赖是指包发布后正常执行时所需要的,也就是线上需要的包。使用下面的命令来安装:
npm install --save packageName

如果是开发中依赖的包,可以在devDependencies设置。

  • devDependencies
    这些依赖只有在开发时候才需要。使用下面的命令来安装:
npm install --save-dev packageName 

demo:

{
  "name": "react",
  "version": "1.0.0",
  "description": "Command line instructions",
  "keywords": [
    "react",
    "es6",
    "react with es6"
  ],
  "homepage": "https://github.com/rainnaZR/es6-react",
  "bugs": {
    "url": "https://github.com/rainnaZR/es6-react",
    "email": "[email protected]"
  },
  "license": "ISC",
  "author": "ZRainna",
  "main": "src/pages/index.js",
  "directories": {
    "tests": "tests",
    "lib":"lib",
    "docs":"docs"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/rainnaZR/es6-react.git"
  },
  "scripts": {"start": "babel-node src/pages/index.js",
    "build": "webpack --config config/webpack.config.js",
    "watch": "webpack-dev-server --config config/webpack.config.js --hot --inline --progress"
  },
  "babel": {
    "presets": [
      "es2015-node5"
    ]
  },
  "devDependencies": {
    "webpack": "^1.13.2",
    "webpack-dev-server": "^1.16.1"
  },
  "dependencies": {
    "babel-loader": "^6.2.5",
    "babel-preset-es2015": "^6.14.0",
    "babel-preset-react": "^6.11.1",
    "react": "^15.3.2",
    "react-dom": "^15.3.2",
    "react-redux": "^4.4.5",
    "react-router": "^2.8.1",
    "redux": "^3.6.0"
  }
}