Nikita Konyshev
Nikita Konyshev

Reputation: 163

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?

Upvotes: 15

Views: 29092

Answers (8)

jack ma
jack ma

Reputation: 26

Your configuration has some issues, you should configure it like this below.

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

Upvotes: 0

exmaxx
exmaxx

Reputation: 3664

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.

Upvotes: 5

Mansehej Singh
Mansehej Singh

Reputation: 131

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

Upvotes: 12

kordoulakis
kordoulakis

Reputation: 1

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"

Upvotes: 0

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',
    },
  ],
}

Upvotes: 2

Shadrachodek
Shadrachodek

Reputation: 331

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
    }
  ]
}

Upvotes: 4

Gedeon Mutshipayi
Gedeon Mutshipayi

Reputation: 4083

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

Upvotes: 5

kissu
kissu

Reputation: 46761

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

Upvotes: 12

Related Questions