0

My app worked for several months without ANY problems.

It is very easy and has only:

  1. core.mjs - main app file
  2. config.json - settings file

core.mjs imports setting with this line: import config from "./config.json";

The app launches with the command: "node --experimental-modules core.mjs"

JSON is valid.

All files are on their places.

I can run this app locally and it will work.

But after deploying this on heroku it crashes on start. Here is the log:

2020-04-28T10:50:37.750749+00:00 app[worker.1]: (node:4) ExperimentalWarning: The ESM module loader is experimental.
2020-04-28T10:50:37.769400+00:00 app[worker.1]: internal/modules/run_main.js:57
2020-04-28T10:50:37.769402+00:00 app[worker.1]:     internalBinding('errors').triggerUncaughtException(
2020-04-28T10:50:37.769402+00:00 app[worker.1]:                               ^
2020-04-28T10:50:37.769402+00:00 app[worker.1]: 
2020-04-28T10:50:37.769403+00:00 app[worker.1]: TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".json" for /app/config.json
2020-04-28T10:50:37.769403+00:00 app[worker.1]:     at Loader.defaultGetFormat [as _getFormat] (internal/modules/esm/get_format.js:70:15)
2020-04-28T10:50:37.769404+00:00 app[worker.1]:     at Loader.getFormat (internal/modules/esm/loader.js:110:42)
2020-04-28T10:50:37.769404+00:00 app[worker.1]:     at Loader.getModuleJob (internal/modules/esm/loader.js:241:31)
2020-04-28T10:50:37.769405+00:00 app[worker.1]:     at async ModuleWrap.<anonymous> (internal/modules/esm/module_job.js:44:17)
2020-04-28T10:50:37.769405+00:00 app[worker.1]:     at async Promise.all (index 1)
2020-04-28T10:50:37.769406+00:00 app[worker.1]:     at async link (internal/modules/esm/module_job.js:48:9) {
2020-04-28T10:50:37.769406+00:00 app[worker.1]:   code: 'ERR_UNKNOWN_FILE_EXTENSION'
2020-04-28T10:50:37.769406+00:00 app[worker.1]: }

FIX WITHOUT JSON

When I tried to fix I just commented import string and copy-paste json object into core.mjs like: const config = {...}; This works but ruins all the concepts.

Any ideas of what to do with this json import problem? (without database)

Nick_Rimer
  • 191
  • 1
  • 9

1 Answers1

0

One simple workaround you can do is wrap the whole JSON file in a JS file:

export default {
   ...json contents
}

and rename your file from config.json to config.js

then you can simply import it using import config from './config.js';

Now, this is not a perfect fix, you're simply exporting a POJO (which has the same syntax as a JSON), that's the only reason it works.

That said, you might run into scenarios where you cannot simply do this conversion as the original JSON file might be a configuration file that cannot be converted (package.json for example).

EDIT: This question has already been asked. More detailed answers could be found at How to import a json file in ecmascript 6?

  • Your answer is a good alternative. But the question was not "how to import a json", 'cause I've succesfully imported it and IT WORKED earlier and it works if I start my application locally. The problem is after deploying my app on heroku server where it stops to work (with the log listed). – Nick_Rimer Apr 28 '20 at 13:08