- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个带有 Typescript 的 React 项目(v. 3.6.3
),
当我构建- npm run build
我收到 typescript 运行时错误:
ERROR in [internal] INTERNAL ERROR: Cannot read property 'length' of undefined stack trace: TypeError: Cannot read property 'length' of undefined at unescapeLeadingUnderscores
LogRocket
记者,这是由
console.log
触发错误的typescipt对象在
typescrit.js
:
{
identifier: NodeObject {
pos: -1,
end: -1,
flags: 8,
modifierFlagsCache: 0,
transformFlags: 0,
parent: undefined,
kind: 149,
left: IdentifierObject {
pos: -1,
end: -1,
flags: 8,
modifierFlagsCache: 0,
transformFlags: 0,
parent: undefined,
escapedText: 'LR',
originalKeywordKind: undefined,
autoGenerateFlags: 0,
autoGenerateId: 0,
emitNode: [Object],
symbol: [SymbolObject]
},
right: IdentifierObject {
pos: -1,
end: -1,
flags: 8,
modifierFlagsCache: 0,
transformFlags: 0,
parent: undefined,
escapedText: 'LogRocket',
originalKeywordKind: undefined,
autoGenerateFlags: 0,
autoGenerateId: 0,
emitNode: [Object],
symbol: [SymbolObject]
}
}
}
tsconfig
:
{
"compilerOptions": {
// Allow importing like `import React from 'react'`
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"module": "esNext",
// Resolve modules using Node-resolution algorithm
"moduleResolution": "node",
// Set React as the JSX factory
"jsx": "react",
// Include typings from built-in lib declarations
"lib": ["es2019", "dom", "dom.iterable", "webworker"],
// Include module source maps for debugging
"sourceMap": true,
"baseUrl": ".",
"skipLibCheck": true,
"target": "ES2020"
},
"exclude": ["node_modules"]
}
TypeError: Cannot read property 'length' of undefined at unescapeLeadingUnderscores (C:\Users\user1\Development\Proj\node_modules\typescript\lib\typescript.js:13569:19) at Object.idText (C:\Users\user1\Development\Proj\node_modules\typescript\lib\typescript.js:13573:16) at typeToTypeNodeHelper (C:\Users\user1\Development\Proj\node_modules\typescript\lib\typescript.js:35286:57) at addPropertyToElementList (C:\Users\user1\Development\Proj\node_modules\typescript\lib\typescript.js:35680:59) at createTypeNodesFromResolvedType (C:\Users\user1\Development\Proj\node_modules\typescript\lib\typescript.js:35630:25) at createTypeNodeFromObjectType (C:\Users\user1\Development\Proj\node_modules\typescript\lib\typescript.js:35460:35) at createAnonymousTypeNode (C:\Users\user1\Development\Proj\node_modules\typescript\lib\typescript.js:35412:42) at typeToTypeNodeHelper (C:\Users\user1\Development\Proj\node_modules\typescript\lib\typescript.js:35320:28) at C:\Users\user1\Development\Proj\node_modules\typescript\lib\typescript.js:35114:106 at withContext (C:\Users\user1\Development\Proj\node_modules\typescript\lib\typescript.js:35155:37)
webpack.config.js
:
const path = require('path')
const {CleanWebpackPlugin} = require('clean-webpack-plugin')
const webpack = require('webpack')
const cmd = require('commander')
const outputdir = path.resolve(__dirname, 'dist')
const flat = require('flat')
const {BundleAnalyzerPlugin} = require('webpack-bundle-analyzer')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const ssr = require('./websrc/ssrRegistered.js')
const CopyPlugin = require('copy-webpack-plugin')
const WorkerPlugin = require('worker-plugin')
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin')
var HardSourceWebpackPlugin = require('hard-source-webpack-plugin')
const getEnvVars = () => {
const vars = flat({process: {env: process.env}})
Object.keys(vars).forEach(key => (vars[key] = JSON.stringify(vars[key])))
return vars
}
const htmlMinifyOpts = {
collapseWhitespace: true,
collapseBooleanAttributes: true,
minifyCSS: true,
removeComments: true,
removeRedundantAttributes: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
useShortDoctype: true
}
cmd
.option('--mode [mode]', '', process.env.NODE_ENV || 'development')
.option('--report')
.option('--debug')
.option('--page [page]', '', '') // page for dev server to open
.parse(process.argv)
const mode = cmd.mode
const opts = {
entry: {app: './websrc/app.tsx'},
output: {
filename: '[name]-[chunkhash].js',
path: outputdir,
publicPath: '/',
// show relative paths in sourcemaps
devtoolModuleFilenameTemplate: '[resource-path]',
devtoolFallbackModuleFilenameTemplate: '[resource-path]',
pathinfo: false
},
mode,
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel-loader',
options: {
cacheDirectory: true,
cacheCompression: false
}
},
{
test: /\.tsx?$/,
exclude: /node_modules/,
loader: 'ts-loader',
options: {
transpileOnly: true,
experimentalWatchApi: true
}
},
{
test: /\.css$/,
use: ['style-loader', 'css-loader']
},
{
test: /\.(woff|woff2|eot|ttf|otf)$/,
use: {
loader: 'file-loader'
}
}
]
},
devServer: {
contentBase: outputdir,
open: true,
openPage: path.normalize(cmd.page),
overlay: {
errors: true
},
historyApiFallback: true
// host: '0.0.0.0'
},
devtool: mode === 'production' ? 'source-map' : 'source-map',
optimization: {
runtimeChunk: 'single'
},
plugins: [
new CleanWebpackPlugin(),
new HtmlWebpackPlugin({
title: 'proj1',
filename: 'index.html',
template: 'websrc/index.tsx',
inject: false,
ssr: ssr({outputdir})(),
mode,
minify: mode === 'production' ? htmlMinifyOpts : {}
}),
new webpack.DefinePlugin(getEnvVars()),
new CopyPlugin(['websrc/static']),
new WorkerPlugin({globalObject: 'self'}),
new ForkTsCheckerWebpackPlugin(),
new HardSourceWebpackPlugin()
],
resolve: {
modules: [__dirname, 'node_modules'],
extensions: ['.ts', '.tsx', '.js']
},
resolveLoader: {
modules: ['node_modules']
}
}
if (cmd.report) {
opts.plugins.push(new BundleAnalyzerPlugin({analyzerMode: 'static'}))
}
if (cmd.debug) console.log(opts) // eslint-disable-line no-console
module.exports = opts
最佳答案
经过一些研究,您似乎遇到了 Typescript GitHub 上问题部分中列出的错误。
https://github.com/microsoft/TypeScript/issues/21801
审查完这一点后,我的建议是在整个应用程序中进行一些空检查。我最好的猜测是您在应用程序/或组件的初始化时遇到了一些竞争条件。希望这会有所帮助,祝你好运!
关于 typescript 运行时错误 : Cannot read property 'length' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58086396/
我只是有一个更琐碎的问题。 为什么undefined == undefined 返回true,而undefined >= undefined 为false? undefined 等于 undefine
用PHP 7.2编写套接字服务器。根据Firefox 60中的“网络”选项卡,服务器的一些HTTP响应的第一行随机变为undefined undefined undefined。因此,我尝试记录套接字
在 JavaScript 中这是真的: undefined == undefined 但这是错误的: undefined <= undefined 起初我以为<=运算符包含第一个,但我猜它试图将其转换
在回答这个问题 (Difference between [Object, Object] and Array(2)) 时,我在 JavaScript 数组中遇到了一些我以前不知道的东西(具有讽刺意味的
来自https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/of , Note: thi
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
当我添加 到我的 PrimeFaces Mobile 页面,然后我在服务器日志中收到以下警告 WARNING: JSF1064: Unable to find or serve resource, u
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我是一名优秀的程序员,十分优秀!