2017-02-24 66 views
1

我正在开发一个带有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有什么不妥?

+1

的可能的复制[使用Node.js的要求与ES6导入/导出](http://stackoverflow.com/questions/31354559/using-node-js-require-vs-es6-import-export ) –

+0

@MatthewHerbst改变了主要问题。 – juniorgarcia

回答

4

您不能混合使用importmodule.exports。在import世界中,你需要导出东西。

// Change this 
module.exports = foo; 

// To this 
export default foo;