Typescript-react-starter: import static json files

Created on 12 May 2017  Â·  2Comments  Â·  Source: microsoft/TypeScript-React-Starter

The webpack setup in stock create-react-app supports this:

import customers from ‘./customers.json’

This is a very handy thing and I have come to rely upon it.

Most helpful comment

If anyone else is running into this, the following worked for me:

const data = require('./data.json');

As far as I can tell, the TypeScript compiler doesn't know how to handle JSON out of the box. Webpack (which this repo uses) does, so switching to a variable declaration with require() lets webpack take over and load the JSON as normal.

All 2 comments

@StokeMasterJack
I guess, this repo is a show case of the react / redux / typescript

The webpack configuration is done in create-react-app (from facebook incubator) and / or react-scripts-ts
you might want to close the issue here and create it around this one:

https://github.com/wmonk/create-react-app-typescript

If anyone else is running into this, the following worked for me:

const data = require('./data.json');

As far as I can tell, the TypeScript compiler doesn't know how to handle JSON out of the box. Webpack (which this repo uses) does, so switching to a variable declaration with require() lets webpack take over and load the JSON as normal.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Arelav picture Arelav  Â·  4Comments

BenjaminWatts picture BenjaminWatts  Â·  6Comments

haani104 picture haani104  Â·  5Comments

wongjiahau picture wongjiahau  Â·  9Comments

nikhilyeole1 picture nikhilyeole1  Â·  9Comments