API: Using Nuxt.js Programmatically

You might want to use your own server with your middleware and your API. That's why you can use Nuxt.js programmatically.

You can require Nuxt.js like this:

  1. const { Nuxt, Builder } = require('nuxt')

Nuxt Constructor

To see the list of options to give to Nuxt.js, see the configuration section.

  1. // Require `Nuxt` And `Builder` modules
  2. const { Nuxt, Builder } = require('nuxt')
  3. // Require Nuxt config
  4. const config = require('./nuxt.config.js')
  5. // Create a new Nuxt instance
  6. const nuxt = new Nuxt(config)
  7. // Make sure to wait for Nuxt to load modules before proceeding
  8. await nuxt.ready()
  9. // Enable live build & reloading on dev
  10. if (nuxt.options.dev) {
  11. new Builder(nuxt).build()
  12. }
  13. // We can use `nuxt.render(req, res)` or `nuxt.renderRoute(route, context)`

You can take a look at the nuxt-express and adonuxt starters to get started quickly.

Debug logs

If you want to display Nuxt.js logs, you can add the following to the top of your file:

  1. process.env.DEBUG = 'nuxt:*'