@azure/msal-node

中文文档
7.2.1发布于 2026/11/07MITTypeScriptESM + CJS近 30 天下载 1亿

Microsoft Authentication Library for Node

原始使用文档
文档目录16 节
  1. About
  2. OAuth2.0 grant types supported:
  3. Scenarios supported:
  4. FAQ
  5. Prerequisites
  6. Installation
  7. Via NPM:
  8. Node Version Support
  9. Usage
  10. MSAL basics
  11. Samples
  12. Build and Test
  13. Local Development
  14. Security Reporting
  15. License
  16. We Value and Adhere to the Microsoft Open Source Code of Co…

Microsoft Authentication Library for Node (msal-node)

npm version npm version codecov

Getting StartedAAD DocsLibrary Reference
  1. About
  2. FAQ
  3. Changelog
  4. Prerequisites
  5. Installation
  6. Node Version Support
  7. Usage
  8. Samples
  9. Build Library
  10. Security Reporting
  11. License
  12. Code of Conduct

About

MSAL Node enables applications to authenticate users using Azure AD work and school accounts (AAD), Microsoft personal accounts (MSA) and social identity providers like Facebook, Google, LinkedIn, Microsoft accounts, etc. through Azure AD B2C service. It also enables your app to get tokens to access Microsoft Cloud services such as Microsoft Graph.

OAuth2.0 grant types supported:

The current version supports the following ways of acquiring tokens:

Public Client:

Confidential Client:

Note that the username and password flow is deprecated and support will be removed in a future release.

More details on different grant types supported by Microsoft authentication libraries in general can be found here.

Scenarios supported:

The scenarios supported with this library are:

  • Desktop app that calls web APIs
  • Web app that calls web APIs
  • Web APIs that call web APIs
  • Daemon apps

More details on scenarios and the authentication flows that map to each of them can be found here.

FAQ

See here.

Prerequisites

Before using @azure/msal-node you will need to register your app in the azure portal:

Installation

Via NPM:

npm install @azure/msal-node

Node Version Support

MSAL Node will follow the Long Term Support (LTS) schedule of the Node.js project. Our support plan is as follows.

Any major MSAL Node release:

  • Will support stable (even-numbered) Maintenance LTS, Active LTS, and Current versions of Node
  • Will drop support for any previously supported Node versions that have reached end of life
  • Will not support prerelease/preview/pending versions until they are stable
MSAL Node versionMSAL support statusSupported Node versions
4.x.xActive development20, 22, 24
3.x.xActive development16, 18, 20, 22, 24
2.x.xIn maintenance16, 18, 20, 22
1.x.xIn maintenance10, 12, 14, 16, 18

Note: There have been no functional changes in the MSAL Node v2 release.

Usage

MSAL basics

Samples

There are multiple samples included in the repository that use MSAL Node to acquire tokens. These samples are currently used for manual testing, and are not meant to be a reference of best practices, therefore use judgement and do not blindly copy this code to any production applications.

AAD samples:

  • auth-code: Express app using OAuth2.0 authorization code flow.
  • auth-code-pkce: Express app using OAuth2.0 authorization code flow with PKCE.
  • device-code: Command line app using OAuth 2.0 device code flow.
  • refresh-token: Command line app using OAuth 2.0 refresh flow.
  • silent-flow: Express app using OAuth2.0 authorization code flow to acquire a token and store in the token cache, and silent flow to use tokens in the token cache.
  • client-credentials: Daemon app using OAuth 2.0 client credential grant to acquire a token.
  • on-behalf-of: Web application using OAuth 2.0 auth code flow to acquire a token for a web API. The web API validates the token, and calls Microsoft Graph on behalf of the user who authenticated in the web application.
  • username-password: Web application using OAuth 2.0 resource owner password credentials (ROPC) flow to acquire a token for a web API.
  • ElectronTestApp: Electron desktop application using OAuth 2.0 auth code with PKCE flow to acquire a token for a web API such as Microsoft Graph.
  • mcp-flows: Express app using OAuth 2.0 authorization code flow with MCP resource isolation. See MCP Flows documentation.
  • Hybrid Spa Sample: Sample demonstrating how to use enableSpaAuthorizationCode to perform SSO for applications that leverage server-side and client-side authentication using MSAL Browser and MSAL Node.

B2C samples:

Others:

Build and Test

// Install dependencies from root of repo
npm install

// Change to the msal-node package directory
cd lib/msal-node

// To run build for common package & node package
npm run build:all

// To run build only for node package
npm run build

// To run tests
npm run test

Local Development

Below is a list of commands you will probably find useful:

npm run build:modules:watch

Runs the project in development/watch mode. Your project will be rebuilt upon changes. TSDX has a special logger for you convenience. Error messages are pretty printed and formatted for compatibility VS Code's Problems tab. The library will be rebuilt if you make edits.

npm run build

Bundles the package to the dist folder. The package is optimized and bundled with Rollup into multiple formats (CommonJS, UMD, and ES Module).

npm run build:all

Builds both msal-common and msal-node

npm run lint

Runs eslint with Prettier

npm test, npm run test:coverage, npm run test:watch

Runs the test watcher (Jest) in an interactive mode. By default, runs tests related to files changed since the last commit. Generate code coverage by adding the flag --coverage. No additional setup needed. Jest can collect code coverage information from entire projects, including untested files.

Security Reporting

If you find a security issue with our libraries or services please report it to [email protected] with as much detail as possible. Your submission may be eligible for a bounty through the Microsoft Bounty program. Please do not post security issues to GitHub Issues or any other public site. We will contact you shortly upon receiving the information. We encourage you to get notifications of when security incidents occur by visiting this page and subscribing to Security Advisory Alerts.

License

Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT License.

We Value and Adhere to the Microsoft Open Source Code of Conduct

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact [email protected] with any additional questions or comments.

版本与发布记录

@azure/msal-node 共发布 188 个版本,首次发布于 2020/07/07;过去 12 个月发布了 46 个版本。

版本发布日期距今
7.2.1latest2026/11/07今天
7.2.02026/11/052 天前
7.1.22026/10/2811 天前
7.1.12026/10/2217 天前
7.1.02026/10/1424 天前
7.0.22026/10/1425 天前

其他发布通道:alpha → 5.0.0-alpha.0,beta → 5.0.0-beta.0,lts → 3.8.10。

关于 @azure/msal-node 的常见问题

@azure/msal-node 是什么?

Microsoft Authentication Library for Node

如何安装 @azure/msal-node?

在项目目录运行 npm install @azure/msal-node,或使用 pnpm add / yarn add / bun add。

@azure/msal-node 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@azure/msal-node 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@azure/msal-node 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=20。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@azure/msal-node 可以商用吗?

@azure/msal-node 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@azure/msal-node 的下载量有多少?

根据 npm 官方下载统计,@azure/msal-node 在 2026/10/08 – 2026/11/06 这 30 天内被下载 100,246,166 次,日均约 3,341,539 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@azure/msal-node 还在维护吗?

最新版本 7.2.1 发布于 2026/11/07(今天);至今共发布 188 个版本,其中过去 12 个月发布了 46 个;首次发布于 2020/07/07。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。