1

The context is that I need to read the data from local memory instead of a database. I want to switch to a different memory object when I am testing. To achieve this, my plan is for my function that fetches the data to check the NODE_ENV, if it is "test", then import the data from directory A, else directory B.

However, doing if/else then import throws SyntaxError of unexpected identifier. Is there some way to do conditional import in NodeJS, or a more elegant way to achieve my purpose in general?

Samson
  • 1,336
  • 2
  • 13
  • 28
  • It sounds like you're using [ESM](https://nodejs.org/api/esm.html). The module specifier string of an `import` declaration must be a string *literal*, it can't be the result of an expression. Instead, see the answers to the linked question, you can do a *dynamic import* via the [`import()` psuedo-function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#dynamic_imports), which returns a promise of the module's module namespace object: `const { someExport} = await import(condition ? "this" : "that");` or ... – T.J. Crowder Jan 09 '22 at 10:56
  • ... `const { default as someName} = await import(condition ? "this" : "that");` for the default export. *(I realized there **had** to be a previous answered version of this question, so I found it and deleted my answer, but figured I should copy it into a comment in case you wanted it for something. Happy coding!)* – T.J. Crowder Jan 09 '22 at 10:57
  • Hi T.J., I don't get it. The duplicate answer you linked me as duplicate was completely different from the answer you gave me. – Samson Jan 09 '22 at 10:58
  • To be more specific I did find it but it was the fourth answer from the top instead of the most-voted and accepted answer. Unless that is actually a better answer? – Samson Jan 09 '22 at 11:00
  • That's the problem with questions when the answer changes over time. :-| Felix's answer was a good answer in 2015, hence the votes, but for the past 2-3 years the correct answer is dynamic `import()`. – T.J. Crowder Jan 09 '22 at 11:09
  • I thought so too, it seems much more elegant! In this case though perhaps there is value in keeping our question and answer here? – Samson Jan 09 '22 at 11:16
  • 1
    SO's model depends on duplicates pointing to canonical answers. There's an "outdated answers project" that is trying to address this kind of issue. In the meantime, I suggest upvoting the `import()` answer there. :-) – T.J. Crowder Jan 09 '22 at 11:19
  • @T.J.Crowder sorry to bother you again. This worked for me, but when my friend tried to run my code on his machine, it gave him "SyntaxError: Unexpected reserved word at Loader.moduleStrategy (internal/modules/esm/translators.js:122:18)" Do I need to wrap it in async or something? Any idea why it may not work on some machines? – Samson Jan 10 '22 at 02:33
  • 1
    It should Just Work™ **if** they have a vaguely up-to-date Node.js. Dynamic import [is supported](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#browser_compatibility) by Node.js v12+ but behind a command-line flag, v13.2+ without a flag. – T.J. Crowder Jan 10 '22 at 07:33

0 Answers0