我无法在NX工作区内的Express应用程序中导入任何.json文件. 这是我的导入声明

import swaggerDocument from '../swagger.json';

和我的项目 struct

tsconfig.base.json
apps
|- test-app
  |- swagger.json
  |- tsconfig.json
  |- tsconfig.app.json
  |- src
    |- main.ts   <-- import statement here

这是我的tsconfig.base.json美元

{
  "compileOnSave": false,
  "compilerOptions": {
    "rootDir": ".",
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "importHelpers": true,
    "target": "es2015",
    "module": "esnext",
    "lib": ["es2020", "dom"],
    "skipLibCheck": true,
    "skipDefaultLibCheck": true,
    "baseUrl": ".",
    "paths": {
      ...paths for other modules
    },
    "strict": true
  },
  "exclude": ["node_modules", "tmp"]
}

tsconfig.json

{
  "extends": "../../tsconfig.base.json",
  "files": [],
  "include": [],
  "references": [
    {
      "path": "./tsconfig.app.json"
    },
    {
      "path": "./tsconfig.spec.json"
    }
  ],
  "compilerOptions": {
    "esModuleInterop": true,
    "resolveJsonModule": true
  }
}

tsconfig.app.json

{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "outDir": "../../dist/out-tsc",
    "module": "commonjs",
    "types": ["node"],
  },
  "exclude": ["jest.config.ts", "src/**/*.spec.ts", "src/**/*.test.ts"],
  "include": ["src/**/*.ts"]
}

那么我到底做错了什么呢?我还try 将.json文件放在/src目录中并从那里导入,但没有成功.

推荐答案

Since you are using TypeScript, you already have "resolveJsonModule": true set in your tsconfig.json, which would allow you to import JSON files when you transpile your TypeScript to JavaScript.
However, due to the current limitations with ESM in Node.js, you would still face issues at runtime.

进口断言建议仍为marked as experimental in Node 18/19/20.

OP mdakovac分到this answer分作为主要解决方案:

如果您使用的是webpack,则默认情况下会导入json文件(因为webpack&>=v2.0.0).

import config from '../config.json';

如果我根据您的用例重新调整this answer的用途:

选项1:手动读取和解析JSON文件

You can use Node.js's file system module to read the JSON file and then parse it to a JavaScript object.
Your main.ts would be:

import { readFile } from 'fs/promises';

async function main() {
  const json = JSON.parse(
    await readFile(
      new URL('../swagger.json', import.meta.url)
    )
  );
  console.log(json);
}

main();

Option 2: Leverage the CommonJS require function to load JSON files

您可以使用createRequire来构造一个CommonJS require函数,然后该函数将允许您在ECMAScript模块中加载JSON文件.

import { createRequire } from 'module';
const require = createRequire(import.meta.url);
const swaggerDocument = require('../swagger.json');

Json相关问答推荐

使用jolt删除空对象

如何在Power BI中集成API和JSON数据后将数据转换为表?

织女星-没有循环的动画条形图第二部分(实际上是织女星)

419(未知状态)使用laravel处理PUT请求

jq如何合并两个json对象

使用 jq 获取所有嵌套键和值

转换为Json时忽略内部对象中的数组

数据清理设计不良的 JSON 数据 - 需要有关最佳策略的建议

将 JSON 字符串解析为 Kotlin Android 中的对象列表(MOSHI?)

从 PySpark 中的复杂 JSON 文件中高效清除 HTML 实体

使用 jq 获取特定键的所有父键

如何在 powerapps 中将详细信息表单转换为 json?

使用 System.Text.Json 序列化记录成员

从 json 对象中过滤掉所有出现的给定属性

使用基本身份验证通过 CURL 发布 JSON

在 Rails 中使用 JSON 创建嵌套对象

Jackson 中的 readValue 和 readTree:何时使用哪个?

Jackson 动态属性名称

使用绝对或相对路径在 curl 请求中发送 json 文件

Microsoft.Net.Http 与 Microsoft.AspNet.WebApi.Client