簡體   English   中英

如何在index.tsx中的hasOwnProperty上解決TypeError

[英]How to resolve TypeError on hasOwnProperty in index.tsx

我正在嘗試為打字稿項目設置一個簡單的框架,但無法編譯它。 任何幫助,將不勝感激。

我收到以下錯誤:

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/"
    ]
}

我遇到過同樣的問題。

我發現錯誤在於tsconfig.json文件中。 它在baseUrl聲明了錯誤的路徑。

現在應用程序可以正常工作了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM