【发布时间】:2021-03-10 01:07:24
【问题描述】:
我正在尝试使用 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)
我正在关注documentation。我的语法似乎没问题。出现这种错误的原因是什么?
我也试过import * as keys from './prod';。这给了我SyntaxError: Unexpected token '*'
我的节点版本是 14.15.4。我在 package.json 中使用"type": "module",。
【问题讨论】:
-
我认为如果您将文件的扩展名重命名为
.mjs它应该可以工作。
标签: node.js ecmascript-6