12

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
  • 11,357
  • 6
  • 47
  • 71

1 Answers1

18

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
  • 795,719
  • 175
  • 1,089
  • 1,143