合规国际互联网加速 OSASE为企业客户提供高速稳定SD-WAN国际加速解决方案。 广告
管理本地安装的包的最好方法是创建一个package.json文件。 package.json文件会给你提供很多好东西: 1. 它用作你的项目的包依赖管理文档。 2. 它允许你使用语义化版本管理规则,指定项目中能使用的包的版本。 3. 使你的构建版本可以重新生成,这意味着你可以更易于与其他开发者分享代码。 ## 需求 最少配置项, package.json 必须包括以下几项: "name" * 全部为小写字母 * 一个单词,无空格 * 允许半角破折号和下划线 "version" * 格式为 x.x.x * 遵循语义化版本号规则 (semver spec)[https://docs.npmjs.com/getting-started/semantic-versioning] 示例: ~~~ { "name": "my-awesome-package", "version": "1.0.0" } ~~~ ## 创建package.json文件 要创建package.json文件,运行以下命令: ~~~ > npm init ~~~ 此为初始化项目命令,会在你运行此命令的文件夹根目录下创建项目配置文件:package.json。同时每行会出现一个问题,你输入答案后会出来另一个问题。这些问题最终会记录到package.json文件中。 ### “--yes”标签 扩展的命令行问答式流程不是必须的。通常你可以轻松地使用package.json文件快速配置项目。 你可以运行带`--yes`或`-y`标签的`npm init`命令,来生成默认的package.json文件: ~~~ > npm init --yes ~~~ 这样只会问你作者是谁。其他的问题都是用默认值填充的: ~~~ > npm init --yes ~~~ 写入`/home/ag_dubs/my_package/package.json`文件的内容如下: ~~~ { "name": "my_package", "version": "1.0.0", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "ag_dubs", "license": "ISC", "repository": { "type": "git", "url": "https://github.com/ashleygwilliams/my_package.git" }, "bugs": { "url": "https://github.com/ashleygwilliams/my_package/issues" }, "homepage": "https://github.com/ashleygwilliams/my_package" } ~~~ * name:默认为作者名字,除非在git目录中,它会是git仓库的名字; * version:版本号,刚初始化的项目总是1.0.0; * main:总是index.js; * scripts:默认创建一行空的测试脚本; * keywords:为空 * author:作者 * 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 你也可以通过`set`命令来设置一些配置项。比如下边的这些: ~~~ > npm set init.author.email "wombat@npmjs.com" > npm set init.author.name "ag_dubs" > npm set init.license "MIT" ~~~ 注释: If there is no description field in the package.json, npm uses the first line of the README.md or README instead. The description helps people find your package on npm search, so it's definitely useful to make a custom description in the package.json to make your package more discoverable. ## 指定依赖包 To specify the packages your project depends on, you need to list the packages you'd like to use in your package.json file. There are 2 types of packages you can list: * "dependencies": these packages are required by your application in production * "devDependencies": these packages are only needed for development and testing ## 手动编辑package.json You can manually edit your package.json. You'll need to create an attribute in the package object called dependencies that points to an object. This object will hold attributes named after the packages you'd like to use, that point to a semver expression that specifies what versions of that project are compatible with your project. If you have dependencies you only need to use during local development, you will follow the same instructions as above but in an attribute called devDependencies. For example: The project below uses any version of the package my_dep that matches major version 1 in production, and requires any version of the package my_test_framework that matches major version 3, but only for development: ~~~ { "name": "my_package", "version": "1.0.0", "dependencies": { "my_dep": "^1.0.0" }, "devDependencies" : { "my_test_framework": "^3.1.0" } } ~~~ ## `--save` 和 `--save-dev`安装标记 The easier (and more awesome) way to add dependencies to your package.json is to do so from the command line, flagging the npm install command with either --save or --save-dev, depending on how you'd like to use that dependency. 在命令行中使用这两个标记,是添加依赖到你的package.json文件的更简单(也更酷)的方式。 添加package.json依赖的入口: `npm install <package_name> --save` 添加package.json开发环境依赖的入口: `npm install <package_name> --save-dev` ## 管理依赖包的版本 npm使用语义化版本管理依赖包,也就是我们常说的“SemVer”。 如果在项目文件夹下有`package.json`文件,你在此文件夹下运行命令`npm install`,npm就会检查文件中列出的依赖包,并下载所有满足语义化规则的最新版本的依赖包。 要学习更多关于语义化版本管理的内容,请查看本章第13节——语义化版本号。