Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nuxt3 项目修改启动端口号 #105

Open
vincentzyc opened this issue Feb 19, 2024 · 0 comments
Open

Nuxt3 项目修改启动端口号 #105

vincentzyc opened this issue Feb 19, 2024 · 0 comments

Comments

@vincentzyc
Copy link
Owner

一、修改开发环境端口号

  • 方式一:使用环境变量配置,设置(PORT 或 NUXT_PORT)
   # .env
   PORT=3001  #http://localhost:3001/
   NITRO_PORT=3001  #http://localhost:3001/

  • 方式二:nuxt.config.ts里配置
    // nuxt.config.ts
    export default defineNuxtConfig({
        devServer: {
            port: 3001,
        },
    })

  • 方式三:运行项目时的命令设置端口号
    // package.json
    "scripts": {
        "dev": "nuxt dev --port 3001"
    }

优先级:未使用的端口(指定的端口被占用时)> 方式三 > 方式二 > NITRO_PORT > PORT

二、修改生产环境端口号

生产环境端口号,默认使用的是打包时开发环境的端口号,要修改生产环境端口号,目前只知道这一种方法,就是在node .output/server/index.mjs命令前面加上PORT=3001

    // package.json
    "scripts": {
        "start": "PORT=3001 node .output/server/index.mjs"
    }

相关链接

nuxt/nuxt#15910
https://nuxt.com/docs/getting-started/deployment#configuring-defaults-at-runtime

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant