Node.js

真的你只需要看官方文档就好了,https://nodejs.org/zh-cn

Node.js 搞Web服务就是很方便。

搞懂EventLoop 事件循环很重要的。

包管理器 npm

添加项目依赖

安装到当前项目,写入 package.json

npm install express
# 简写
npm i express

安装后 package.json

{
  "dependencies": {
    "express": "^5.1.0"
  }
}

安装开发依赖

npm install -D typescript
# 或
npm i --save-dev typescript

{
  "devDependencies": {
    "typescript": "^5.9.0"
  }
}

安装指定版本

npm install express@5.1.0
# 或
npm install express@latest

删除依赖

npm uninstall express
# 或
npm remove express

执行后:

更新依赖

查看过期包

npm outdated
# 更新
npm update
# 升级到最新版
npm install express@latest

安装package.json中所有依赖

npm install
# 或
npm ci
命令 用途
npm install 普通开发
npm ci CI/CD、严格按 lock 文件安装

查看当前镜像源

npm config get registry
https://registry.npmjs.org/

# 国内镜像
npm config set registry https://registry.npmmirror.com
# 验证
npm config get registry
https://registry.npmmirror.com

# 恢复官方
npm config set registry https://registry.npmjs.org

查看所有 npm 配置

npm config list
# 或
npm config ls -l

npm 缓存位置

查看缓存目录

npm config get cache

# macos ~/.npm
# linux ~/.npm
# windows C:\Users\<用户名>\AppData\Local\npm-cache

清理缓存

npm cache clean --force
# 查看缓存
npm cache verify

全局安装位置

查看

npm root -g
# 例如
/opt/homebrew/lib/node_modules
# 查看全局可执行程序目录
npm bin -g
# 常见 /opt/homebrew/bin