4

I'm trying to webpack an express application but I'm having the following problem wherever I try to retrieve the / page:

Getting Error: Cannot find module "." at webpackMissingModule

Here's code that reproduces this:

import express from 'express';

const app = express();
const port = 8088;

app.set('view engine', 'pug')

app.listen(port, () => console.log(`Listening on ${port}`));

app.get('/', (req, res) => {
    res.render('index');
});

Initially I thought it was because pug was not included in the modules so I tried adding require('pug') in the page but that just moved the error to the server launch rather than runtime.

Here's my webpack configuration:

const path = require('path');
module.exports = {
    entry: {
        index: path.join(__dirname, 'index.js')
    },
    target: 'node',
    module: {
        rules: [
            {
                test: /\.js$/,
                loader: 'babel-loader',
                include: [
                    __dirname
                ],
                exclude: /node_modules/
            }
        ]
    },
    resolve: {
        modules: [__dirname, 'node_modules']
    },
    output: {
        path: __dirname,
        filename: '[name].entry.js'
    }
}

I am using express 4.16, pug 2.0-rc4, webpack 3.8 and babel loader 7.1

I've also tried to include all node modules but then I get a different error (dP.f is not a function)

1 Answer 1

4

That's because although you are excluding node_modules from being compiled by babel, they are still being included on your bundle.

You also need to ignore node_modules from being included into your bundle.

Install webpack-node-externals

npm install webpack-node-externals --save-dev

And add two lines to your webpack config.

const path = require('path');
const nodeExternals = require('webpack-node-externals'); //include this

module.exports = {
    entry: {
        index: path.join(__dirname, 'index.js')
    },
    target: 'node',
    module: {
        rules: [
            {
                test: /\.js$/,
                loader: 'babel-loader',
                include: [
                    __dirname
                ],
                exclude: /node_modules/
            }
        ]
    },
    resolve: {
        modules: [__dirname, 'node_modules']
    },
    externals: [nodeExternals()], // just add this
    output: {
        path: __dirname,
        filename: '[name].entry.js'
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

This works, but it's probably not ideal since I will not be able to deploy the bundle directly but rather would need to also install all modules on production. However I can probably fiddle with a whitelist to get it down to what I need

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.