简体   繁体   English

module.exports 不包含默认导出

[英]module.exports does not contain a default export

I am trying to export my theme in my react app using `module.exports =我正在尝试使用 `module.exports = 在我的反应应用程序中导出我的主题

 module.exports = {
   defaultFontColor: "#282828",
   name: "Planswell",
   primary: primary,
   deemphasizedPrimary: "#e7f6ee",
   secondary: "#E4A432",
   danger: "#DF5F2B"...}` 

in my file whatever.js , and i try to import it in another file using import whatever from "themes/whatever.js";在我的文件whatever.js中,我尝试使用import whatever from "themes/whatever.js";

All was working well, but i updated Babel, and am now getting the error Attempted import error: 'themes/whatever.js' does not contain a default export (imported as 'whatever').一切运行良好,但我更新了 Babel,现在出现错误Attempted import error: 'themes/whatever.js' does not contain a default export (imported as 'whatever').

What changed with Babel that caused this error? Babel 发生了什么变化导致了这个错误? And how do I fix it?我该如何解决?

If the only export in your whatever.js is如果whatever.js中的唯一导出是

module.exports = {mod1, mod2, ...}

Then, assuming whatever is actually a module in your file, you should have never been able to import it using然后, whatever您的文件中实际上是一个模块,您应该永远无法使用

import whatever from "themes/whatever.js";

The only way that would be possible is if in your whatever.js you did:唯一可能的方法是在你的whatever.js中你做了:

export default whatever;

Otherwise, you will have to destructure the import like so:否则,您将不得不像这样解构导入:

import {whatever, mod1, mod2, ...} from "themes/whatever.js";

Again, all this assumes that whatever is actually a module inside your whatever.js file eg const whatever = () => {... .同样,所有这whatever.js whatever中实际上是一个模块,例如const whatever = () => {... You don't make that part clear.你没有把那部分说清楚。

The error you're getting should help you guide your way.您遇到的错误应该可以帮助您指导自己的方式。 When using module.exports = {...} syntax, the default export is the object.使用module.exports = {...}语法时,默认导出为 object。

You should try importing specific exported properties of the module such as import { someModule } from 'themes/whatever.js' .您应该尝试导入模块的特定导出属性,例如import { someModule } from 'themes/whatever.js' Optionally you can use您可以选择使用

import * as whatever from 'themes/whatever.js'

// Use it

whatever.myFunction()

Babel is pretty complex tool so I would check from which version you upgraded to and then looked at the change log to see what has changed. Babel 是一个非常复杂的工具,所以我会检查你升级到哪个版本,然后查看更改日志以了解发生了什么变化。 Babel has plethora of presets and plugins so it could be any combination, sorry no simple answer here. Babel 有大量的预设和插件,所以它可以是任意组合,抱歉这里没有简单的答案。 To me it seems like perhaps you're using some different type of module.在我看来,您可能正在使用某种不同类型的模块。

Maybe you are using @babel/preset-env with combination of browserslist settings and you transpile to ES6 modules?也许您正在使用@babel/preset-envbrowserslist设置的组合,并且您转换为 ES6 模块?

in your whatever.js you have exporting the as an object that contain your component so you have to export it like this在您的whatever.js中,您将导出为包含您的组件的object,因此您必须像这样导出它

import {whatever} from "themes/whatever.js";

for your example to work you have to export your component without brackets为了使您的示例正常工作,您必须导出不带括号的组件

export {whatever}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM