简体   繁体   中英

Bundling express.js and next.js app throwing Error: Cannot find module next.config.js'

I have setup a express.js + next.js app , which is running fine in development environment. When I am trying to run its webpack bundle, Its throwing error

Error: Cannot find module '/Users/user/workspace/project/next.config.js'

I am trying to run its bundle as aws-lamda is not allowing me to upload zip size more than 50MB.

// server.js
const express = require('express');
const argv = require('yargs').argv;    
const nextApp = require('./nextApp.js');
const handle = nextApp.getRequestHandler();
const pageRoutes = require('./routes/pages/index.js');
const port = argv.port || 3000;

const server = express();
// route to next.js web pages
server.use('/', pageRoutes);

server.get('*', (req, res) => {
  return handle(req, res)
});

nextApp.prepare()
  .then(() => {        
    server.listen(port, (err) => {
      if (err) throw err
      console.log(`> Ready on http://localhost:${port}`)
    });
  })
  .catch((ex) => {
    console.error(ex.stack)
    process.exit(1)
  });

module.exports = server;

So far I have found that we can/should extends next.config.js for adding additional bundle entries instead of creating a separate webpack.config.js. Following config will create a serverbundle.js file in build/server directory.

const merge = require('webpack-merge');

module.exports = {
    distDir: 'build',
    webpack (config, {isServer}) {

        if (isServer) {
            return merge(config, {
                entry () {
                    return config.entry().then((entry) => {
                        return Object.assign({}, entry, { serverbundle: './server' })
                    })
                },
                output: {
                    filename: '[name].js'
                }
            });    
        }
        return config;
    }
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM