Ts-loader: how can i load .json in typescript file?

Created on 4 Oct 2015  路  3Comments  路  Source: TypeStrong/ts-loader

webpack config:

{
    module: {
        loaders: [
            {test: /\.json$/, loader: 'json'},
            {test: /\.ts$/, loader: 'ts-loader'}
        ]
    },
    externals: {
        THREE: 'THREE'
    }
}

my directory:

  • src

    • entry.ts

    • ball.json

entry.ts:

/// <reference path="../typings/threejs/three.d.ts" />
import ball from './ball.json';

//....

but when i run the webpack, it will report
ERROR in ./src/entry.ts
(5,20): error TS2307: Cannot find module './ball.json'.

question

Most helpful comment

.json can be imported when I using babel-loader so I just simply think ts-loader will be the same.

#30 (comment) work for me.

thank you very much.

All 3 comments

Don't use import. See the section in the README on loading assets and https://github.com/TypeStrong/ts-loader/issues/30#issuecomment-128456710

.json can be imported when I using babel-loader so I just simply think ts-loader will be the same.

#30 (comment) work for me.

thank you very much.

@xosuperpig Using import is specifically a TypeScript feature to import type information. When you use const, let or var it will work just like babel does.

Was this page helpful?
0 / 5 - 0 ratings