我正在尝试在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';
我希望所有内容都能正确编译并最终获得bundle.js。相反,我得到如下错误(长时间错误的一部分):
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.
答案 0 :(得分:0)
实际上我知道了。默认情况下,webpack不包含这些节点依赖关系。我需要在webpack.config.js中添加target: "node"
属性