0

Understanding the contents of "package.json"

Why I have to understand

When the npm module was published in March 2017, the corresponding package.json needed to be edited, so I did a research to have a fully understanding about all the properties of the package.json/

There could be some lacking of properties, but i will add them in later on/

name

Name of the Module, this field is required It is used in module import by import and require () in source. The npm module is assumed to be unique in name and version, so do not duplicate names with other libraries.

{ "name": "react-native-card-media" }

version

Version of the Module, this field is required The npm module is assumed to be unique in name and version. When upgrading npm, do not forget to update the version number.

{ "version": "0.0.5" }

private

If this property is set to true, you can not publish the module. Be sure not to publish projects that will not be published by mistake.

{ "private" : true }

description

Module's explanation Since it is displayed in npm search, it helps people find and understand your package.

{ "description": "Card Media component for React Native. Also supports multiple image layout." }

main

Specify the script file that is called first in the module. For example, we name the module foo, install it by the user, and when we execute require ("foo") which is the exports object of the module specified by main is returned. You must specify a relative path from the package root.

{ "main": "index.js" }

scripts

You can define an alias command to execute arbitrary shell script.

{
  "scripts": {
    "test": "eslint *.js ./components/*.js ./example/*/*.js",
    "start":      "node app.js",
    "production": "NODE_ENV=production node app.js"
  }
}

The key is a name that can be used as an alias like npm test, npm start, and shell script is specified as one line for the value. However, when registering non-reserved words such as test and start as alias commands, you must executenpm run production. When describing a one-line shell script, the bin of the module in dependencies and devDependencies automatically enters PATH. And you do like this:

{
  "devDependencies": {
    "eslint": "^3.18.0"
  }
}

Not like this:

{
  "scripts": {
    "test": "node_modules/.bin/eslint *.js"
  }
}

but like this

{
  "scripts": {
    "test": "eslint *.js"
  }
}

Scripts seems to have many more functions, so I'd like to write on it in the following posts

repository

Specify where the source code is managed.

{
  "repository": {
    "type": "git",
    "url": "git+ssh://git@github.com/dondoko-susumu/react-native-card-media.git"
  }
}

author

author specifies only one person, contributors specify array of people.

It seems common to write with such abbreviations.

{ "author": "Ken Kubota <kkbt2003@gmail> (https://github.com/dondoko-susumu/)" }

license

License information

{"license" : "MIT"}

bugs

URL and things like that which referring to the project problems and bug trackings are reported by email address If it is GitHub so it seems to be similar to the POST issue URL

{
  "bugs": {
    "url": "https://github.com/dondoko-susumu/react-native-card-media/issues",
    "email" : "project@hostname.com"
  }
}

homepage

Home page URL of the project

{
  "homepage": "https://github.com/dondoko-susumu/react-native-card-media#readme"
}

dependencies

Describe dependent modules and versions. When execute npm installing in the directory where package.json is installed, the modules described in dependencies and (devDependencies) are installed in the node_module directory.

If any modules depend on installed modules, they are also installed. Modules that depend on dependencies are installed like strings.

Do not write something that automatically runs a test script or a trans-piler (a module for building a release module) in dependencies. Such items are described in devDependencies described later.

--save Option

$ npm install --save co

With the --save option, it is registered in package.json without permission as follows.

{
  "dependencies": {
    "co": "^4.6.0"
  }
}

Version notation

^ (Caret Ranges)

Version specification specified automatically by the --save option (it seems to call ^ (caret))

^1.2.3 := >=1.2.3 <2.0.0
^0.2.3 := >=0.2.3 <0.3.0
^0.0.3 := >=0.0.3 <0.0.4

devDependencies

Define what you need when developing a module to be published to npm.

The one described in devDependencies is not installed if you are executing npm install as an external module of another project yourself.

On the other hand, if you clone the project with Git and install it at npm install it will be installed. This library is necessary only for development, it is useless at run time, so it does not need to be included.

Test scripts, compilers, task runners, etc. Define what you do not need to load as modules.

By the way, when you execute npm install as an end user, both dependencies and devDependencies are installed.

jest

You can write the setting of jest with the top level jest key of package.json.

set preset in settings

For preset, specify the npm module exporting jest-preset.json at the top level.

{
  "jest": {
    "preset": "react-native"
  }
}

references URL:

jest configuration npm publish semver publish


All rights reserved

Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí