捆绑express.js和next.js应用程序抛出错误:找不到模块next.config.js'

时间:2019-06-08 16:55:11

标签: express aws-lambda next.js

我已经设置了express.js + next.js app,它在开发环境中运行良好。当我尝试运行其webpack捆绑包时,其抛出错误

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

我正在尝试运行其捆绑软件,因为aws-lamda不允许我上传超过50MB的zip文件。

// 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;

1 个答案:

答案 0 :(得分:0)

到目前为止,我发现我们可以/应该扩展next.config.js来添加其他包条目,而不是创建单独的webpack.config.js。进行以下配置后,将在build / server目录中创建一个serverbundle.js文件。

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