3

Can I import json file without the extension in typescript?

for example, I have file.json, and this is my typescript file: import jsonData from './file'.

But I got an error: [ts] Cannot find module './file'.

2
  • what's your module in typescript compilerOptions? Commented Oct 14, 2018 at 19:31
  • module: esnext Commented Oct 15, 2018 at 2:55

1 Answer 1

0

try "./file.json" instead of "./file"


or

read json file using fs.readFile("./file.json"), then parse it with JSON.parse()

Sign up to request clarification or add additional context in comments.

5 Comments

hahaha.. the question was how to resolve without add extension.
then.. um, use fs module for reading json file like ordinary text file,
and use JSON.parse for parsing
Pleas edit your answer and not provide additional info in comments as comments are likely to be deleted.
Why, I'm curious, do you want to don't need have the extension explicitly written?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.