打字稿React TS2307找不到模块json

时间:2017-04-06 16:28:13

标签: reactjs typescript webpack

我目前遇到了一个奇怪的错误。我正在尝试使用带有json-loader的导入加载json文件。

import * as ApiConfig from "../../src/conf.json";

我创建了一个typings.d.ts来定义css和json模块。

declare module "*.json" {
   const value: any;
   export default value;
}


declare module "*.css" {
    const content: any;
    export default content;
}

当我没有在我的WebStorm中打开我的typings.d.ts文件时出现此错误

Error:(1, 28) TS2307:Cannot find module '../../src/conf.json'.

但是当我打开它时,我没有这个错误。

这是我的tsconfig.json

{
  "compilerOptions": {
    "module": "commonjs",
    "target": "es2015",
    "jsx": "react",
    "sourceMap": true,
    "allowJs": true,
    "experimentalDecorators": true
  },
  "exclude": [
    "node_modules"
  ]
}

以下是我的webpack.config.js中的加载器

loaders: [
            {
                exclude: [
                    /\.html$/,
                    /\.(js|jsx)$/,
                    /\.css$/,
                    /\.json$/,
                    /\.svg$/
                ],
                loader: 'url-loader',
                query: {
                    limit: 10000,
                    name: 'static/media/[name].[hash:8].[ext]'
                }
            },
            {
                test: /\.js$/,
                loader: 'babel-loader',
                include: [ path.resolve(__dirname, '../') ],
                exclude: [ path.resolve(__dirname, 'bundles') ],
                query: {
                    presets: ['react', 'es2015'],
                    plugins: ['transform-decorators-legacy'],
                    cacheDirectory:true
                },
                exclude: /(node_modules|bower_components)/
            },
            {
                test: /\.css$/,
                include: [ path.resolve(__dirname, '../') ],
                exclude: [ path.resolve(__dirname, 'bundles') ],
                loader: "style-loader!css-loader"
            },
            {
                test: /\.svg$/,
                loader: "svg-loader"
            },
            {
                test: /\.json$/,
                loader: "json-loader"
            }
        ]

由于

2 个答案:

答案 0 :(得分:1)

这实际上是WebStorm中的一个错误,它已在2017.1.1版本中得到修复。

答案 1 :(得分:0)

使用 var 代替导入

var json = require('../../src/conf.json');
相关问题