What are some useful tips and tricks in NPM

清浅
Release: 2019-01-21 17:49:31
Original
4117 people have browsed it

In NPM you can run just one command to install multiple modules, get package information, install specific versions of packages, list dependencies, and other useful tips and tricks

If you use NPM in your daily workflow, I believe the tips and tricks introduced in this article will be helpful to you

What are some useful tips and tricks in NPM

[Recommended course:node.js course]

[Recommended article:What is npmHow to install and configure npm

Generate package.json

We usually execute npm init and then start adding the information requested by npm. However, if we don't care about all this information and want to keep the default values, then we press enter for every piece of data npm requests. To avoid this, you can type npm init -y. This way you can skip asking questions.

Note: You can also use npm init --yes, which has the same effect.

Installing modules

You can use the simpler npm i instead of npm install.

Install multiple modules at once

You do not need to type an npm install command for each module, for example:

bash code:
npm i gulp-pugnpm i gulp-debugnpm i gulp-sass
Copy after login

You can install all these modules at once by running just one command:

bash code:
npm i gulp-pug gulp-debug gulp-sass
Copy after login

There is also an easier way, if all names start with the same prefix , you don’t need to type the entire name:

bash code:
npm i gulp{-debug,-sass,-pug}
Copy after login

Use install flags (installation parameters) shortcut

If you want to install software package and save it as a production dependency, you would normally do this

bash code:
npm i gulp --save-prod
Copy after login

You can use the -P shortcut, like this:

bash code :
npm i gulp -P
Copy after login

The same goes for development dependencies, instead of typing the full --save-dev flag, you can use the -D shortcut, like this:

bash code:
npm i gulp -D
Copy after login

By default, when you run npm install without any flags (arguments), npm will add the package as a dependency to the package.json file. If you want to prevent this, install with --no-save flags (parameter) as follows:

bash code:
npm i gulp --no-save
Copy after login

Get package information

The following command will display relevant information about the vue package:

npm view vue or npm v vue

npm 获取包信息

If you just want to get the latest version of the package, you can try the following command:

bash Code:
> npm v vue version> 2.5.17
Copy after login

If you want to get the complete version list of npm packages, please try the plural form

bash Code:
> npm v vue versions> [ '0.0.0', '0.6.0', '0.7.0', ... '2.5.15', '2.5.16', '2.5.17-beta.0', '2.5.17' ]
Copy after login

Install a specific version of a package

If you want to install a version instead of the latest version of a package, you can type:

bash Code:
npm i vue@2.5.15
Copy after login

Since it's easier to remember names than numbers (at least for me), you can use a dist-tag list of names and run the npm v command to get the list, like this:

bash code:
npm i vue@beta
Copy after login

Search for package

Sometimes you may not remember a package that you or your friends recommended some time ago ( the exact name of the package). In this case, you can use npm search and perform the search directly in the terminal:

bash code:
npm search gulp debug
Copy after login

or

bash code:
npm s gulp debug
Copy after login

This will print a list of packages with description, author and some other information:

npm 搜索结果

Uninstall package(package)

if You don't want to open the package.json file and manually remove the dependencies from there, you can remove it using:

bash code:
npm uninstall vue
Copy after login

This will remove the dependencies from the node_modules folder and package.json Delete the package from the file. Of course, you can use rm, un or r to achieve the same effect, for example:

bash code:
npm rm vue
Copy after login

If for some reason you just want to delete the package files from the node_modules folder , but still save it as a dependency in the package.json file, you can use the --no-save parameter:

bash code:
npm rm vue --no-save
Copy after login

List dependencies

If you want to see the list of project dependencies, you can use

bash code:
npm ls
Copy after login

This will list all the dependencies in the package.json file and their All dependencies. If you just want to list your dependencies you can do this

bash code:
npm ls --depth=0
Copy after login

这将打印出这样的东西:

bash 代码:
├── jquery@3.3.1├── vue@2.5.17└── yarn@1.12.3
Copy after login

当然,如果要查看所有全局安装的包的列表,可以使用 -g 标志

bash 代码:
npm ls -g -depth 0
Copy after login

运行测试

你可以使用 npm run tests 运行测试,但你可以用 npm test 甚至更简短的 npm t 代替。

显示可用的 script

有时,我们希望查看 package.json 文件中包含的脚本。 我们当然可以打开 package.json 文件,但我们也可以这样做:

bash 代码:
npm run
Copy after login

如果在 package.json 文件中有这样的配置,如下所示:

bash 代码:
"scripts": { "test": "jest", "build": "gulp build"}
Copy after login

那么 npm run 命令将显示以下内容:

bash 代码:
Lifecycle scripts included in npm: test jestavailable via `npm run-script`: build gulp-build
Copy after login

从 Github 仓库安装 package(包)

你可以直接从 Github 仓库安装一个包:

bash 代码:
npm i https://github.com/sindresorhus/gulp-debug
Copy after login

或者你可以省略域名部分

bash 代码:
npm i sindresorhus/gulp-debug
Copy after login

打开包的 Github 页面

你当然可以通过 Google 搜索,然后查找该页面,或者你可以执行以下操作:

bash 代码:
npm repo create-react-app
Copy after login

无需安装软件包即可执行上述命令。

列出所有可用的 NPM 环境变量

你可以通过运行以下命令来查看可供我们使用的 NPM 变量的完整列表:

bash 代码:
npm run env | grep npm_
Copy after login

上面的命令将打印如下内容:

bash 代码:
npm_config_fetch_retry_maxtimeout=60000npm_config_tag_version_prefix=vnpm_config_strict_ssl=truenpm_config_sso_type=oauth...
Copy after login

这些变量的好处是它们可以在你的脚本中使用,你甚至可以创建自己的 NPM 环境变量,让我们看看如何创建。

添加自己的 NPM 变量

你可以通过向 package.json 文件添加自己的 NPM 变量。 它可以是任何 key,但我更喜欢将所有 NPM 变量放在 config key 中,以保持结构有序。 像这样:

bash 代码:
"config": { "build_folder":"./dist" }
Copy after login

现在,如果你使用前面讨论的命令 npm run env | grep npm_ 列出你的变量,你会看到你的新变量在那里:

bash 代码:
npm_package_config_build_folder=./distnpm_config_fetch_retry_maxtimeout=60000npm_config_tag_version_prefix=vnpm_config_strict_ssl=truenpm_config_sso_type=oauth...
Copy after login

默认情况下,npm 会将你的变量命名以 npm_package 为前缀,并保持其在 package.json文件中的结构,即 config_build_folder 。

在 NPM script 中使用 NPM 变量

一旦你看到了完整的变量列表,并且你希望在 script 中使用这些变量中的任何一个的值,那么你就可以在 package.json 中执行此操作(请参阅上一节中变量 npm_package_config_build_folder 的值)

bash 代码:
"scripts": { "build": "gulp build --dist $npm_package_config_build_folder"}
Copy after login

一旦你用 npm run build 运行这个命令,它将被执行为

bash 代码:

gulp build --dist ./dist
Copy after login

总结:以上就是有关NPM 的一些有用的提示和技巧,希望对大家有所帮助。




The above is the detailed content of What are some useful tips and tricks in NPM. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
npm
source:html.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!