【发布时间】:2017-07-08 03:00:16
【问题描述】:
我正在尝试通过 WebPack 在浏览器中使用 node_modules。我已经阅读了教程和开始的步骤,但是卡住了。
我使用 webpack 生成带有下面 webpack 配置的 bundle.js,在 Chrome 浏览器中转到我的 index.html 时出现错误:
Uncaught ReferenceError: require is not defined
at Object.<anonymous> (bundle.js:205)
我还需要执行哪些额外步骤才能让浏览器重新识别 require?
index.html
<script src="bundle.js"></script>
<button onclick="EntryPoint.check()">Check</button>
index.js
const SpellChecker = require('spellchecker');
module.exports = {
check: function() {
alert(SpellChecker.isMisspelled('keng'));
}
};
package.json
{
"name": "browser-spelling",
"version": "1.0.0",
"description": "",
"main": "index.js",
"dependencies": {
"node-loader": "^0.6.0",
"spellchecker": "^3.3.1",
"webpack": "^2.2.1"
}
}
webpack.config.js
module.exports = {
entry: './index.js',
target: 'node',
output: {
path: './',
filename: 'bundle.js',
libraryTarget: 'var',
library: 'EntryPoint'
},
module: {
loaders: [
{
test: /\.node$/,
loader: 'node-loader'
},
{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel-loader',
query: {
presets: ['es2015']
}
}
]
}
};
【问题讨论】:
标签: javascript node.js webpack bundle