简体   繁体   English

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

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

I am trying to set up a simple frame for a typescript project and I can't get it to compile. 我正在尝试为打字稿项目设置一个简单的框架,但无法编译它。 Any help would be appreciated. 任何帮助,将不胜感激。

I receive the following error: 我收到以下错误:

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

Here is the relevant code: 以下是相关代码:

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 App.tsx

import React, { Component } from 'react';

class App extends Component<void,void> {

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

export default App;

In case it helps, I will also include my webpack and tsconfig files. 如果有帮助,我还将包括我的webpack和tsconfig文件。 These are taken from an existing project, so please feel free to suggest useful modifications or simplifications. 这些取自现有项目,因此请随时提出有用的修改或简化建议。 Thanks! 谢谢!

webpack.config.js 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: 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/"
    ]
}

I had the same issue. 我遇到过同样的问题。

I found that the error was in tsconfig.json file. 我发现错误在于tsconfig.json文件中。 It declares a wrong path in baseUrl . 它在baseUrl声明了错误的路径。

Now application works. 现在应用程序可以正常工作了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何修复解析错误:index.tsx 中的意外标记,预期“,”? - How to fix Parsing error: Unexpected token, expected "," in index.tsx? ReactJS:将 index.js 转换为 index.tsx - 未找到模块:错误:无法解析“./App” - ReactJS: Converting index.js to index.tsx - Module not found: Error: Can't resolve './App' package react-router 中的 index.tsx 有 152 个问题 - index.tsx in package react-router has 152 problems &#39;Router&#39; 不能用作 JSX 组件。 在我在 index.tsx 中添加组件 Msal 组件和在 app.tsx 中添加 PageLayout 之前,它工作正常 - 'Router' cannot be used as a JSX component. It was working fine until I added the component Msal componant in index.tsx and PageLayout in app.tsx hasOwnProperty未捕获TypeError - hasOwnProperty Uncaught TypeError 如何解决类型“void”不可分配给 TSX 中的类型“ReactNode”问题 - How to resolve Type 'void' is not assignable to type 'ReactNode' issue in TSX TSX React - 如何使用多个 JSON 对象索引 JSON API 数据 - TSX React - How to index JSON API Data with multiple JSON Objects 如何使用hasOwnProperty检查属性? - How to check property with hasOwnProperty? {} .hasOwnProperty()如何工作? - How {}.hasOwnProperty() works? 类型错误:无法读取未定义的属性“hasOwnProperty” - TypeError: Cannot read property 'hasOwnProperty' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM