[英]Error finding core node modules during webpack build in windows
I am trying to write modern ES code in node.js. 我正在尝试在node.js中编写现代ES代码。 For that I am trying to configure babel using webpack.
为此,我正在尝试使用webpack配置babel。 I am not able to build my source files using
webpack
command. 我无法使用
webpack
命令构建源文件。 But it is unable to properly find node_modules. 但是它无法正确找到node_modules。
My webpack.config.js looks like this 我的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;
}
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. 我希望所有内容都能正确编译并最终获得bundle.js。 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.
默认情况下,webpack不包含这些节点依赖关系。 I needed to add
target: "node"
property in webpack.config.js 我需要在webpack.config.js中添加
target: "node"
属性
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.