简体   繁体   English

尝试在另一个文件 nodejs 文件中导入导出的变量时出错

[英]Get an error when trying to import the exported variable in another file nodejs file

I have a file called TemplateGenerator and I have:我有一个名为 TemplateGenerator 的文件,我有:

export const test= {
    "type": "bundle"
}
export const bundle= {
    "type": "bundle",
    "id": "bundle--bb7142cc-83d5-4feb-98c7-53997d25c368",
    "objects": [generateObservedDataObj(),generateObservedDataObj(),generateObservedDataObj()]
}

So when I try to import it in another js file called Results like this:因此,当我尝试将其导入另一个名为 Results 的 js 文件时,如下所示:

import {bundle} from '../utils/TemplateGenerator';

I get我得到

 SyntaxError: The requested module 'file:///app/src/results/Results.js' does not provide an export named 'default'
     at Object.<anonymous> (/app/src/routers/Routes.js:1)
    at Generator.next (<anonymous>)
     at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10)
     at Object.<anonymous> (/app/src/server.js:1)
     at Generator.next (<anonymous>)

Am I missing anything?我错过了什么吗?

It should be:它应该是:

export default const ...

since it is the only function/variable exported.因为它是唯一导出的函数/变量。

First, declare the variable, then export it首先声明变量,然后导出

const bundle= {
    "type": "bundle",
    "id": "bundle--bb7142cc-83d5-4feb-98c7-53997d25c368",
    "objects": [generateObservedDataObj(),generateObservedDataObj(),generateObservedDataObj()]
}

export default bundle;

This error:这个错误:

 SyntaxError: The requested module 'file:///app/src/results/Results.js' does not provide an export named 'default'

Probably indicates that you import Result.js wrong.大概说明你导入Result.js误。 Are you sure that import {bundle} from '../utils/TemplateGenerator' actually is the root of your problem?您确定import {bundle} from '../utils/TemplateGenerator'实际上是您问题的根源吗? Did you maybe import Result like this: import { default } from './wherever/Result.js' ?您是否可能像这样导入Resultimport { default } from './wherever/Result.js'

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

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