当前位置: 首页 > news >正文

安装NodeJS和TypeScript简要指南

在ubuntu上安装NodeJS

安装

方法 1:使用 Ubuntu 默认仓库安装
sudo apt update && sudo apt upgradesudo apt install nodejssudo apt install npmnode -v
#v22.16.0
方法 2:使用 nvm(Node Version Manager)安装–推荐

nvm 允许在同一台机器上管理多个 Node.js 版本,非常适合需要在不同版本间切换的开发者。

nvm项目地址:https://github.com/nvm-sh/nvm

安装nvm:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.3/install.sh | bash

或者

wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.3/install.sh | bash

卸载 Node.js

apt 安装的卸载方法:

如果您是通过 apt 安装的 Node.js,可以使用以下命令卸载:

sudo apt remove nodejs

如果您想同时删除配置文件,请使用:

sudo apt purge nodejs

nvm 安装的卸载方法:

如果您是通过 nvm 安装的 Node.js,可以使用以下命令卸载特定版本:

nvm uninstall node_version

在Windows上安装NodeJS

可以在以下网址https://github.com/coreybutler/nvm-windows/releases 下载 nvmsetup.zip。

安装完成后。 打开 PowerShell,尝试使用 nvm-windows 来列出当前安装的 Node 版本(此时应为无):nvm ls

nvm ls
No installations recognized.

安装完成后:

nvm ls* 24.2.0 (Currently using 64-bit executable)

安装当前版本的 Node.js:nvm install latest

nvm install latest
24.2.0
Downloading node.js version 24.2.0 (64-bit)...
Extracting node and npm...
Complete
Installation complete.
If you want to use this version, type:nvm use 24.2.0

安装所需的 Node.js 版本号后,输入 nvm use <version>(将 <version> 替换为数字,即:nvm use 24.2.0),选择要使用的版本。

若要更改要用于项目的 Node.js 版本,需创建新的项目目录 mkdir NodeTest,然后进入目录 cd NodeTest,然后输入 nvm use <version><version> 替换为要使用的版本号。

验证安装的 npm 版本:npm --version,这个版本号将自动更改为与当前 Node.js版本相关的 npm 版本。

使用nvm

以下为在linux使用的指令,在windows中有些不同。

source ~/.bashrc#If you want to see what versions are available to install:
nvm ls-remote#If you want to see what versions are installed:
nvm ls#And then in any new shell just use the installed version:#install a spesific version
nvm install v22.16.0nvm use node
#Or you can just run it:nvm run node --version#You can also get the path to the executable to where it was installed:nvm which 12.22#To set a default Node version to be used in any new shell, use the alias 'default':nvm alias default node # this refers to the latest installed version of node
nvm alias default 18 # this refers to the latest installed v18.x version of node
nvm alias default 18.12  # this refers to the latest installed v18.12.x version of nodenode -v

安装Typescript

确认已安装NodeJS

更新NPM到最新版本

sudo npm install npm@latest -g

安装TypeScript编译器

sudo npm install -g typescript

验证TypeScript 安装

$ tsc --version
Version 5.8.3

创建TypeScript测试项目

Step 1: 创建项目目录

mkdir ts-test-project
cd ts-test-project

Step 2: 初始化一个TypeScript 项目

tsc --init
# creates a tsconfig.json file with:
target: es2016module: commonjsstrict: trueesModuleInterop: trueskipLibCheck: trueforceConsistentCasingInFileNames: trueYou can learn more at https://aka.ms/tsconfig

Step 3: 创建一个简单的 TypeScript 文件

nano hello.tslet message: string = "Hello, World!";
console.log(message);

Step 4: 编译和运行这个TypeScript 文件

tsc hello.ts
#compile the hello.ts file into a JavaScript file named hello.js

使用Node.js运行编译后的文件:

node hello.js
http://www.lryc.cn/news/615174.html

相关文章:

  • 虚拟机远程连接报错解决办法
  • 「iOS」————分类与扩展
  • 书生浦语第五期-L1G4-InternLM 论文分类微调实践(XTuner 版)
  • 代码随想录day60图论10
  • 快速使用selenium+java案例
  • Nginx 性能优化与动态内容处理
  • TOMCAT笔记
  • 七、《Serverless架构:按毫秒计费的成本革命》--从新浪AI推理平台50%效能提升看无服务器本质
  • 前端如何安全存储 API 密钥 —— 两种实用方案
  • CosyVoice 语音合成模型性能优化实战:从 CPU 瓶颈到 GPU 加速的完整解决方案
  • electron多进程设计
  • K8s-pod控制器
  • Baumer高防护相机如何通过YoloV8深度学习模型实现输电线路塔电缆检测分割(C#代码UI界面版)
  • DAY 37 作业(补)
  • 99-基于Python的京东手机数据分析及预测系统
  • 【工具变量】全国省级农业保险保费收入与赔付支出数据更新(2001-2023年)
  • 爬虫攻防战:反爬与反反爬全解析
  • react-window
  • 【Datawhale AI夏令营】基于多模态RAG的企业财报问答系统
  • Arduino系列教程:点亮一个LED灯
  • 【工具】Python多环境管理
  • Red Hat Enterprise Linux 7.9安装Oracle 11.2.0.4单实例数据库-图文详解
  • Python训练营打卡Day27-类的定义和方法
  • 线程池多反应堆服务器webserver(c++)
  • 算法篇----模拟
  • Linux的软件防火墙iptables
  • QML 鼠标穿透
  • 从免费到盈利:Coze智能体1小时封装变现全流程指南——井云科技
  • 云服务器--阿里云OSS(2)【Springboot使用阿里云OSS】
  • 81 keil仿真调试记录