search
HomeWeb Front-endJS TutorialDetailed explanation of how to detect and upgrade Node dependencies in projects

This article will introduce you to the method of detecting and upgrading Node dependencies in the project. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

Detailed explanation of how to detect and upgrade Node dependencies in projects

In modern front-end development, a project often relies on many third-party packages, ranging from dozens or even hundreds. So how to detect and upgrade these dependencies is became a problem. [Related recommendations: "nodejs Tutorial"]

npm outdated and npm update


Fortunately, npm provides us with npm outdated and npm update these two commands.

Enter an older react project and execute npm outdated. As expected, upgrade suggestions are given.

Detailed explanation of how to detect and upgrade Node dependencies in projects

Then execute npm update to upgrade. No response ???

Looked at --help, maybe the posture is wrong? Try npm update less . No response 1

Continue trying npm update less --save, still no response.

The Wanted that appears in the picture belongs to Wanted, but I still want to upgrade to Latest

Then I checked the detailed log through npm update --dd, I found that there will always be some prompts like this in the end:

Detailed explanation of how to detect and upgrade Node dependencies in projects

It seems that there is no hope of upgrading. The author thinks this may be for the sake of stability of the project? After all, upgrading across major versions is still very risky. But it doesn’t make sense that the minor version won’t be upgraded.

It seems npm update is hopeless.

So ncu is here!

神器ncu


ncu is npm-check-updates, which can detect and update## The dependent packages in #package.json are the latest latest versions. Let’s take a look at how to use it.

The first step is to install the dependency package

npm install -g npm-check-updates. Of course, you can also not install it and use npx to execute it. Here the author uses Global installation method.

After the installation is complete, you can execute the command through

npm-check-updates or quickly execute it through ncu.

Detecting upgradeable dependency packages

It is still the old react project just now, execute

ncu for upgrade detection:

Detailed explanation of how to detect and upgrade Node dependencies in projects

Um! It looks much clearer, and it directly gives the latest version that can be upgraded to. The screenshot here is only a part of it, and the list is quite long... But it’s not a big problem, after all

ncu can be upgraded with one click.

One-click upgrade

ncu provides us with the command parameters -u for one-click upgrade, execute ncu -u is enough.

After the execution, the output information is long, and only the last sentence is selected:

Run npm install to install new versions.

It is so crisp and clear that it makes people suspicious. Then go to

package.json to check it out. It turns out that it is a powerful one, and everyone who can upgrade it will be upgraded to the latest version.

You need to be careful when performing one-click upgrade. After all,

ncu -u will not consider cross-version compatibility issues. Some packages may be incompatible when performing major version upgrades. You need to pay attention here. , be prepared to change the code.

Global package detection, on-demand detection

Of course

ncu You can also detect globally installed packages, execute ncu - g is enough:

Detailed explanation of how to detect and upgrade Node dependencies in projects

Not only that, if you only need to detect a single package or need to ignore a package, this is fine. This is the usage example given by the ncu warehouse:

# upgrade only mocha
ncu mocha
ncu -f mocha
ncu --filter mocha

# upgrade packages that start with "react-"
ncu react-*
ncu "/^react-.*$/"

# upgrade everything except nodemon
ncu \!nodemon
ncu -x nodemon
ncu --reject nodemon

# upgrade only chalk, mocha, and react
ncu chalk mocha react
ncu chalk, mocha, react
ncu -f "chalk mocha react"

# upgrade packages that do not start with "react-".
ncu \!react-*
ncu '/^(?!react-).*$/' # mac/linux
ncu "/^(?!react-).*$/" # windows

Configuration file mode

If you find it too troublesome to enter various filtering conditions on the command line, it is more convenient. The way to use it is to create a configuration file, such as

.ncurc.json

{
  "upgrade": true,
  "reject": ["webpack", "antd"]
}

Configurable field explanation:

  • upgrade : Do you need one-click upgrade?
  • #filter: Which modules to upgrade only
  • reject: Which modules to ignore
Of course, the configuration file naming is arbitrary, such as the default routine naming:

.ncurc.{json,yml,js}. Or customize the file name, and then use --configFileName and --configFilePath to specify the file.

API 调用

ncu 还对外暴露了 API,我们可以通过编程的方式检测并升级某个项目的依赖包。使用示例如下:

const ncu = require('npm-check-updates');

(async () => {
  const upgraded = await ncu.run({
    // Pass any cli option
    packageFile: './package.json',
    upgrade: true,
    // Defaults:
    // jsonUpgraded: true,
    // silent: true,
  });

  console.log(upgraded); // { "mypackage": "^2.0.0", ... }
  // `upgrade: false`:返回可升级的包及最新版本信息
	// `upgrade: true`:返回已经升级的包及最新版本信息
})();

目前,我们的脚手架里就用到了这个特性,通过脚手架来对项目依赖进行升级。

OK,以上就是关于 Node 依赖检测并升级的全部内容,你开始用 ncu 了吗?

更多编程相关知识,可访问:编程入门!!

The above is the detailed content of Detailed explanation of how to detect and upgrade Node dependencies in projects. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:掘金社区. If there is any infringement, please contact admin@php.cn delete
Python vs. JavaScript: A Comparative Analysis for DevelopersPython vs. JavaScript: A Comparative Analysis for DevelopersMay 09, 2025 am 12:22 AM

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Python vs. JavaScript: Choosing the Right Tool for the JobPython vs. JavaScript: Choosing the Right Tool for the JobMay 08, 2025 am 12:10 AM

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript: Understanding the Strengths of EachPython and JavaScript: Understanding the Strengths of EachMay 06, 2025 am 12:15 AM

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScript's Core: Is It Built on C or C  ?JavaScript's Core: Is It Built on C or C ?May 05, 2025 am 12:07 AM

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript Applications: From Front-End to Back-EndJavaScript Applications: From Front-End to Back-EndMay 04, 2025 am 12:12 AM

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Python vs. JavaScript: Which Language Should You Learn?Python vs. JavaScript: Which Language Should You Learn?May 03, 2025 am 12:10 AM

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

JavaScript Frameworks: Powering Modern Web DevelopmentJavaScript Frameworks: Powering Modern Web DevelopmentMay 02, 2025 am 12:04 AM

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

The Relationship Between JavaScript, C  , and BrowsersThe Relationship Between JavaScript, C , and BrowsersMay 01, 2025 am 12:06 AM

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

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

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor