Nuxt configuration file

By default, Nuxt is configured to cover most use cases. This default configuration can be overwritten with the nuxt.config.js file.


nuxt.config.js

alias

This option lets you define aliases that will be available within your JavaScript and CSS.

nuxt.config.js

  1. import { resolve } from 'path'
  2. export default {
  3. alias: {
  4. 'style': resolve(__dirname, './assets/style')
  5. }
  6. }

Nuxt configuration file - 图1

See more on the alias property

build

This option lets you configure various settings for the build step, including loaders, filenames, the webpack config and transpilation.

nuxt.config.js

  1. export default {
  2. build: {
  3. /*
  4. ** You can extend webpack config here
  5. */
  6. extend(config, ctx) {}
  7. }
  8. }

Nuxt configuration file - 图2

See more on the build property

css

This option lets you define the CSS files, modules, and libraries you want to include globally (on every page).

nuxt.config.js

  1. export default {
  2. css: ['~/assets/css/main.css', '~/assets/css/animations.scss']
  3. }

You can omit the file extension for CSS, SCSS, Postcss, Less, Stylus, … files listed in the css array in your nuxt config file.

nuxt.config.js

  1. export default {
  2. css: ['~/assets/css/main', '~/assets/css/animations']
  3. }

By omitting the extension, if you have a css file and decide to change to use sass for example, you won’t have to update your nuxt.config as it will use the new extension once the filename remains the same.

Nuxt configuration file - 图3

See more on the css property

dev

This option lets you define the development or production mode of Nuxt (important when you use Nuxt programmatically)

nuxt.config.js

  1. export default {
  2. dev: process.env.NODE_ENV !== 'production'
  3. }

Nuxt configuration file - 图4

See more on the dev property

env

This option lets you define environment variables that are required at build time (rather than runtime) such as NODE_ENV=staging or VERSION=1.2.3. However, for runtime environment variables runtimeConfig is required.

nuxt.config.js

  1. export default {
  2. env: {
  3. baseURL: process.env.BASE_URL
  4. }
  5. }

runtimeConfig

The runtime config has built-in dotenv support for better security and faster development. The runtime config is added to the Nuxt payload so there is no need to rebuild in order to update the runtime configuration when working in development or with server-side rendering or client-side only applications. (For static sites you will still need to regenerate your site to see changes.)

.env support

If you have a .env file in your project root directory, it will be automatically loaded into process.env and accessible within your nuxt.config/serverMiddleware and any other files they import.

You can customize the path by using --dotenv <file> or disable entirely with --dotenv false. For example, you might specify a different .env file in production, staging or development environments.

publicRuntimeConfig

  • should hold all env variables that are public as these will be exposed on the frontend. This could include a reference to your public URL for example.
  • is available using $config in both server and client.

nuxt.config.js

  1. export default {
  2. publicRuntimeConfig: {
  3. baseURL: process.env.BASE_URL || 'https://nuxtjs.org'
  4. }
  5. }

privateRuntimeConfig

  • should hold all env variables that are private and that should not be exposed on the frontend. This could include a reference to your API secret tokens for example.
  • is only available on server using same $config (it overrides publicRuntimeConfig)

nuxt.config.js

  1. export default {
  2. privateRuntimeConfig: {
  3. apiSecret: process.env.API_SECRET
  4. }
  5. }

Using your config values:

You can then access these values anywhere by using the context in your pages, store, components and plugins by using this.$config or context.$config.

pages/index.vue

  1. <script>
  2. asyncData ({ $config: { baseURL } }) {
  3. const posts = await fetch(`${baseURL}/posts`)
  4. .then(res => res.json())
  5. }
  6. </script>

Inside your templates you can access your runtimeConfigs directly using $config.*

pages/index.vue

  1. <template>
  2. <p>Our Url is: {{ $config.baseURL}}</p>
  3. </template>

Nuxt configuration file - 图5

Your private config could be exposed if you use $config outside of a server-only context (for example, if you use $config in fetch, asyncData or directly inside your template).

Nuxt configuration file - 图6

See more on the runtimeConfig

Nuxt configuration file - 图7

See our blog post on Moving from @nuxtjs/dotenv to runtime config

Nuxt configuration file - 图8

See more on the env property

generate

This option lets you set up parameter values for every dynamic route in your application that will be transformed into HTML files by Nuxt.

nuxt.config.js

  1. export default {
  2. generate: {
  3. dir: 'gh_pages', // gh_pages/ instead of dist/
  4. subFolders: false // HTML files are generated according to the route path
  5. }
  6. }

Nuxt configuration file - 图9

See more on the generate property

head

nuxt.config.js

  1. export default {
  2. head: {
  3. title: 'my title',
  4. meta: [
  5. { charset: 'utf-8' },
  6. .....
  7. ]
  8. }
  9. }

This option lets you define all default meta tags for your application.

Nuxt configuration file - 图10

See more on head integration

loading

This option lets you customize the loading component that Nuxt uses by default.

nuxt.config.js

  1. export default {
  2. loading: {
  3. color: '#fff'
  4. }
  5. }

Nuxt configuration file - 图11

See more on loading integration

modules

With this option you can add Nuxt modules to your project.

nuxt.config.js

  1. export default {
  2. modules: ['@nuxtjs/axios']
  3. }

Nuxt configuration file - 图12

See more on the modules property

modulesDir

The modulesDir property is used to set the modules directories for path resolving. For example: Webpack’s resolveLoading, nodeExternals and postcss. The configuration path is relative to options.rootDir (default: process.cwd()).

nuxt.config.js

  1. export default {
  2. modulesDir: ['../../node_modules']
  3. }

Setting this field may be necessary if your project is organized as a Yarn workspace-styled mono-repository.

Nuxt configuration file - 图13

See more on the modulesDir property

plugins

This option lets you define JavaScript plugins that should be run before instantiating the root Vue.js application.

nuxt.config.js

  1. export default {
  2. plugins: ['~/plugins/url-helpers.js']
  3. }

Nuxt configuration file - 图14

See more on the plugins property

router

With the router option you can overwrite the default Nuxt configuration of Vue Router.

nuxt.config.js

  1. export default {
  2. router: {
  3. linkExactActiveClass: 'text-primary'
  4. }
  5. }

Nuxt configuration file - 图15

See more on the router property

server

This option lets you configure the connection variables for the server instance of your Nuxt application.

nuxt.config.js

  1. import path from 'path'
  2. import fs from 'fs'
  3. export default {
  4. server: {
  5. https: {
  6. key: fs.readFileSync(path.resolve(__dirname, 'server.key')),
  7. cert: fs.readFileSync(path.resolve(__dirname, 'server.crt'))
  8. }
  9. }
  10. }

Nuxt configuration file - 图16

See more on the server property

srcDir

This option lets you define the source directory of your Nuxt application.

nuxt.config.js

  1. export default {
  2. srcDir: 'client/'
  3. }

Project structure example with your Nuxt application in the client directory.

  1. **-| app/
  2. ---| node_modules/
  3. ---| nuxt.config.js
  4. ---| package.json
  5. ---| client/
  6. ------| assets/
  7. ------| components/
  8. ------| layouts/
  9. ------| middleware/
  10. ------| pages/
  11. ------| plugins/
  12. ------| static/
  13. ------| store/**

dir

This option lets you define custom names of your Nuxt directories.

nuxt.config.js

  1. export default {
  2. dir: {
  3. pages: 'views' // Nuxt will look for the views/ instead of the pages/ folder
  4. }
  5. }

Nuxt configuration file - 图17

See more on the dir property

pageTransition

This option lets you define the default properties of the page transitions.

nuxt.config.js

  1. export default {
  2. pageTransition: 'page'
  3. }

Nuxt configuration file - 图18

See more on the transition property

Other configuration files

Besides the nuxt.config.js there might be other config files in your project root, such as .eslintrc, prettier.config.json or .gitignore. These are used to configure other tools such as your linter, code formatter or your git repository and detached from the nuxt.config.js.

.gitignore

In your .gitignore file you will need to add the following so that they are ignored and not added to version control. node_modules which is where all your installed modules are. The nuxt folder which is what gets created when running the dev or build commands. The dist folder is the folder that gets created when running the generate command.

.gitignore

  1. node_modules .nuxt dist

What’s next

Nuxt configuration file - 图19

Check out the configuration-glossary