search
HomeWeb Front-endJS TutorialWhat are some useful tips and tricks in NPM

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

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

bash code:
npm i gulp-pug gulp-debug gulp-sass

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}

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

You can use the -P shortcut, like this:

bash code :
npm i gulp -P

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

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

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

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' ]

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

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

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

or

bash code:
npm s gulp debug

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

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

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

List dependencies

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

bash code:
npm ls

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

这将打印出这样的东西:

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

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

bash 代码:
npm ls -g -depth 0

运行测试

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

显示可用的 script

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

bash 代码:
npm run

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

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

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

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

从 Github 仓库安装 package(包)

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

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

或者你可以省略域名部分

bash 代码:
npm i sindresorhus/gulp-debug

打开包的 Github 页面

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

bash 代码:
npm repo create-react-app

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

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

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

bash 代码:
npm run env | grep npm_

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

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

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

添加自己的 NPM 变量

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

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

现在,如果你使用前面讨论的命令 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...

默认情况下,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"}

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

bash 代码:

gulp build --dist ./dist

总结:以上就是有关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!

Statement
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
Behind the Scenes: What Language Powers JavaScript?Behind the Scenes: What Language Powers JavaScript?Apr 28, 2025 am 12:01 AM

JavaScript runs in browsers and Node.js environments and relies on the JavaScript engine to parse and execute code. 1) Generate abstract syntax tree (AST) in the parsing stage; 2) convert AST into bytecode or machine code in the compilation stage; 3) execute the compiled code in the execution stage.

The Future of Python and JavaScript: Trends and PredictionsThe Future of Python and JavaScript: Trends and PredictionsApr 27, 2025 am 12:21 AM

The future trends of Python and JavaScript include: 1. Python will consolidate its position in the fields of scientific computing and AI, 2. JavaScript will promote the development of web technology, 3. Cross-platform development will become a hot topic, and 4. Performance optimization will be the focus. Both will continue to expand application scenarios in their respective fields and make more breakthroughs in performance.

Python vs. JavaScript: Development Environments and ToolsPython vs. JavaScript: Development Environments and ToolsApr 26, 2025 am 12:09 AM

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.

Is JavaScript Written in C? Examining the EvidenceIs JavaScript Written in C? Examining the EvidenceApr 25, 2025 am 12:15 AM

Yes, the engine core of JavaScript is written in C. 1) The C language provides efficient performance and underlying control, which is suitable for the development of JavaScript engine. 2) Taking the V8 engine as an example, its core is written in C, combining the efficiency and object-oriented characteristics of C. 3) The working principle of the JavaScript engine includes parsing, compiling and execution, and the C language plays a key role in these processes.

JavaScript's Role: Making the Web Interactive and DynamicJavaScript's Role: Making the Web Interactive and DynamicApr 24, 2025 am 12:12 AM

JavaScript is at the heart of modern websites because it enhances the interactivity and dynamicity of web pages. 1) It allows to change content without refreshing the page, 2) manipulate web pages through DOMAPI, 3) support complex interactive effects such as animation and drag-and-drop, 4) optimize performance and best practices to improve user experience.

C   and JavaScript: The Connection ExplainedC and JavaScript: The Connection ExplainedApr 23, 2025 am 12:07 AM

C and JavaScript achieve interoperability through WebAssembly. 1) C code is compiled into WebAssembly module and introduced into JavaScript environment to enhance computing power. 2) In game development, C handles physics engines and graphics rendering, and JavaScript is responsible for game logic and user interface.

From Websites to Apps: The Diverse Applications of JavaScriptFrom Websites to Apps: The Diverse Applications of JavaScriptApr 22, 2025 am 12:02 AM

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python vs. JavaScript: Use Cases and Applications ComparedPython vs. JavaScript: Use Cases and Applications ComparedApr 21, 2025 am 12:01 AM

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!