首页 > 解决方案 > 升级到 Babel 7 后,无法分配给对象 ERROR 的只读属性“exports”

问题描述

在我的反应应用程序中升级到 Babel 7 后出现以下错误。如果我需要包含更多详细信息,请告诉我!

我已经尝试过替换的解决方案,module.exports但这export default会带来其他错误。

错误:

module.exports = {
^

TypeError: Cannot assign to read only property 'exports' of object '#<Object>'

  23 | const host = process.env.RAZZLE_HOST || `http://localhost:${port}`;
  24 | 
> 25 | module.exports = {
  26 |   port,
  27 |   host,
  28 |   isAdminSite,

config.js 上下文:

const host = process.env.RAZZLE_HOST || `http://localhost:${port}`;

module.exports = {
  port,
  host,
  isAdminSite,
  adminSiteUrl,
  userSiteUrl,

导入如:

import _ from 'lodash';
import moment from 'moment-timezone';
import splitLinks from '../helpers/split-links';
import { host } from '../config';
import MatchView from './match_view';```

标签: node.jsreactjsbabeljs

解决方案


基本上,您可以混合requireexport.

export {
 someObj
}
//and then import by

require('./file.js')

但你不能混用:importmodule.exports它是行不通的。

所以,在你的情况下,module.exports需要是exportimport需要是require


推荐阅读