Typescript compiler error when importing json file

Ken :

So the code is simple:

calls.json

{"SERVER":{
    "requests":{
      "one":"1"
    }
} }

file.ts

import json = require('../static/calls.json');
console.log(json.SERVER);

the generated javascript is correct and when running the node js server, the console log json.SERVER prints '{ requests: { one: '1' } }', as it should.

The typescript compiler (commonjs) however, somehow does not particularly like this situation and throws: "Cannot find module '../static/calls.json'".

Ofcourse I tried writing a .d.ts file, like this:

declare module '../static/calls.json'{
    var exp:any;
    export = exp;
}

this then obviously throws: "Ambient module declaration cannot specify relative module name".

I also tried different variants, like:

declare module 'calls.json' {
    import * as json from '/private/static/calls.json';
    export = json;
}

and then requiring:

import json = require('calls.json');

None work properly and have their own little compiler errors :)

I want to use an external .json file because I use commonjs serverside and amd clientside and I want a single file for loading constants.

thoughtrepo :

Use var instead of import.

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

You're loading a JSON file, not a module, so import shouldn't be used is this case. When var is used, require() is treated like a normal function again.

If you're using a Node.js definition, everything should just work, otherwise require will need to be defined.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Error "Cannot find module" when importing a remote JSON file in TypeScript

From Dev

jest with typescript throws an error when importing a file

From

Importing json file in TypeScript

From Dev

Importing a JSON file with Typescript

From Dev

Webpack error when importing a json file

From Dev

Importing json file in typescript causing "Unexpected token" error with valid json

From Dev

Typescript always errors when importing a config.json file

From Dev

Importing JSON file in Node Typescript

From Dev

R Error importing when import JSON file using the fromJSON function

From Dev

Colon error when importing JSON file into a HTML Document

From Dev

Importing JSON file into Firebase error

From Dev

Error importing JSON file into mongoDB

From Dev

Error when importing raster file

From Dev

Typescript - Error when importing React functional component

From Dev

Not a constructor error when importing packages for React Typescript

From Dev

Remove "default" when importing JSON file in Vite+React using Typescript

From Dev

Why do I get a Swift compiler error when importing Vapor?

From Dev

angular 7 - importing a json file - compilation error

From Dev

Importing json file resulting in a parsing Error for HttpClient

From Dev

MongoDB giving error while importing JSON file

From Dev

ERROR When Importing SQL File into MySQL Database

From Dev

Error when importing CSV file into Amazon Personalize

From Dev

`error: Could not deserialize` when importing a JAR file

From Dev

Getting error when importing sql file

From Dev

An error occurs when importing css file in webpack

From Dev

Error when importing json array to Firebase and Firestore

From Dev

I want to use twilio api in my project but when I am importing the twilio in my typescript file it gives an error

From Dev

TypeScript is compiling into this failing JS file when importing a module. Why?

From Dev

importin typescript types/interfaces with svelte components triggers compiler error while importing them

Related Related

  1. 1

    Error "Cannot find module" when importing a remote JSON file in TypeScript

  2. 2

    jest with typescript throws an error when importing a file

  3. 3

    Importing json file in TypeScript

  4. 4

    Importing a JSON file with Typescript

  5. 5

    Webpack error when importing a json file

  6. 6

    Importing json file in typescript causing "Unexpected token" error with valid json

  7. 7

    Typescript always errors when importing a config.json file

  8. 8

    Importing JSON file in Node Typescript

  9. 9

    R Error importing when import JSON file using the fromJSON function

  10. 10

    Colon error when importing JSON file into a HTML Document

  11. 11

    Importing JSON file into Firebase error

  12. 12

    Error importing JSON file into mongoDB

  13. 13

    Error when importing raster file

  14. 14

    Typescript - Error when importing React functional component

  15. 15

    Not a constructor error when importing packages for React Typescript

  16. 16

    Remove "default" when importing JSON file in Vite+React using Typescript

  17. 17

    Why do I get a Swift compiler error when importing Vapor?

  18. 18

    angular 7 - importing a json file - compilation error

  19. 19

    Importing json file resulting in a parsing Error for HttpClient

  20. 20

    MongoDB giving error while importing JSON file

  21. 21

    ERROR When Importing SQL File into MySQL Database

  22. 22

    Error when importing CSV file into Amazon Personalize

  23. 23

    `error: Could not deserialize` when importing a JAR file

  24. 24

    Getting error when importing sql file

  25. 25

    An error occurs when importing css file in webpack

  26. 26

    Error when importing json array to Firebase and Firestore

  27. 27

    I want to use twilio api in my project but when I am importing the twilio in my typescript file it gives an error

  28. 28

    TypeScript is compiling into this failing JS file when importing a module. Why?

  29. 29

    importin typescript types/interfaces with svelte components triggers compiler error while importing them

HotTag

Archive