npm - 如何将 TypeScript 升级到新的主版本
即使我升级了所有包并删除了 node_modules 和 package-lock.json,然后用 npm install 重新安装,我的 git 仓库中的 TypeScript 运行时仍然显示 4.9.5。
我如何才能让 tsc 的版本更新到 5.0 以上?
npx tsc --version
Version 4.9.5
./node_modules/.bin/tsc --version
Version 4.9.5
我有一个 NPM 单体仓库,其中所有包都将“typescript”列为开发依赖。不久前,我在每个包上使用了“npm uninstall typescript”,然后通过“npm install typescript -D”重新添加,因此包依赖列出了新的主版本(之前是 v4,现在是 v5)。
当我构建我的一个包(create-react-app)时,它显示 ESLint 认为我正在使用 TypeScript 版本 4.9.5。
WARNING: You are currently running a version of TypeScript which is not officially supported by @typescript-eslint/typescript-estree.
You may find that it works just fine, or you may not.
SUPPORTED TYPESCRIPT VERSIONS: >=3.3.1 <4.9.0
YOUR TYPESCRIPT VERSION: 4.9.5
Please only submit bug reports when using the officially supported version.
我的任何 package.json 文件或 package-lock.json 文件都没有显示 v4。所有文件都显示 v5。
npm list --depth=0 | grep -i typescript
│ └── [email
│ └── [email
│ ├── [email
│ └── [email
│ ├── [email
│ ├── @babel/[email
│ ├── [email
│ ├── [email
│ ├── [email
│ ├── @babel/[email deduped
│ ├── @rollup/[email
│ └── [email
│ ├── [email
│ ├── [email
│ └── [email
│ └── [email
│ └── [email
│ └── [email
│ └── [email
│ ├── [email
│ ├── [email
│ ├── [email
│ ├── [email
│ ├── [email
│ ├── [email
│ ├── @babel/[email deduped
│ ├── [email
│ ├── [email
│ └── [email
│ ├── [email
│ └── [email
│ └── [email
│ ├── [email
│ └── [email
│ └── [email
│ └── [email
我目前正在通过 nvm 使用 node 16.19 和 npm 8.11。
node --version
v16.19.1
npm --version
8.19.3
编辑:这篇文章是关于 Typescript 的,而不是 React 或 Create-React-App,在一个 NPM 工作区(单体)仓库中。对于任何混淆感到抱歉。
答案#1
如果你是
- 使用 NPM 工作区的多包仓库,
- 并且嵌套的包在开发中依赖于 TypeScript,
- 并且你已经升级了 TypeScript 版本,
- 但是
npx tsc --version仍然显示旧版本
例如:
cd <repo_clone_directory>
ls -1 packages/
micro-service-1
micro-service-2
ui-1
ui-2
npx tsc --version
Version 4.9.5
那么你可能需要在你的顶层 package.json 文件中添加一个 TypeScript 的(开发)依赖。
cd <repo_clone_directory>
npm install typescript
npx tsc --version
Version 5.2.2
如果你的 CreateReactApp 包在 ESLint 运行时抱怨你的 TypeScript 版本不受支持,那么你应该
- 暂时禁用 ESLint。
- 最终迁移到 Vitejs,因为 CRA 已被弃用。
来自 packages/ui-1/package.json
"scripts": {
"start": "DISABLE_ESLINT_PLUGIN=true react-scripts start",
"build": "DISABLE_ESLINT_PLUGIN=true react-scripts build",
编辑:在顶级 package.json 中添加 “typescript” 也有助于小版本升级。
