本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
TypeScript在 AL2 023
注意
本文档提供了有关其基于 TypeScript Node.js 的执行环境的基本信息。它还涵盖了典型的开发工作流程,并解释了如何 TypeScript 在 AL2 023 中打包以提供一致且可重现的开发环境。
TypeScriptnodejs20-typescript或nodejs22-typescript)安装 TS 编译器的方式:在系统级别全局安装,每个支持的Node.js版本分别安装。
t sc 不直接依赖于任何Node.js版本。编译器需要一定级别的运行时功能,这些功能是通过 t ar
Node.js基于的运行时设计有一定的弱点:它仅支持主机上运行时的一个版本,并且要求所有依赖项在项目级别的可重复性和一致性。这导致了以下常见的使用方法 TypeScript:TS 编译器、当前Node.js版本的 TS 基本配置以及所有软件依赖项都安装在本地项目中。尽管全局安装的节点模块应该只是 CLI 工具,例如 npm,但也是 CLI 工具的 tsc 很少在全球范围内安装。值得庆幸的是,tsc 的全局(系统范围)和本地(项目内)安装可以毫无问题地共存,也可以是独立使用的不同版本。请注意,本地安装的 tsc 应使用 npx 工具执行,该工具与 npm 一起安装。因此,即使使用系统 TS 编译器,用户也可以选择运行时组件的版本,例如Node.js(通过备选方案切换活动版本)、TS 编译器(要么在本地安装,要么在全局安装并通过备选方案切换活动版本),并根据特定需求对其进行配置。
Amazon Linux 以与其他全局安装的节点模块(例如 npm)相同的方式打包 TS 编译器,基本Node.js版本为每个版本。软件包和二进制文件采用命名空间,其名称中包含的主要Node.js版本。编译器的默认可执行文件名称 tsc 在运行时由备选工具管理,并指向已安装该文件并将由其执行的Node.js当前活动版本。此选择不取决于当前的Node.js运行时版本。可以将节点可执行文件指向 Node.js 20,并将 tsc 配置为由 Node.js 22 解释。也可以使用 TS 编译器的命名空间名称,例如 t sc-{MAJOR_VERSION},这与默认 ts c 名称的配置无关。
一些用于管理 TS 编译器活动版本的有用命令
-
检查配置了哪些备选方案
alternatives --list -
检查 tsc 的当前配置
alternatives --display tsc -
以交互方式更改版本 tsc
alternatives --config tsc -
切换到手动模式并选择特定版本
alternatives --set tsc /usr/bin/tsc-{MAJOR_VERSION} -
切换回 auto 版本选择模式
alternatives --auto tsc
在同一系统上安装和使用多个版本的 Node 和 TS 编译器的示例:
# Check the AL2023 release $ cat /etc/amazon-linux-release Amazon Linux release 2023.9.20250929 (Amazon Linux) # Install a TypeScript compiler for Node.js 20 and 22 # Node.js 20 and 22 will be installed automatically $ sudo dnf install -qy nodejs20-typescript nodejs22-typescript # Check what was installed $ rpm -q nodejs20 nodejs20-typescript nodejs22 nodejs22-typescript nodejs20-20.19.5-1.amzn2023.0.1.x86_64 nodejs20-typescript-5.9.2-1.amzn2023.0.1.noarch nodejs22-22.19.0-1.amzn2023.0.1.x86_64 nodejs22-typescript-5.9.2-1.amzn2023.0.1.noarch # Check the active version of Node.js - it is version 20 $ alternatives --display node node - status is auto. link currently points to /usr/bin/node-20 /usr/bin/node-20 - priority 100 slave npmrc: /usr/lib/nodejs20/lib/node_modules/npm/npmrc slave npm: /usr/bin/npm-20 slave npx: /usr/bin/npx-20 slave node_modules: /usr/lib/nodejs20/lib/node_modules /usr/bin/node-22 - priority 100 slave npmrc: /usr/lib/nodejs22/lib/node_modules/npm/npmrc slave npm: /usr/bin/npm-22 slave npx: /usr/bin/npx-22 slave node_modules: /usr/lib/nodejs22/lib/node_modules Current 'best' version is /usr/bin/node-20. # Check the active JS runtime version for TypeScript # Currently, the tsc compiler will be executed by Node.js 22 $ alternatives --display tsc tsc - status is auto. link currently points to /usr/bin/tsc-22 /usr/bin/tsc-22 - priority 100 slave tsserver: /usr/bin/tsserver-22 /usr/bin/tsc-20 - priority 100 slave tsserver: /usr/bin/tsserver-20 Current 'best' version is /usr/bin/tsc-22. # Check versions printed by executables $ node -v v20.19.5 $ tsc -v Version 5.9.2 # while the node is 20, tsc is executed by node 22 anyway $ head -1 /usr/bin/tsc #!/usr/bin/node-22 # However, instead of default executable names, e.g. node or tsc, # we can use namespaced names to target any installed version $ node-20 -v v20.19.5 $ node-22 -v v22.19.0 $ tsc-20 -v Version 5.9.2 $ tsc-22 -v Version 5.9.2 $ head -1 /usr/bin/tsc-20 #!/usr/bin/node-20 $ head -1 /usr/bin/tsc-22 #!/usr/bin/node-22