deno import JSON file as module
Asked Answered
C

3

20

I have a simple file that imports a json:

main.ts

import json from './file.json'

However, deno throws the following error when importing a json file:

$ deno run main.ts
Compile file:///home/path/to/project/main.ts
error: Uncaught TypeError: Cannot resolve extension for "file:///home/path/file.json" with mediaType "Json".
    at getExtension ($deno$/compiler.ts:218:13)
    at new SourceFile ($deno$/compiler.ts:263:22)
    at Function.addToCache ($deno$/compiler.ts:339:16)
    at processImports ($deno$/compiler.ts:743:31)
    at async processImports ($deno$/compiler.ts:753:7)
    at async compile ($deno$/compiler.ts:1316:31)
    at async tsCompilerOnMessage ($deno$/compiler.ts:1548:22)
    at async workerMessageRecvCallback ($deno$/runtime_worker.ts:74:9)

The file path is correct and the file is a valid JSON. The Typescript compiler should allow this by default.

I also tried to explicitly enable resolveJsonModule:

tsconfig.json

{
  "compilerOptions": {
    "resolveJsonModule": true
  },
  "include": [
    "**/*"
  ]
}

and run it with the config but still get the same error:

$ deno run main.ts --config=tsconfig.json
Compile file:///home/path/to/project/main.ts
error: Uncaught TypeError: Cannot resolve extension for "file:///home/path/file.json" with mediaType "Json".
    at getExtension ($deno$/compiler.ts:218:13)
    at new SourceFile ($deno$/compiler.ts:263:22)
    at Function.addToCache ($deno$/compiler.ts:339:16)
    at processImports ($deno$/compiler.ts:743:31)
    at async processImports ($deno$/compiler.ts:753:7)
    at async compile ($deno$/compiler.ts:1316:31)
    at async tsCompilerOnMessage ($deno$/compiler.ts:1548:22)
    at async workerMessageRecvCallback ($deno$/runtime_worker.ts:74:9)

What's wrong here?

Conlon answered 20/5, 2020 at 7:13 Comment(0)
B
24

Since Deno 1.17 JSON can now once again be imported in ESM. Import assertions must now be used:

import data from "./file.json" assert { type: "json" };
console.log(data);

For more info, see https://examples.deno.land/importing-json.

Beret answered 12/1, 2022 at 13:15 Comment(2)
Hi! Is there other file types possible? Like text? Is there any doc on that?Persona
I could find the docs sorry ! github.com/tc39/proposal-import-assertionsPersona
W
25

As per the following thread support for reading json files was removed just before shipping deno 1.0

https://github.com/denoland/deno/issues/5633

However, you can use the following syntax for reading a json file

Deno.readTextFile('./file.json').then(data => {
    console.log(JSON.parse(data))
})

or

const data = JSON.parse(Deno.readTextFileSync('./file.json'));

Also, be sure to run the file containing above code with --allow-read flag. Otherwise you will ge a permission denied error

deno run --allow-read index.ts
Wynd answered 20/5, 2020 at 7:51 Comment(0)
B
24

Since Deno 1.17 JSON can now once again be imported in ESM. Import assertions must now be used:

import data from "./file.json" assert { type: "json" };
console.log(data);

For more info, see https://examples.deno.land/importing-json.

Beret answered 12/1, 2022 at 13:15 Comment(2)
Hi! Is there other file types possible? Like text? Is there any doc on that?Persona
I could find the docs sorry ! github.com/tc39/proposal-import-assertionsPersona
S
15

As an alternative to Afeef's answer, since a JSON file is a valid object literal, you can add export default to it and change the extension to .js.

from settings.json

{
   "something": {
      "foo": "bar"
   } 
}

to settings.js

export default {
   "something": {
      "foo": "bar"
   } 
}

And now you can can use import

import settings from './settings.js'
console.log(typeof settings) // object
constole.log(settings.something.foo) // bar

The upside, aside from being shorter, is that you don't need --allow-read access

Sectarianism answered 20/5, 2020 at 12:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.