【问题标题】:How to resolve TypeError on hasOwnProperty in index.tsx如何解决 index.tsx 中 hasOwnProperty 上的 TypeError
【发布时间】:2017-08-02 15:04:59
【问题描述】:

我正在尝试为打字稿项目设置一个简单的框架,但无法编译。任何帮助将不胜感激。

我收到以下错误:

in ./app/index.tsx
Module build failed: TypeError: Cannot convert undefined or null to object
at hasOwnProperty (<anonymous>)
at Object.hasProperty (C:\Users\sjb3\docs\NIST-CT\StriDE\node_modules\typescript\lib\typescript.js:2229:31)
at parseConfig (C:\Users\sjb3\docs\NIST-CT\StriDE\node_modules\typescript\lib\typescript.js:71815:16)
at C:\Users\sjb3\docs\NIST-CT\StriDE\node_modules\typescript\lib\typescript.js:71721:22
at Object.parseJsonConfigFileContent (C:\Users\sjb3\docs\NIST-CT\StriDE\node_modules\typescript\lib\typescript.js:71735:11)
at readConfigFile (C:\Users\sjb3\docs\NIST-CT\StriDE\node_modules\awesome-typescript-loader\src\instance.ts:324:33)
at Object.ensureInstance (C:\Users\sjb3\docs\NIST-CT\StriDE\node_modules\awesome-typescript-loader\src\instance.ts:101:9)
at compiler (C:\Users\sjb3\docs\NIST-CT\StriDE\node_modules\awesome-typescript-loader\src\index.ts:47:22)
at Object.loader (C:\Users\sjb3\docs\NIST-CT\StriDE\node_modules\awesome-typescript-loader\src\index.ts:16:18)
@ multi webpack-hot-middleware/client react-hot-loader/patch ./app/index.tsx

以下是相关代码:

index.tsx

import React from 'react'
import ReactDOM from 'react-dom';
import { AppContainer } from 'react-hot-loader';

import App from './App';

ReactDOM.render(
    <AppContainer>
        <App />
    </AppContainer>,
    document.getElementById('stride-root')
)

if (module.hot) {
    module.hot.accept('./App', () => {
        const NextApp = require('./App').default;
        ReactDOM.render(
            <AppContainer>
                <NextApp/>
            </AppContainer>,
            document.getElementById('stride-root')
        );
    });
}

App.tsx

import React, { Component } from 'react';

class App extends Component<void,void> {

    render() {
        return (
            <div> Hello World. </div>
        );
    }
}

export default App;

如果有帮助,我还将包含我的 webpack 和 tsconfig 文件。这些取自现有项目,因此请随时提出有用的修改或简化建议。谢谢!

webpack.config.js

var path = require('path');
var webpack = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');

module.exports = {
    devtool: 'eval-source-map',
    entry : [
        'webpack-hot-middleware/client',
        // 'babel-polyfill',
        'react-hot-loader/patch',
        path.join(__dirname, 'app/index.tsx')
    ],

    output : {
        path : path.resolve(__dirname, '/'),
        filename : '[name].js',
        publicPath: '/'
    },
    resolve: {
        extensions : [".ts", ".tsx", ".js", ".jsx"]
    },
    plugins: [
        new webpack.HotModuleReplacementPlugin(),
        // new webpack.LoaderOptionsPlugin({
        //   debug: true,
        // }),
        new HtmlWebpackPlugin({
            template: 'app/index.html',
            inject: true,
            filename: 'index.html'
        }),
        new ExtractTextPlugin("main.css"),
        new webpack.NoEmitOnErrorsPlugin(),


    ],
    module: {
        rules: [
            {
                test: /\.(j|t)sx?$/,
                exclude: /node_modules/,
                loader :'awesome-typescript-loader'
            },
            {
                test: /\.scss$/,
                exclude: /node_modules/,
                use : [
                    "style-loader",
                    { loader: 'css-loader', options: { sourceMap: true } },
                    { loader: 'sass-loader', options: { sourceMap: true } }
                ]
            },
            {
                enforce: "pre",
                test : /\js$/,
                loader: "source-map-loader"
            },
        ]
    },
    devtool : "source-map"
}

tsconfig.json:

{
    "compilerOptions": {
        "outDir": "./dist/",        // path to output directory
        "sourceMap": true,          // allow sourcemap support
        "strictNullChecks": true,   // enable strict null checks as a best practice
        "module": "es6",            // specifiy module code generation
        "jsx": "react",             // use typescript to transpile jsx to js
        "target": "es5",            // specify ECMAScript target version
        "allowJs": true,             // allow a partial TypeScript and JavaScript codebase
        "allowSyntheticDefaultImports" : true,      // Allows simple import statements
    },
    "include": [
        "./src/"
    ]
}

【问题讨论】:

标签: javascript typescript webpack


【解决方案1】:

我遇到了同样的问题。

我发现错误在tsconfig.json 文件中。它在baseUrl 中声明了错误的路径。

现在应用程序可以工作了。

【讨论】:

    猜你喜欢
    • 2015-06-29
    • 2014-02-20
    • 2011-09-10
    • 2021-07-10
    • 1970-01-01
    • 2019-12-25
    • 2021-06-29
    • 2019-07-10
    • 1970-01-01
    相关资源
    最近更新 更多