简体   繁体   中英

Error finding core node modules during webpack build in windows

I am trying to write modern ES code in node.js. For that I am trying to configure babel using webpack. I am not able to build my source files using webpack command. But it is unable to properly find node_modules.

My webpack.config.js looks like this

const path = require('path');

const config = {
    entry: {
        bundle: path.resolve('src/js/index.js')
    },
    output: {
        path: path.resolve(__dirname, 'dist'),
        filename: '[name].js'
    },
    module: {
        rules: [
            {
                test: /\.js?$/,
                exclude: path.resolve(__dirname, 'node_modules'),
                loader: "babel-loader",
                options: {
                    presets: [
                        ["@babel/preset-env", { targets: { node: true } }]
                    ],
                    plugins: [
                        ["@babel/plugin-proposal-class-properties", { 
                        "loose": false }]
                    ]
                }
            }
        ]
    }
};
module.exports = (env, args) => {

    if (args.mode === 'development') {
        config.devtool = 'source-map';
    }

    return config;
}

I export the functions this way:

export default { welcome, defaultWelcomeNo };

I import the modules this way:

import welcomeHandler from './intents/welcome';
import bodyParser from 'body-parser';

I expect everything to compile properly and to get bundle.js in the end. Instead I am getting error as follows(part of a long error):

ERROR in ./node_modules/tunnel-agent/index.js
Module not found: Error: Can't resolve 'tls' in 'D:\Visual Studio 
Code\Workspace\multi-bot\node_modules\tunnel-agent'
 @ ./node_modules/tunnel-agent/index.js 4:10-24
 @ ./node_modules/request/lib/tunnel.js
 @ ./node_modules/request/request.js
 @ ./node_modules/request/index.js
 @ ./node_modules/request-promise-native/lib/rp.js
 @ ./src/js/utils/Util.js
 @ ./src/js/index.js
npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! multi-bot@1.0.0 dev: `webpack --mode development`
npm ERR! Exit status 2
npm ERR!
npm ERR! Failed at the multi-bot@1.0.0 dev script.
npm ERR! This is probably not a problem with npm. There is likely 
additional 
logging output above.

Actually I figured it out. Those node depencies were not included by webpack by default. I needed to add target: "node" property in webpack.config.js

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