cyrilwanner / next-compose-plugins

💡next-compose-plugins provides a cleaner API for enabling and configuring plugins for next.js
MIT License
736 stars 12 forks source link

How to use it with custom server? #18

Closed orafaelfragoso closed 4 years ago

orafaelfragoso commented 5 years ago

I'm having trouble loading my configuration on my custom server. This is the setup I'm using:

next.config.js

const withPlugins = require('next-compose-plugins')
const withEnv = require('next-env')
const withSass = require('@zeit/next-sass')
const withImages = require('next-images')
const withOptimizedImages = require('next-optimized-images')
const withSourceMaps = require('@zeit/next-source-maps')

const config = require('./config')

module.exports = withPlugins([
  withEnv(config.env),
  withImages(),
  [withSass, config.sass],
  [withOptimizedImages, config.optimizedImages],
  withSourceMaps(),
], config.next)

server.js

const express = require('express')
const next = require('next')
const helmet = require('helmet')
const routes = require('./config').routes
const conf = require('./next.config')

const port = process.env.PORT || 3000
const dev = process.env.NODE_ENV === 'development'
const app = next({ dev, dir: './src', conf: conf() })
const handler = routes.getRequestHandler(app)

app.prepare().then(() => {
  express()
    .use(helmet())
    .use(handler)
    .listen(port, (err) => {
      if (err) {
        throw err
      }

      console.log(`> Ready on http://localhost:${port}`) // eslint-disable-line no-console
    })
})

And this is the error I'm getting:

/Users/c80372a/Development/score-poc/node_modules/next-compose-plugins/lib/index.js:18
  defaultConfig
  ^

TypeError: Cannot destructure property `defaultConfig` of 'undefined' or 'null'.
    at /Users/c80372a/Development/score-poc/node_modules/next-compose-plugins/lib/index.js:17:57
    at Object.<anonymous> (/Users/c80372a/Development/score-poc/server.js:9:45)
    at Module._compile (internal/modules/cjs/loader.js:701:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
    at Module.load (internal/modules/cjs/loader.js:600:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
    at Function.Module._load (internal/modules/cjs/loader.js:531:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:754:12)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

The question here is: How can I make this configuration load on the server properly? NextJS is not reading the config file by default and I cannot load it manually.

I was able to make it work by importing the next.config.js object directly, outside the withPlugins function, but this is not what I want, since all the plugins won't be loaded this way.

Please, help!

cyrilwanner commented 4 years ago

Sorry for the late reply. Next.js should load the config, even when using a custom server. If that is not the case, please try updating next.js or open an issue in their reposiroty.

gajus commented 3 years ago

Next.js should load the config, even when using a custom server. If that is not the case, please try updating next.js or open an issue in their reposiroty.

Not clear from your answer what is the solution.

Yes, next.js does try to load next.config.js even when using custom server.

However, we want pass config using next.js server conf parameter (https://nextjs.org/docs/advanced-features/custom-server).

@orafaelfragoso Just gave up on this issue, so not helpful.