2017-02-24 97 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;