npm learning (e) the use package.json

Use package.json

The best way to manage npm package locally installed is to create a package.json file.

A packagejson file:

  • Lists the project depends package.
  • It allows the use of semantic versioning rules specified version of the package of projects that can be used.
  • It allows you to build repeatable, making it easier to share with other developers.

demand

A package.json must:

  • “name”

    Uppercase letters are not allowed

    Spaces are not allowed

    Allows the use of dashes and underscores

  • “version”

    In the form of xxx

    Follow semver specification

For example:

{
  "name": "my-awesome-package",
  "version": "1.0.0"
}

Create a package.json

There are two basic ways to create package.json file.

1. Run the CLI questionnaire

Creating value package.json file you provide, run: npm the init

This will launch a command-line questionnaire, and finally create a directory on your startup command package.json in

2. Create a default package.json

Get the default package.json, run npm init -yes or npm init -y

This method will generate a default package.json, using information extracted from the current directory.

Wrote to /Users/wangkun/Desktop/nodeWork/work2/lesson8-1/package.json:

{
  "name": "lesson8-1",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "dependencies": {
    "react": "^16.8.6"
  },
  "devDependencies": {},
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
  • name: Current directory name

  • version: Version

  • description: Description

  • main: Import documents, always index.js

  • dependencies: production dependent

  • devDependencies: the development of dependence

  • scripts: Script to create an empty test script by default

  • keywords: Keyword

  • author: Author

  • license: License

You can also init command set several configuration options. Some useful:

> npm set init.author.email "[email protected]"

> npm set init.author.name "ag_dubs"

> npm set init.license "MIT"

note: 

If no package.json description field. The first line use README.md npm README or be replaced. This description is to help people find your package when searching npm, therefore customize definitely useful in describing the package.json make your

Package easier to find.

How to customize package.json questionnaire 

If you want to create multiple package.json files, you may want to customize the questions to ask the init process, so that the files always contains key information you expect. You can customize the fields and questions asked of.

To do this, create a custom .npm-init.js in the main directory ~ / .npm-init.js in.

A simple .npm-init.js might look like this:

module.exports = {
  customField: 'Custom Field',
  otherCustomField: 'This field is really cool'
}

Using this file in the main directory, run npm init outputs a package.json those containing the following:

{
  customField: 'Custom Field',
  otherCustomField: 'This field is really cool'
}

You can also use a custom prompt function problems.

 module.exports = prompt("what's your favorite flavor of ice cream, buddy?", "I LIKE THEM ALL");

For more information about how to create a high-level customization, see the init-package-json document

Specified dependency

To specify the project depends package, you need to be listed in package.json want to use the file package, you can List two packages:

"Dependencies": your application requires these packages in production.

"DevDependencies": The package is only for development and testing.

Manually edit package.json

You can manually edit package.json. You need to create a property called dependencies in the package object, the attribute points to an object. This object will contain the name of the attribute you want to use the package. It points to a semver expression that refers to

Given the project compatible with your version of the project. If you have dependencies, you only need to use during local development, follow the same instructions as above, but use of the property named devDependencies.

For example, the production of any major release version 1 my_dep matching packets following items used, and any required my_test_framework package version matching major version 3, but only for developing:

{
  "name": "my_package",
  "version": "1.0.0",
  "dependencies": {
    "my_dep": "^1.0.0"
  },
  "devDependencies" : {
    "my_test_framework": "^3.1.0"
  }
}

--save and --save-dev installed mark

Easier (also even better) way to add a dependency to package.json in, started from the command line, use --save or --save-dev mark npm install command, depending on how you want to use the dependency .

package.json文件中的dependencies:npm install <package_name> --save

向package.json文件中的devDependencies:npm install <package_name> --save-dev

Management relies version

npm semantic version control, or, as we often mention SemVer to manage the package version and the version range.

If you have a package.json files in your directory and run npm install, npm will see the file listed dependencies, and semantic versioning download the latest version.

 

 

Guess you like

Origin www.cnblogs.com/kunmomo/p/11221085.html