ES6 variable import name in node.js?

2019-01-02 17:58发布

is it possible to import something into module providing variable name while using ES6 import?

I.e. I want to import some module at a runtime depending on values provided in a config:

import something from './utils/' + variableName;

7条回答
还给你的自由
2楼-- · 2019-01-02 18:53

In addition to Felix's answer, I'll note explicitly that this is not currently allowed by the ECMAScript 6 grammar:

ImportDeclaration :

  • import ImportClause FromClause ;

  • import ModuleSpecifier ;

FromClause :

  • from ModuleSpecifier

ModuleSpecifier :

  • StringLiteral

A ModuleSpecifier can only be a StringLiteral, not any other kind of expression like an AdditiveExpression.

查看更多
登录 后发表回答