简体   繁体   中英

Nuxt3 Vite server port

I need to config server port for Nuxt3. I try to do it so:

nuxt.config.ts

import { defineNuxtConfig } from 'nuxt3'

export default defineNuxtConfig(
  vite: {
    server: {
      port: 5000,
    },
  },
})

But it doesn't work. How to set server port in Nuxt3?

As told here , this is currently not supported.

Change it like this

{
  "scripts": {
    "build": "nuxt build",
    "dev": "nuxt dev --port=5678", // here
    "generate": "nuxt generate",
    "preview": "nuxt preview"
  },
  "devDependencies": {
    "nuxt": "3.0.0-rc.1"
  }
}

Here is how to change the port for production: https://github.com/nuxt/framework/discussions/1884#discussioncomment-1631668

My solution for dev and production mode in all platforms (windows, Linux, MacOS) :

  1. Install the cross-env to support cross platforms.
sudo npm i cross-env -g
  1. Edit your project package.json
{
  "private": true,
  "scripts": {
    "build": "nuxt build",
    "dev": "nuxt dev --port=8001",
    "generate": "nuxt generate",
    "preview": "cross-env PORT=8001 node .output/server/index.mjs"
  },
  "devDependencies": {
    "nuxt": "3.0.0-rc.4"
  },
}

  1. Run the app
yarn run preview
module.exports = {
  apps: [
    {
      name: 'NuxtApp',
      port: 3001,
      exec_mode: 'cluster',
      instances: '1',
      script: './.output/server/index.mjs',
      args: 'preview',
    },
  ],
}

You can use like this in your ecosystem.config.js

If you are using PM2, set the port on ecosystem.config.js

module.exports = {
  apps: [
    {
      name: 'NuxtAppName',
      exec_mode: 'cluster',
      instances: 'max',
      script: './.output/server/index.mjs',
    port: 5000
    }
  ]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM