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

查看:31
本文介绍了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:无法分配给只读属性exports"的对象#<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天全站免登陆