takeaway
When you download a package via NPM, there is always a package.json file in the root directory of the package, which describes the details of the package, such as name, version number, author, etc. There are other properties that developers should be familiar with, and the following are some of the properties that are commonly used during development.
Property list
dependencies
Dependencies are specified with a simple hash of package name to version range. The version range is a string which has one or more space-separated descriptors. Dependencies can also be identified with a tarball or git URL
This property describes the dependencies of the package, meaning that the package must be based on these dependencies to function properly. When you download a package, you download its dependencies as well.
devDependencies
Dependencies are specified with a simple hash of package name to version range. The version range is a string which has one or more space-separated descriptors. Dependencies can also be identified with a tarball or git URL
The dependencies under devDependencies are dependencies that are needed during development. Unlike dependencies, it does not download dependencies when installed.
files
The ‘files’ field is an array of files to include in your project. If you name a folder in the array, then it will also include the files inside that folder.
When you publish a package, exactly what files will be published? It’s controlled by this property, and in general, it’s configured like this.
"files": [
"lib"
]
Copy the code
In this configuration, when the package is published, the Lib folder is published to the NPM Registry.
main
The main field is a module ID that is the primary entry point to your program. That is, if your package is named
foo
, and a user installs it, and then doesrequire("foo")
, then your main module’s exports object will be returned.
This property describes the entry to a package.
If there is a package named Module-1 in the project, you might use it this way, either via commonJS syntax or ES6.
// ES6
import 'module-1' from 'module-1'
// commonjs
const module-1 = require('module-1')
Copy the code
How does the above code execute the logic.
In fact, he looks at node_modules to see if there is a packge for Mode-1. Json file in the package path, and then look for the main field in the packge.json file. Finally, according to the main configuration information, find the specified file, so that the packge can be loaded correctly.
This is just a simple description. The real situation is much more complicated. For detailed logic, see nodeJS Module
In most cases, this property is configured as follows.
"main": "lib/index.js"
Copy the code
Refer to the link
- nodejs
- npm packge.json