Package. json, package-lock.json, and node_modules are package.json, package-lock.json and node_modules. What does it do? I’ll take you through it today!

Recommended reading

NPM common commands: blog.csdn.net/qq_32442973…

A, YinDou

In your impression, what are the elements of a specification? Like a box of medicine.

Just like this instruction manual, it has: drug name, ingredients, category and so on! Package. json is the same, so it’s no exaggeration to say that it’s an instruction manual! So what does our package.json really mean? Let’s get into the topic for you one by one.

Second,Package. json says what

Document presentation:

{"name": "firstApp ", "version": "0.1.0", "author": "zhangsan <[email protected]>", "description": "I am the first Node.js program in the world ", "keywords": ["node.js", "javascript"], "private": true, "bugs": {"url": "Http://path/to/bug", "email" : "[email protected]"}, "contributors" : [{" name ":" not bad ", "email" : "[email protected]" }], "repository": { "type": "git", "url": "https://path/to/url" }, "homepage": "Http://necolas.github.io/normalize.css", "license", "MIT", "dependencies" : {" react ":" ^ 16.8.6 ", "the react - dom" : "^ 16.8.6 react - the router -", "dom" : "^ 5.0.1", "the react - scripts" : "3.0.1"}, "devDependencies" : {" browserify ": "~ 13.0.0", "karma - browserify" : "~ 5.0.1"}, "scripts" : {" start ":" the react - scripts start ", "build" : "react-scripts build", "test": "react-scripts test", "eject": "react-scripts eject" }, "bin": { "webpack": "./bin/webpack.js" }, "main": "lib/webpack.js", "module": "es/index.js", "eslintConfig": { "extends": "The react - app"}, "engines" : {" node ":" > = 0.10.3 < 0.12 "}, "browserslist" : {" production ": [" > 0.2% ", "not dead", "not op_mini all"], "development" : [ "last 1 chrome version", "last 1 firefox version", "last 1 safari version" ] }, "style": [ "./node_modules/tipso/src/tipso.css" ], "files": [ "lib/", "bin/", "buildin/", "declarations/", "hot/", "web_modules/", "schemas/", "SECURITY.md" ] }Copy the code
parameter content
name The project/module name must contain no more than 214 characters and cannot start with a period (.) or an underscore (_). It cannot contain uppercase letters
version Project version
author Project developer, its value is you inA valid account name for npmjs.org follows the “Account name< mail > “rule, for example, [email protected]
description The project description is a string. It helps people find this package when using NPM Search
keywords The item keyword is an array of strings. It helps people find this package when using NPM Search
private Private or not. When set to true, NPM refuses to publish
license Software license terms that let users know their rights and limitations
bugs Bug submission address
contributors Project contributor
repository Project Warehouse Address
homepage The official URL of the project package
dependencies In the production environment, the project operation required
devDependencies In the development environment, project needs depend on
scripts Short for executing the NPM script command, such as “start” : “react-scripts start”.
bin Path to the executable file corresponding to the internal command
main Default project execution files, such as require(‘ webpack ‘); The webpack.js file in the lib directory is loaded by default. If not, the index.js file in the project and directory is loaded by default
module In the early days when there was no ES6 modularization solution, CommonJS standard was followed, and CommonJS standard package represented the entry file in the way of main. In order to distinguish, Module mode was added. However, the ES6 modular scheme is more efficient. Therefore, the SYSTEM checks whether the Module field exists before using the main field
eslintConfig EsLint checks file configuration for automatic read validation
engines The platform on which the project is run
browserslist A list of versions available for browser use
style The location of the style file for use by the browser; Style file packaging tool Parcelify, which knows where to package style files
files An array of file names included by the project

Please focus on the bold part.

So how do you create this file?

Create a new folder and then:

npm init -y
Copy the code

This initializes a package.json.

Third, package – lock. Json

To sum it up, lock the version number of the package you installed and upload it to Git to ensure that everyone else has the same dependencies when installing NPM.

Fourth, the node_modules

Node_modules is the folder used to store the packages downloaded and installed using the package management tool after Node is installed. Tools like Webpack, gulp, grunt, etc. In Node.js modules and files have a one-to-one correspondence, which means that a Node.js file is a module.

Modules:

In Node.js, modules and files have a one-to-one correspondence, which means that a Node.js file is a module. The contents of the file may be some JavaScript methods, jsON data, compiled C/C++ extensions, etc. The structure of Node.js has been mentioned in the misunderstanding about Node.js.

HTTP, FS, NET and other core modules provided by Node.js are implemented by C/C++ and externally encapsulated by JavaScript.

Require search module:

There are two types of modules in Node.js: core modules and file modules. Core modules are directly named, such as the longest HTTP module.

var http = require('http');
Copy the code

In the example above we used the relative path ‘./test’ to get the custom file module.

Node loads modules:

The core module has the highest priority and is directly loaded by name. If there is a naming conflict, the core module is loaded first. File modules can only be loaded according to the path. (You can omit the default.js extension, otherwise display the declaration)

  • An absolute path
  • Relative paths