webpack:同一模块中的import + module.exports导致错误 [英] webpack: import + module.exports in the same module caused error

查看:753
本文介绍了webpack:同一模块中的import + module.exports导致错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用webpack开发一个网站.当我有这样的代码时:

I'm developing a website with webpack. When I have a code like this:

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')不会引起任何错误.

Turns out that changing import $ from 'jquery' to var $ = require('jquery') don't cause any errors.

为什么使用module.exports导入会导致此错误?使用require代替有什么问题吗?

推荐答案

您不能混合使用importmodule.exports.在import世界中,您需要导出内容.

You can't mix import and module.exports. In the import world, you need to export things.

// Change this
module.exports = foo;

// To this
export default foo;

这篇关于webpack:同一模块中的import + module.exports导致错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆