Nuxt3 Vite server port
Asked Answered
P

7

14

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?

Provence answered 6/5, 2022 at 9:19 Comment(1)
Btw, as a side note since the RC1, you only need import { defineNuxtConfig } from 'nuxt' (no need for nuxt3). As shown here: nuxtjs.org/announcements/nuxt3-rc/#vite--webpackBackcross
B
12

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

Backcross answered 6/5, 2022 at 9:34 Comment(3)
product PORT=5000 node .output/server/index.mjsHighfalutin
The devServer configuration is available to set the port. https://mcmap.net/q/795363/-nuxt3-vite-server-portAffect
devServer did not work for me, this does. Thanks!Life
V
10

As of the time of writing the answer, you can now define the port in your nuxt.config as follows:

export default defineNuxtConfig({
  devServer: {
    port: 3001,
  },
})

Source

Veracruz answered 18/5, 2023 at 13:8 Comment(0)
P
5

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
Persist answered 6/7, 2022 at 12:15 Comment(0)
H
5

Create a .env file in your project root:

PORT=5000

By doing that, you set the PORT environment variable which Nuxt picks up at start.

Horsa answered 11/7, 2023 at 21:5 Comment(0)
S
3

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
    }
  ]
}
Shawna answered 7/9, 2022 at 23:5 Comment(0)
M
2

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

module.exports = {
  apps: [
    {
      name: 'NuxtApp',
      port: 3001,
      exec_mode: 'cluster',
      instances: '1',
      script: './.output/server/index.mjs',
      args: 'preview',
    },
  ],
}
Mcallister answered 13/1, 2023 at 6:40 Comment(0)
K
0

It's true, even though theoretically you could do

Server: {
   port: 'xxxx'
}

or host: '0' in order to expose to the local network, in the definition of server you can see:

Use environment variables or top level server options to configure Nuxt server.

server?: Omit<ServerOptions, 'port' | 'host'>;

So even if you set those, they won't be taken into account.

Solve like @kissu said by changing package.json to something like
"dev": "nuxt dev --host=0 --port=3000"

Kamalakamaria answered 27/4, 2023 at 22:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.