webpack:在同一模块导入+ module.exports导致错误
问题描述:
我正在开发一个带有webpack的网站。当我有这样的代码:webpack:在同一模块导入+ module.exports导致错误
import $ from 'jquery';
function foo() {};
module.exports = foo;
我得到了错误Uncaught TypeError: Cannot assign to read only property 'exports' of object '#<Object>'
。
原来,import $ from 'jquery'
更改为var $ = require('jquery')
不会导致任何错误。
为什么使用module.exports导入会导致此错误?反而使用require有什么不妥?
答
您不能混合使用import
和module.exports
。在import
世界中,你需要导出东西。
// Change this
module.exports = foo;
// To this
export default foo;
的可能的复制[使用Node.js的要求与ES6导入/导出](http://*.com/questions/31354559/using-node-js-require-vs-es6-import-export ) –
@MatthewHerbst改变了主要问题。 – juniorgarcia