Node.js-SyntaxError:意外的令牌';{';

Node.js-SyntaxError:意外的令牌';{';,node.js,ecmascript-6,Node.js,Ecmascript 6,我正在尝试使用ES6语法。我的代码是这样的 if(process.env.NODE_ENV === 'production') { import { googleClientID,googleClientSecret,mongoURI,cookieKey } from './prod'; } else { import { googleClientID,googleClientSecret,mongoURI,cookieKey } from './dev'; } expor

我正在尝试使用ES6语法。我的代码是这样的

if(process.env.NODE_ENV === 'production') {
    import { googleClientID,googleClientSecret,mongoURI,cookieKey  } from './prod';
} else {
    import { googleClientID,googleClientSecret,mongoURI,cookieKey  } from './dev';
}

export { googleClientID,googleClientSecret,mongoURI,cookieKey  }
这显示了以下错误

[0] [nodemon] restarting due to changes...
[0] [nodemon] starting `node index.js`
[0] file:///C:/Projects/emaily/config/keys.js:2
[0]     import { googleClientID,googleClientSecret,mongoURI,cookieKey  } from './prod';
[0]            ^
[0]
[0] SyntaxError: Unexpected token '{'
[0]     at Loader.moduleStrategy (internal/modules/esm/translators.js:145:18)
我正在遵循。我的语法似乎还可以。出现这种错误的原因是什么

我还尝试将“/prod”中的“
import*”作为密钥;
。这给了我
SyntaxError:Unexpected-token'*”


我的节点版本是14.15.4。我使用的是package.json中的
“type:”module“,

导入只能处于最顶层。它们被提升(首先是所有其他非
导入
语句),不能在块或条件中

你需要像这样的东西

import * as prod from './prod';
import * as dev from './dev';

const obj = process.env.NODE_ENV === 'production' ? prod : dev;
const { googleClientID, googleClientSecret, mongoURI, cookieKey } = obj;

export { googleClientID, googleClientSecret, mongoURI, cookieKey }

导入只能处于最顶层。它们被提升(首先是所有其他非
import
语句),不能是块或条件

你需要像这样的东西

import * as prod from './prod';
import * as dev from './dev';

const obj = process.env.NODE_ENV === 'production' ? prod : dev;
const { googleClientID, googleClientSecret, mongoURI, cookieKey } = obj;

export { googleClientID, googleClientSecret, mongoURI, cookieKey }

我认为,如果您将文件扩展名重命名为
.mjs
,它应该可以工作。我认为,如果您将文件扩展名重命名为
.mjs
,它应该可以工作。