Configuration
npm setnpm set init-author-name 'Your name'npm set init-author-email 'Your email'npm set init-author-url 'http://yourdomain.com'npm set init-license 'MIT'
The above command is equivalent to setting the default value for npm init. When executing npm init init in the future, the author name, email, homepage, and license fields of package.json will automatically write the preset value. This information will be stored in the ~/.npmrc file in the user's home directory, so that the user does not need to enter every project.
If a project has different settings, you can run npm config for that project.
1.
npm set save-exact true
The above command sets that when adding a module, package.json will record the exact version of the module, rather than an optional version range.
2.
npm config get prefix
3.
npm config set prefix /usr/local
npm use
Install:
npm install grunt-cli
Before installation, npm install will check whether the specified module already exists in the node_modules directory. If it exists, it will no longer be reinstalled, even if the remote repository already has a new version.
If you want a module to be reinstalled for npm regardless of whether it has been installed or not, you can use the -f or --force parameters.
Local installation: package will be downloaded to the current directory and can only be used in the current directory. After the installation is completed, an additional node_modules directory is found in the current directory, and grunt-cli is installed in it.
npm install -g grunt-cli
Global installation: package will be downloaded to a specific system directory, and the installed package can be used in all directories. Now it has become /usr/local/lib/node_modules/grunt-cli, /usr/local/lib/node_modules/ is the global installation directory mentioned before.
1. Install the devDependencies module configured in the current directory package.json file
npm install
2. Install local module files
npm install ./package.tgz
3. Install the module with the specified URL
npm install https://github.com/indexzero/forever/tarball/v0.5.6
4. Install the modules contained in the specified directory in the local file system
npm install <folder>
5. Install and update the version configuration in package.json
npm install <name> [save|save-dev|save-optional]
(1) The name and version information of the module installed with the save parameter will appear in the dependencies option of package.json
(2) The name and version information of the module installed with the save-dev parameter will appear in the devDependencies option of package.json
(3) The name and version information of the module installed with the save-optional parameter will appear in the optionalDependencies option of package.json
6. Install the specified version of the module
npm install <name>@<version>Example:npm install [email protected]
7. Install a certain version within the specified version number range of the module
npm install <name>@<version range>
Example:
npm install async@”>=0.2.0 <0.2.9″
Force force pulls remote resources even if the module is already installed locally
Example:
npm install underscore force
8.-g or global installation module globally. If this parameter is not available, it will be installed in the node_modules subdirectory of the current directory.
Example:
npm install -g express
If you want all modules to be reinstalled forcibly, delete the node_modules directory and re-execute npm install.
$ rm -rf node_modules$ npm install
renew
npm update [-g] [<name> [<name> … ]
Updates the module in the specified name list. -g parameter updates the globally installed module.
If the name is not specified and is not in a certain module, all packages that are dependent on the current directory will be updated (including global and module); if the current directory is in a certain module directory, the modules that are dependent on the module will be updated, so when running npm update without specifying the name, it is best to run in a certain module to avoid updating to other modules that do not want to be updated.
uninstall
npm uninstall package
Check
Check out which packages are installed
npm ls --depth=0
View specific information for specific packages
npm ls grunt-clinpm info grunt-cli
search
npm search grunt-cli
release
1.package.json
package.json description:
When the npm command is run, it will read the package.json file of the current directory and interpret this file. This file is based on the Packages/1.1 specification. In this file, you can define your application name (name), application description ( description), keywords ( keywords), version number ( version ), application configuration items ( config ), homepage ( homepage ), author ( author ), resource repository address ( repository ), bug submission address ( bugs ), authorization methods ( licenses ), directories ( directories ), application entry file ( main ), command line file ( bin ), application dependencies ( dependencies ), development environment dependencies ( devDependencies ), running engines ( engines ) and scripts ( scripts ), etc.
For developers, both developing and publishing modules rely on their correct understanding of the meaning contained in this file package.json. Let's use an example shared in this article to illustrate the following:
{ "name": "test", "version": "0.1.0", "description": "A testing package", "author": "A messed author <[email protected]>", "dependencies": { "express": "1.xx", "ejs": "0.4.2", "redis": ">= 0.6.7" }, "devDependencies": { "vows": "0.5.x" }, "main": "index", "bin": { "test": "./bin/test.js" }, "scripts": { "start": "node server.js", "test": "vows test/*.js", "preinstall": "./configure", "install": "make && make install" }, "engines": { "node": "0.4.x" }}In this example, we define the application's entry file (main) as index. When other applications refer to our module require('test'), the main value index.js file is called. Scripts ( scripts ) defines several different commands using the hash table. The node server.js defined in script.start will be called at npm start, and the commands defined in scripts.test will be called when npm test is called. If some native modules need to be compiled, we can define precompiled and compiled commands.
In this example, the application dependencies module (dependencies) and the development environment dependencies module (devDependencies) are also defined. The application dependent module will be installed in the node_modules directory of the current module during installation. The dependency modules in the development environment are mainly used in the development environment. Use the command npm install or link plus the parameter -dev to install it in the node_modules directory of the current module.
name: the name of package (because it will become part of the url, the letters of non-url-safe will not pass, and ".", "_" are not allowed to appear). It is best to search for the name you have already existed at [](http://registry.npmjs.org/)
version: the version of package. When the package changes, the version should also change. At the same time, the version you declare needs to pass the verification of semver (semver can be Googled by itself)
dependencies: The application dependency module of package, that is, if others want to use this package, at least what needs to be installed. The application dependency module will be installed in the node_modules directory of the current module.
devDependencies: The development dependency module of package, and the unrelated code is automatically deleted when the plug-in is released. Use a file to record the plug-ins installed or needed in the current project, that is, others want to develop on this package, and you can install the plug-ins required for the project with one click.
2. Version number
Everyone has also noticed that some version numbers in package.json are >= 0.6.7 and some are 1.xx. What is the difference? npm is used for semantic version recognition for version management. Not all modules provide backward compatibility, and sometimes some modules are not backward compatible for some reason. So we need to define some rules to ensure that the module can be available in certain specific versions and to ensure that the latest version can be used, because those versions always modify some bugs or improve performance, etc. Let's take a look at the fields defined by the version:
Example: 0.4.2
+ Large version (0)
+ Small version (4)
+ Patch Version (2)
When a software is released, the default is version 1.0.0. If the patch is released in the future, the last digit is added, such as 1.0.1; if new functions are added and the original functions are not affected, the intermediate number (i.e., small version number), such as 1.1.0; if the changes introduced destroy backward compatibility, the first digit is added, such as 2.0.0.
In the above definition of package.json, we are sure that the module can run in all Nodejs versions 0.4 and above and below 0.5. The dependent module redis can run on all versions greater than or equal to 0.6.7. The dependent module ejs can only ensure that it runs in version 0.4.2. The dependent module express can be compatible with 1.0.0 or equal to 2.0.0.
generate
npm init
Used to initialize and generate a new package.json file. It will ask users a series of questions. If you think you don’t need to modify the default configuration, just press Enter all the way.
If -f (representing force) and -y (representing yes), skip the questioning stage and directly generate a new package.json file.