10

When this line is executed:

import stats from `./${process.env.STATS}`

the error below is reported:

Parsing error: Unexpected token `

The module can be loaded successfully with the expression:

const stats = require(`./${process.env.STATS}`);

The import statement seems to require a regular string as it works with the statement:

import stats from './statsdir'

where './statsdir' is the value of process.env.STATS.

Why does the error occur?

gnerkus
  • 10,642
  • 6
  • 45
  • 69

1 Answers1

16

Why does the error occur?

It seems you found the answer yourself:

The import statement seems to require a regular string

Exactly. import needs a string literal. It import location cannot be dynamic.

Related: ES6 variable import name in node.js?

Community
  • 1
  • 1
Felix Kling
  • 756,363
  • 169
  • 1,062
  • 1,111