next. js_如何更改Next.js应用程序端口

本文介绍如何在本地运行Next.js应用时更改默认的HTTP端口。通常,默认端口为3000,但可能与其他服务冲突。通过修改package.json文件中的scripts部分,可以将端口更改为所需的数值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

next. js

I’ve been asked how to change the HTTP port of an app built using Next.js, when you are running it locally. By default the port is 3000, but that’s a commonly used port and perhaps you have another service running on it.

当您在本地运行时,有人问我如何更改使用Next.js构建的应用程序的HTTP端口。 默认情况下,端口为3000 ,但这是一个常用的端口,也许您正在运行其他服务。

How can you change it?

您该如何更改?

The answer is in the package.json file stored in the Next.js app main folder.

答案在Next.js应用程序主文件夹中存储的package.json文件中。

By default the file content is this:

默认情况下,文件内容为:

{
  "name": "learn-starter",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start"
  },
  "dependencies": {
    "next": "9.3.5",
    "react": "16.13.1",
    "react-dom": "16.13.1"
  }
}

Note: the exact packages numbers will differ in your case, as they get updated

注意:具体包号在您的情况下会有所不同,因为它们会更新

The thing you need to change is the scripts part.

您需要更改的是scripts部分。

Change:

更改:

"dev": "next dev",

to

"dev": "next dev -p 3001"

to start Next.js on port 3001 instead of 3000.

在端口3001而不是3000上启动Next.js。

Now when you run npm run dev, the command used to start the development server locally, you will see it start on port 3001:

现在,当您运行npm run dev ,用于本地启动开发服务器的命令将在3001端口启动:

翻译自: https://flaviocopes.com/nextjs-change-app-port/

next. js

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值