我有以下 webpack 配置:
module.exports = {
entry: "./league/index.ts",
output: {
path: "./",
filename: "bundle.js"
},
resolve: {
// Add `.ts` and `.tsx` as a resolvable extension.
extensions: ["", ".ts", ".js"]
},
module: {
loaders: [
// all files with a `.ts` or `.tsx` extension will be handled by `ts-loader`
{ test: /\.ts?$/, loader: "ts-loader" }
]
}
};
当我运行 webpack
时,出现此错误(出于隐私考虑,实际项目路径替换为 PROJECT_PATH
):
ERROR in ./~/backbone/backbone.js
Module not found: Error: Cannot resolve module 'jquery' in PROJECT_PATH\node_modules\backbone
@ ./~/backbone/backbone.js 17:4-21:6
造成这种情况的原因是 backbone.js
中的这段代码:
// Set up Backbone appropriately for the environment. Start with AMD.
if (typeof define === 'function' && define.amd) {
define(['underscore', 'jquery', 'exports'], function(_, $, exports) {
// Export global even in AMD case in case this script is loaded with
// others that may still expect a global Backbone.
root.Backbone = factory(root, exports, _, $);
});
// Next for Node.js or CommonJS. jQuery may not be needed as a module.
} else if (typeof exports !== 'undefined') {
var _ = require('underscore'), $;
try { $ = require('jquery'); } catch (e) {}
factory(root, exports, _, $);
// Finally, as a browser global.
}
jQuery 不是我需要的依赖项,但是 webpack
正在将 require
调用解释为我需要它。
最佳答案
Backbone 依赖于 jQuery,如果您使用另一个类似于 jQuery 的模块,如zepto ,您需要给它 jquery
作为别名。
来自Backbone wiki :
Using WebPack, the resolve.alias configuration option can be used:
{ context: __dirname + "/app", entry: "./entry", output: { path: __dirname + "/dist", filename: "bundle.js" } resolve: { alias: { "jquery": "zepto" } } }
关于javascript - Webpack 在没有 jQuery 的情况下无法加载主干,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40116663/