使用 TypeScript 在节点应用程序中导入 JSON 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44929021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 04:41:13  来源:igfitidea点击:

Import JSON file in node application with TypeScript

typescripttypescript-typings

提问by Patrick P.

I'm really getting crazy because I can't find a solution for this. What I want to archive is to import a JSON file with a configuration into my TypeScript file. I've learned that I need a declaration file. So I've added a file json-loader.d.ts into my project. I also tried it at several levels (root, typings folder, custom_typings folder) because this are the solutions I've found. The file content looks like this:

我真的快疯了,因为我找不到解决方案。我想要存档的是将带有配置的 JSON 文件导入到我的 TypeScript 文件中。我了解到我需要一个声明文件。所以我在我的项目中添加了一个文件 json-loader.d.ts 。我还在多个级别(根目录、typings 文件夹、custom_typings 文件夹)尝试了它,因为这是我找到的解决方案。文件内容如下所示:

declare module "json!*" {
    let json: any;
    export = json;
}

declare module "*.json" {
    const value: any;
    export default value;
}

But the compiler still tells me, that it is not possible to import the JSON file, because it isn't a module. So, how is the compiler getting aware, that there is such a declaration file?

但是编译器仍然告诉我,无法导入 JSON 文件,因为它不是模块。那么,编译器是如何知道有这样一个声明文件的呢?

I already tried to modify my tsconfig.json like this:

我已经尝试像这样修改我的 tsconfig.json:

{
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs",
    "outDir": "dist",
    "typeRoots": [
      "./node_modules/@types",
      "./typings"
    ]
  },
  "include": [
    "src/**/*.ts"
  ],
  "exclude": [
    "node_modules"
  ]  
}

But it still doesn't work. Any suggestions?

但它仍然不起作用。有什么建议?

Thanks!

谢谢!

回答by tony

Examples on the web that use declare module "*.json"work because Webpack is configured to load JSON files.

Web 上的示例declare module "*.json"之所以有效,是因为 Webpack 被配置为加载 JSON 文件。

If you don't want to bother with that you can use varinstead of import:

如果你不想打扰,你可以使用var代替import

var json = require('../config.json');

var json = require('../config.json');

as suggested in this answer

正如这个答案中所建议的

回答by Erik Vullings

I've successfully used the following to import my package.jsoninside a CLI program (so I can re-use the version, license and description information in my help page).

我已经成功地使用以下内容将我的内容导入package.json到 CLI 程序中(因此我可以在帮助页面中重复使用版本、许可证和描述信息)。

declare module '*.json' {
  const foo: {
    name: string;
    version: string;
    author: string;
    license: string;
    description: string;
  };
  export = foo;
}

Alternatively, you can import other files containing interface descriptions etc., but you need to put the imports insidethe module declaration, e.g.

或者,您可以导入包含接口描述等的其他文件,但您需要将导入放在模块声明中,例如

declare module 'ormconfig.json' {
  import { Connection } from "typeorm";

  const foo: Connection[];
  export = foo;
}

回答by JGFMK

Have you done something like add a SystemJS script in your web page to do module loading - index.html etc. I used https://cdnjs.com/libraries/systemjsand iirc you need https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.20.14/system.src.jsSystem.JS is the module loader.. Has to be first script imported. And then you need something like this:

您是否做过类似在网页中添加 SystemJS 脚本以进行模块加载 - index.html 等的操作。我使用了https://cdnjs.com/libraries/systemjs和 iirc,您需要https://cdnjs.cloudflare.com/ ajax/libs/systemjs/0.20.14/system.src.jsSystem.JS 是模块加载器.. 必须是第一个脚本导入。然后你需要这样的东西:

    <script
   src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.20.14/system.src.js "/>
    <script>
       System.config({
         defaultJSExtension:true
       });
      System.import('pathtoscriptwithoutdotjsatend');
   </script>