Table of contents
1、npm prefix
This command is used to display the prefix
1.1 Use syntax
npm prefix [-g]
1.2 Description
Print local prefix to standard output. This is the nearest parent directory containing the package.json
file or node_modules
directory, unless -g
is also specified.
If -g
is specified, this will be the value of the global prefix.
1.3 Example
2、npm prune
This command is used to delete packages that are not related to the project.
2.1 Use syntax
npm prune [[<@scope>/]<pkg>...]
First, take a look at the current project packages, as shown below:
Run npm prune to process as follows:
It was found that some files could not be deleted, and other installation packages had been deleted. Check it again, as shown below:
2.1 Description
This command deletes the "extraneous" package. If a package name is provided, only packages matching one of the provided names are removed.
Irrelevant packages are those packages that exist in the node_modules
folder and are not listed as dependencies for any package.
If the --production
flag is specified or the NODE_ENV
environment variable is set to production
, this command will delete your < The package specified in /span> . to sets negation devDependencies
. Setting --no-production
NODE_ENV
production
If you use the --dry-run
flag, no changes will actually be made.
If the --json
flag is used, the changes made by npm prune
(or the changes made by --dry-run
will be used) Will be printed as a JSON object.
In normal operation, irrelevant modules are automatically pruned, so you only need this command with the --production
flag. However, in the real world, operations are not always "normal". This command can help clean up any garbage generated when a crash or error occurs.
3、npm publish
This command is used to publish a package
3.1 Use syntax
npm publish <package-spec>
3.2 Description
Publish the package to the registry so it can be installed by name.
By default, npm will publish to the public registry. This can be overridden by specifying a different default registry or using scope
in the name in conjunction with the scope configured registry.
package
is interpreted in the same way as other commands (e.g. npm install
, which can be:
- a) The folder containing the program described by the package.json
file - b) a gzipped tarball containing (a)
- c) The url that resolves to (b)
- d) published on the registry
<name>@<version>
with (c) - e) points to (d)
<name>@<tag>
- f) With the "latest" tag that satisfies (e)
<name>
- g) solves for (a)
<git remote url>
Publishing will fail if the package name and version combination already exists in the specified registry.
Once a package is published with a given name and version, that specific name and version combination can never be used again, even if it has been removed by npm unpublish.
Starting with npm@5
, sha1sum and integrity fields with sha512sum will be submitted to the registry during publishing. Subsequent installations will use the strongest supported algorithm to verify downloads.
is similar to --dry-run
, see npm pack, which figures out the files to include and packages them into a tarball for uploading to the registry.
Files included in the package
To see what will be included in your package, run npm pack --dry-run
. All files are included by default except:
-
always contains certain files related to package installation and distribution. For example,
package.json
,README.md
,LICENSE
, etc. -
If there is a "files" list in package.json, only the specified files will be included. (If directories are specified, they will be traversed recursively and their contents included, following the same ignore rules.)
-
If there are
.gitignore
or.npmignore
files, the ignored files and all subdirectories within them will be excluded from the package. If both files exist,.gitignore
is ignored and only.npmignore
is used..npmignore
files follow the same pattern rule as.gitignore
files -
If a file matches certain patterns, it will never be included unless explicitly added to the list in
package.json
, or in < Use the rule in the /span> or .gitignore file to cancel the ignore."files"
.npmignore
!
-
Symbolic links are never included in npm packages.
4、npm query
4.1 Using syntax
npm query <selector>
4.2 Description
The npm query command allows retrieving arrays of dependent objects using css selectors.
4.3 Example
1. Use the postinstall script to find all dependencies and uninstall them
npm query ":attr(scripts, [postinstall])" | jq 'map(.name)|join("\n")' -r | xargs -I {} npm uninstall {}
2. Find all git dependencies and explain who needs them
npm query ":type(git)" | jq 'map(.name)' | xargs -I {} npm why {}
3. Query all dependencies
npm query "*"
4. Query all direct dependencies
npm query ":root > *"
5. Query all direct production dependencies
npm query(":root > .prod")
6. Query all direct development dependencies
npm query(":root > .dev")
7. Query jqeury dependencies
npm query "#jquery"
8. Obtain dependencies for a specific license (MIT or ISC)
npm query "[license=MIT], [license=ISC]"
9. Find all packages that contain @ruyadorno as a contributor
:attr(contributors, [[email protected]])
The above are just some common examples, please refer tonpm official website
5、npm rebuild
5.1 Using syntax
npm rebuild [<package-spec>] ...]
别名: rb
5.2 Description
This command runs the npm build command on the matching folder. This is useful when you install a new version of node and all C++ plugins must be recompiled with the new binaries. When installing using --ignore-scripts
and --no-bin-links
it is also useful to explicitly select which packages to build and/or link bins.
If one or more package specifications are provided, only packages whose names and versions match one of the specifiers will be rebuilt.
Note: Using npm-rebuild to rebuild all npm packages on your local system or production environment may take some time. In some cases, rebuilding the entire project using npm-rebuild can even take hours.
6、a.s.l. repo
6.1 Using syntax
npm repo [<pkgname> [<pkgname> ...]]
6.2 Description
This command will open the package repository page in the browser.
This command attempts to guess the possible location of the package's repository URL and then attempts to open it using the --browser configuration parameter. If no package name is provided, it will search for package.json
in the current folder and use the repository
attribute.
For example, open the jquery warehouse page.
7、npm restart
7.1 Using syntax
npm restart [-- <args>]
7.2 Description
This will restart a project. Equivalent to running npm run-script restart
.
If the current project specifies a script in package.json
, the following script will be run: "restart"
- prerestart
- restart
- postrestart
If it does not specify a "restart"
script, but it does have a stop
and/or start
script, it will be run The following script:
- prerestart
- transfer
- stop
- poststop
- prestart
- start
- poststart
- postrestart
8、npm root
8.1 Using syntax
npm root
8.2 Description
Print valid node_modules
folders to standard output.
Using npm is useful in shell scripts that perform operations using node_modules
folders. For example:
#!/bin/bash
global_node_modules="$(npm root --global)"
echo "Global packages installed in: ${global_node_modules}"
9、npm run-script
9.1 Using syntax
npm run-script <command> [-- <args>]
别名: run, rum, urn
9.2 Description
This will run arbitrary commands from the package's "scripts"
object. If "command"
is not provided, it will list available scripts.
run[-script]
Used by the test, start, restart and stop commands, but can also be called directly. When scripts in a package are printed, they are divided into lifecycle (test, start, restart) and directly run scripts.
Any positional parameters will be passed to the specified script. Use --
to pass flags and options prefixed with -
, otherwise they will be parsed by npm.
For example:
npm run test -- --grep="pattern"
The parameter will only be passed to the script specified after npm run
, not any pre
or post
script.
env
script is a special built-in command that can be used to list the environment variables available to the script when it is run. If the "env" command is defined in your package, it will take precedence over the built-in commands.
In addition to the shell's pre-existing PATH
, npm run
adds node_modules/.bin
to the provided to the script. a> in your package, you would write: on prefix. For example, if you have a PATH
in. Any binaries provided by locally installed dependencies can be used without the node_modules/.bin
tap
devDependency
"scripts": {"test": "tap test/*.js"}
replace
"scripts": {"test": "node_modules/.bin/tap test/*.js"}
The actual shell used to run the script depends on the platform. By default, this is the /bin/sh command on Unix-like systems and cmd.exe on Windows. The actual shell /bin/sh refers to also depends on the system.
The script is run from the root of the package folder, regardless of what the current working directory is when npm run
is called. If you want the script to behave differently depending on the subdirectory you are in, you can use the INIT_CWD
environment variable, which contains the full path from where you are running npm run
.
npm run
sets the NODE
environment variable to the executable that executes npm
. node
If you try to run the script without the node_modules
directory and it fails, you will get a warning about running npm install
in case you forgotten.
For example, we will configure some common scripts in the package.json file in advance to facilitate our running.
By running npm run start we can start the current project, specify the command configured in the environment variable, or point to a command that can be run in node_modules.
10、npm search
10.1 Using syntax
npm search [search terms ...]
别名: find, s, se
10.2 Description
Search the registry for packages that match the search term. npm search
Performs a linear, incremental, lexically sorted search of all files in the registry through package metadata. If your terminal supports colors, it will further highlight matches in the results. This can be disabled via the configuration item color
Additionally, using the --searchopts
and --searchexclude
options paired with more search terms will include and exclude more patterns. The main difference between --searchopts
and standard search terms is that the former do not highlight results in the output and you can use them for more fine-grained filtering. Additionally, you can add both to your configuration to change the default search filtering behavior.
Search also allows you to locate maintainers in search results by prefixing their npm username with =
.
If a word begins with /
then it is interpreted as a regular expression and supports standard JavaScript RegExp syntax. In this case, the search will ignore the trailing /
. (Note that you must escape or quote many regular expression characters in most shells.)
10.3 Example
We try to search a package (jiang-isarray) as follows:
npm search jiang-isarray