我想将react-scripts
更新到下一个版本(4.0.0
),以便可以使用本指南here使用快速刷新功能。但是,当重新启动服务器时,由于以下错误,脚本无法正常工作:
$ yarn start
yarn run v1.22.4
$ react-scripts start
E:\Github\Web\so-rank\node_modules\react-scripts\scripts\utils\verifyTypeScriptSetup.js:210
appTsConfig.compilerOptions[option] = suggested;
^
TypeError: Cannot add property noFallthroughCasesInSwitch, object is not extensible
at verifyTypeScriptSetup (E:\Github\Web\so-rank\node_modules\react-scripts\scripts\utils\verifyTypeScriptSetup.js:210:45)
at Object.<anonymous> (E:\Github\Web\so-rank\node_modules\react-scripts\scripts\start.js:31:1)
at Module._compile (internal/modules/cjs/loader.js:1138:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
at Module.load (internal/modules/cjs/loader.js:986:32)
at Function.Module._load (internal/modules/cjs/loader.js:879:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
at internal/main/run_main_module.js:17:47
error Command failed with exit code 1.
最佳答案
可以通过在noFallthroughCasesInSwitch
中启用tsconfig.json
选项来解决此问题。有关更多信息,请参见讨论here。
{
"compilerOptions": {
"noFallthroughCasesInSwitch": true,
...
},
...
}
对于任何好奇的人,上述解决方案都不能解决该错误。它只是跳过下面的错误代码,如果没有提供,则会将建议的值分配给typescript编译器选项。默认情况下,从tsconfig.json
生成的react-scripts
没有noFallthroughCasesInSwitch
选项。添加该选项消除了运行代码的需要。// Some options when not present in the tsconfig.json will be assigned
// a suggested value which crashes the program
if (suggested != null) {
if (parsedCompilerOptions[option] === undefined) {
appTsConfig.compilerOptions[option] = suggested; // error here
...
}
}
编辑:如果脚本与其他选项一起崩溃,并且堆栈跟踪与我的问题相同,则应检查
tsconfig.json
中是否缺少以下编译器选项如果未在
tsconfig.json
中指定,These是Typescript编译器选项的建议值const compilerOptions = {
// These are suggested values and will be set when not present in the
// tsconfig.json
target: {
parsedValue: ts.ScriptTarget.ES5,
suggested: 'es5',
},
lib: { suggested: ['dom', 'dom.iterable', 'esnext'] },
allowJs: { suggested: true },
skipLibCheck: { suggested: true },
esModuleInterop: { suggested: true },
allowSyntheticDefaultImports: { suggested: true },
strict: { suggested: true },
forceConsistentCasingInFileNames: { suggested: true },
noFallthroughCasesInSwitch: { suggested: true },
module: {
parsedValue: ts.ModuleKind.ESNext,
value: 'esnext',
reason: 'for import() and import/export',
},
moduleResolution: {
parsedValue: ts.ModuleResolutionKind.NodeJs,
value: 'node',
reason: 'to match webpack resolution',
},
resolveJsonModule: { value: true, reason: 'to match webpack loader' },
isolatedModules: { value: true, reason: 'implementation limitation' },
noEmit: { value: true },
jsx: {
parsedValue: ts.JsxEmit.React,
suggested: 'react',
},
paths: { value: undefined, reason: 'aliased imports are not supported' },
};
您需要将这些选项显式添加到tsconfig.json
中,以便脚本可以跳过有问题的分支并避免崩溃。关于reactjs - 使用 typescript 模板将create-react-app更新到4.0时出错,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/64115884/