[英]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.