Home > Article > Web Front-end > Introduction to npm&package.json in Nodejs
This article mainly introduces the detailed explanation of npm&package.json of Nodejs. The editor thinks it is quite good. Now I will share it with you and give it as a reference. Let’s follow the editor and take a look.
For a long time, as a front-end developer, in the company, I have written the page first, and then cooperated with the back-end to fill in the data into the front-end page, but occasionally I have free time. No matter what, I will also look at some frameworks and so on, and then use the framework to make a single-page application, app, etc. At this time, the data on the page is always fake data, and there is no way to do the data request part (because there is no background Well). So I feel it's time to learn node, which will also be helpful for webpack, front-end engineering, etc. that I will learn in the future.
As the front end, because tools such as gulp and webpack are often used, the most common ones we see are npm and package.json, so let’s summarize them first.
npm
Initialization
$ npm init or $ npm init --y
When doing front-end development, we often use build tools, such as gulp, webpack, etc. In order to allow others to participate, we need to tell others what dependencies the project has, and then let others install the same dependencies. The package.json generated by npm init is used to record the dependencies in our project. Similarly, when doing node development, dependency packages will also be used, and package.json records are also required.
Enter npm init in the terminal and it will ask for various information about package.json to confirm. If all use the default value , you can directly enter npm init --y in the terminal to quickly generate package.json.
Install dependent packages
$ npm install <package name> <package name> ... $ npm install <package name> -g $ npm install <package name> --save $ npm install <package name> --save-dev $ npm install <pacakage name> --O //--save-optional -B: --save-bundle -E: --save-exact
npm install acab04e509e5b64e7e2bc0d60ab53414 -g
means global installation, It should be noted that global mode does not mean installing a module installation package as a global package. It does not mean that it can be referenced from anywhere through require(). The meaning of -g is to install a module installation package as a global package. Packages are installed as globally available executable commands. This means that all packages installed through -g can be run in command mode in the terminal, such as gulp, webpack, etc. The difference between
--save
and --save-dev
is that the former is a dependency needed to run the project in the production environment, and is recorded in package.json after installation. under the dependencies keyword; the latter is a dependency required during development and is recorded under the devDependencies keyword after installation.
Similarly --O/B/E
will be recorded under the corresponding keywords respectively.
Update dependency packages
$ npm update $ npm update -g $ npm outdated $ npm outdated -g
Run in the project directorynpm update
You can upgrade the dependencies used in the project to the latest version, and npm update -g
can upgrade globally installed dependency packages to the latest version.
npm outdated
is used to check whether the module is outdated and list it.
Uninstall dependencies
$ npm uninstall <package name> <package name> ... $ npm uninstall <package name> -g $ npm uninstall <package name> --save $ npm uninstall <package name> --save-dev
Use npm uninstall
to uninstall dependencies, but after uninstalling, in package. The records in json will not be deleted. If you want to delete the records in package.json while uninstalling dependencies, you need to use all the options during installation when uninstalling. For example, if you use npm during installation install acab04e509e5b64e7e2bc0d60ab53414 --save
, when uninstalling, also use npm uninstall a57d0322de109e503dba2a67e33c825b --save
, and if you use --save-dev
, you also need to add the same options when uninstalling.
Use custom npm command
In package.json, there is a scripts
keyword, you only need to write it within the keyword Just customize the command and the actual command executed accordingly.
"scripts":{ "test": "nonde ./test.js", "dev": "gulp --gulpfile gulpfile-dev.js", "build": "gulp --gulpfile gulpfile-build.js" }
In the above configuration, as long as we run npm dev in the terminal, we are running gulp --gulpfile gulpfile-dev.js, which saves us typing in the terminal A very long command, very convenient.
Others
##npm view a57d0322de109e503dba2a67e33c825bYou can view the package.json file of the package. If you only want to see a certain feature of the package, Just add the corresponding key at the end, for example
npm v zepto version is to view the currently installed zepto version, v is the abbreviation of view.
npm ls can analyze all packages that can be found through the module path under the current project and generate a dependency tree.
npm doc acab04e509e5b64e7e2bc0d60ab53414You can open the official website of the dependent package, which actually opens the homepage in package.json.
package.json file
npm init. The file is used to record the dependencies used in the project and the project configuration information (such as name, version, license, etc.). The
npm install command automatically downloads the dependencies required for project operation and development based on this
configuration file.
一个比较完整的package.json文件如下:
{ "name": "project", "version": "1.0.0", "author": "张三", "description": "第一个node.js程序", "keywords":["node.js","javascript"], "repository": { "type": "git", "url": "https://path/to/url" }, "license":"MIT", "engines": {"node": "0.10.x"}, "bugs":{"url":"http://path/to/bug","email":"bug@example.com"}, "contributors":[{"name":"李四","email":"lisi@example.com"}], "scripts": { "start": "node index.js" }, "dependencies": { "express": "latest", "mongoose": "~3.8.3" }, "devDependencies": { "grunt": "~0.4.1", "grunt-contrib-concat": "~0.3.0" } }
在package.json中一些关键字的含义:
1.name:包名
2.version:版本号
3.description:包的描述
4.homepage:包的官网url
5.autor:包的作者名字
6.contributors:包的其他贡献者
7.dependencies:依赖包的列表,使用npm install可以安装依赖包到node_medule目录下
8.repository:包代码存放的地方,可以是git或者svn
9.keywords:关键字
10.scripts:脚本说明对象。它主要被包管理器用来安装、编译、测试和卸载包,示例如下:
"scripts":{ “install”:"install.js", "test":"test.js" }
11.main:模块引入方法require()在引入包时,会优先检查这个字段,并将其作为包中其余模块的入口,如果该字段不存在,则node会检查目录下的index.js,index.node,index.json作为默认入口。
12.devDependencies:一些模块只在开发时需要依赖,配置这个属性,可以提示包的后续开发者安装依赖包
The above is the detailed content of Introduction to npm&package.json in Nodejs. For more information, please follow other related articles on the PHP Chinese website!