【问题标题】:deno import JSON file as moduledeno 导入 JSON 文件作为模块
【发布时间】:2020-09-06 11:14:58
【问题描述】:

我有一个导入 json 的简单文件:

ma​​in.ts

import json from './file.json'

但是,deno 在导入 json 文件时会抛出以下错误:

$ 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)

文件路径正确且文件是有效的 JSON。 Typescript compiler should allow this by default。

我也尝试显式启用resolveJsonModule:

tsconfig.json

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

并使用配置运行它,但仍然得到相同的错误:

$ 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)

这里有什么问题?

【问题讨论】:

    标签: json module deno


    【解决方案1】:

    从 Deno 1.17 开始,JSON 现在可以再次导入 ESM。现在必须使用导入断言:

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

    有关详细信息,请参阅https://examples.deno.land/importing-json。

    【讨论】:

      【解决方案2】:

      作为 Afeef 答案的替代方案,由于 JSON 文件是有效的对象文字,您可以将 export default 添加到它并将扩展名更改为 .js。

      来自 settings.json

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

      到 settings.js

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

      现在你可以使用import

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

      除了更短之外,好处是您不需要--allow-read 访问权限

      【讨论】:

        【解决方案3】:

        在发布 deno 1.0 之前,删除了对读取 json 文件的以下线程支持

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

        但是,您可以使用以下语法来读取 json 文件

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

        或

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

        另外,请务必使用--allow-read 标志运行包含上述代码的文件。否则会出现权限被拒绝错误

        deno run --allow-read index.ts
        

        【讨论】:

          猜你喜欢
          • 2020-12-03
          • 1970-01-01
          • 1970-01-01
          • 2020-11-01
          • 1970-01-01
          • 2018-06-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多