最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Getting Error: Cannot find module "." at webpackMissingModule - Stack Overflow

programmeradmin2浏览0评论

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)

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)

Share Improve this question asked Nov 23, 2017 at 10:09 apokryfosapokryfos 40.8k11 gold badges81 silver badges125 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

That's because although you are excluding node_modules from being piled 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'
    }
}
发布评论

评论列表(0)

  1. 暂无评论