3

Hi I am using node 16 for my project. When I deploy my app in Azure App Service. When I start running the application, I get the following error:

import myToken from './MyToken.json' assert {type: "json"};
                                      ^^^^^^
SyntaxError: Unexpected identifier
ESMLoader.moduleStrategy (node:internal/modules/esm/translators:139:18)
ESMLoader.moduleProvider (node:internal/modules/esm/loader:236:14)

Can anyone please help on it? Thank you

My node version v16.13.1. I deploy the nodejs to Azure App Service.

0

3 Answers 3

2

Importing JSON modules is an experimental feature. This feature could change at any time

Try getting Node launched with --experimental-json-modules

1

Tested with nodejs 22.3.0

import myToken from './MyToken.json' with {type: "json"};

https://nodejs.org/docs/v20.15.0/api/esm.html#import-attributes

0

Node v16.13.1, which you say in the question you were using, didn't support importing JSON files nor support the "import assertion" syntax that is being used in the code you show. That feature was added in Node v17.1.0, and your code will work fine in that version of Node.

Confusingly, as of Node v22, your code will once again fail with an almost-identical syntax error, because import assertions have now been removed from the language. The syntax to use instead - added in v18.20.0, but now the only option as of v22.0.0 - is to use the keyword with instead of assert - i.e. to write:

import myToken from './MyToken.json' with {type: "json"};

(This new syntax is named "import attributes" instead of "import assertions", but is just the same thing with a different syntax; the spec proposal itself got renamed!)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.