在 Windows 中构建 webpack 期间查找核心节点模块时出错



我正在尝试在node.js中编写现代ES代码。为此,我正在尝试使用 webpack 配置 babel。我无法使用webpack命令构建源文件。但它无法正确找到node_modules。

我的 webpack.config.js 看起来像这样

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

我以这种方式导出函数:

export default { welcome, defaultWelcomeNo };

我以这种方式导入模块:

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

我希望所有内容都能正确编译并最终获得捆绑.js。相反,我收到如下错误(长错误的一部分):

ERROR in ./node_modules/tunnel-agent/index.js
Module not found: Error: Can't resolve 'tls' in 'D:Visual Studio 
CodeWorkspacemulti-botnode_modulestunnel-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.

其实我想通了。默认情况下,webpack 不包含这些节点依赖项。我需要在 webpack.config 中添加target: "node"属性.js

最新更新