我正在使用webpack和two.js编写一个web应用程序。为了完整起见,这些是我的依赖关系:
"devDependencies": {
"jasmine-core": "^2.8.0",
"karma": "^1.7.0",
"karma-chrome-launcher": "^2.2.0",
"karma-jasmine": "^1.1.0",
"karma-webpack": "^2.0.4",
"webpack": "^3.5.5"
},
"dependencies": {
"two.js": "^0.6.0"
}我有下面的webpack.config.js
const path = require('path');
module.exports = {
entry: './myentry.js',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'out')
}
};入口文件myentry.js刚刚导入two.js
import * as two from "two.js";捆绑成功..。
库two.js是npm依赖项的一部分,因此可以在node_modules文件夹中的本地节点模块中找到它。当我继续创建包时:
webpack --config webpack.config.js它是成功的,我得到了以下输出:
Hash: 5e762def59fa65ff8487
Version: webpack 3.5.5
Time: 468ms
Asset Size Chunks Chunk Names
bundle.js 258 kB 0 [emitted] [big] main
[0] ./myentry.js 271 bytes {0} [built]
+ 1 hidden module生成的包是可用的这里。
但是..。运行失败:
因此,我在HTML页面中使用了这个包:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>My stuff</title>
<script type="module" src="./out/bundle.js"></script>
</head>
<body></body>
</html>当我在Chrome中打开页面时(我使用http-服务器来避免与CORS相关的问题),我在F12 Tools中得到了这些错误
bundle.js:1674 Uncaught TypeError: Cannot read property 'Backbone' of undefined
at bundle.js:1674
at Object.<anonymous> (bundle.js:1831)
at __webpack_require__ (bundle.js:20)
at Object.root (bundle.js:72)
at __webpack_require__ (bundle.js:20)
at Object.defineProperty.value (bundle.js:63)
at bundle.js:66这是怎么回事?
发布于 2017-08-27 07:48:36
看起来问题在于我在HTML中引用包的方式:
<script type="module" src="./out/bundle.js"></script>这是不正确的,因为生成的包不是ES6模块:(
<script type="application/javascript" src="./out/bundle.js"></script>这使错误消失了。它实际上为其他错误打开了大门,但它们与主干完全无关。
https://stackoverflow.com/questions/45896233
复制相似问题