Azure Client Runtime for AutoRest Generated Node.js Libraries
ms-rest-azure served as the foundational client runtime for Node.js Azure client libraries generated using the AutoRest toolchain. It provided essential infrastructure for handling HTTP requests, error responses, tracing, and Azure-specific authentication mechanisms such as Device Token, Service Principal, and Managed Identity. The package (version 3.0.2, last published approximately four years ago) was part of the older `azure-sdk-for-node` repository. As of March 2023, this package, along with many others in its repository, has been officially deprecated. The Azure SDK team has transitioned to a new generation of SDKs (`@azure/*` packages) that offer isomorphic capabilities (browser and Node.js support), consistent design guidelines, and native TypeScript support. Users are strongly encouraged to migrate to the newer `@azure/core-client` and `@azure/identity` packages for modern Azure development.
Common errors
-
Error: credentials argument needs to implement signRequest method
cause This error often occurs when an incompatible credential object is passed to a client library, frequently due to mixing versions of `@azure/ms-rest-nodeauth` or `ms-rest-azure` or attempting to use a modern `@azure/identity` credential with an older client.fixEnsure that the credential object matches the expected interface of the client library. If using `ms-rest-azure` based clients, use `ms-rest-azure` or `@azure/ms-rest-nodeauth` credentials. If using modern `@azure/*` clients, use `@azure/identity` credentials. Avoid mixing credential types across different SDK generations. -
TypeError: msRestAzure.loginWithServicePrincipalSecret is not a function
cause This error can occur if `ms-rest-azure` is imported incorrectly (e.g., trying to destructure a default export as named), or if the package is truly not installed or corrupted. It's also common if attempting to use older `ms-rest-azure` functions in an environment where they are not correctly resolved, especially in newer ESM contexts where `require` patterns are discouraged.fixVerify correct installation (`npm install ms-rest-azure`) and import syntax. For CommonJS, ensure `const msRestAzure = require('ms-rest-azure');` and then `msRestAzure.loginWithServicePrincipalSecret`. For ESM, use `import * as msRestAzure from 'ms-rest-azure';` or named imports `import { loginWithServicePrincipalSecret } from 'ms-rest-azure';`. If still problematic, consider migrating to `@azure/identity`.
Warnings
- breaking The `ms-rest-azure` package was officially deprecated as of March 2023, along with many other packages in the `azure-sdk-for-node` repository. It no longer receives active development, new features, or security updates. Users should migrate to the new Azure SDK for JavaScript/TypeScript packages (`@azure/*`).
- deprecated Authentication methods like `loginWithServicePrincipalSecret` and `interactiveLogin` were initially moved from `ms-rest-azure` to `@azure/ms-rest-nodeauth` to support isomorphic libraries. `@azure/ms-rest-nodeauth` itself is now deprecated in favor of `@azure/identity`.
- gotcha `ms-rest-azure` was primarily designed for Node.js and CommonJS (CJS) environments as part of the older Azure SDKs. Modern JavaScript development often uses ECMAScript Modules (ESM), which can lead to compatibility issues when mixing `require()` and `import` statements or running in pure ESM projects.
- gotcha Versions of `ms-rest-azure` (via its dependency `ms-rest`) require Node.js version 6.10 or higher. Running on older Node.js environments will result in compatibility errors due to ES6 syntax and features introduced in those versions.
Install
-
npm install ms-rest-azure -
yarn add ms-rest-azure -
pnpm add ms-rest-azure
Imports
- loginWithServicePrincipalSecret
const { loginWithServicePrincipalSecret } = require('ms-rest-azure');import { loginWithServicePrincipalSecret } from 'ms-rest-azure'; - DeviceTokenCredentials
const { DeviceTokenCredentials } = require('ms-rest-azure');import { DeviceTokenCredentials } from 'ms-rest-azure'; - AzureServiceClient
const { AzureServiceClient } = require('ms-rest-azure');import { AzureServiceClient } from 'ms-rest-azure';
Quickstart
import { loginWithServicePrincipalSecret } from 'ms-rest-azure';
import { SubscriptionClient } from '@azure/arm-subscriptions'; // Example: using a client that depends on ms-rest-azure
const clientId = process.env.AZURE_CLIENT_ID ?? '';
const secret = process.env.AZURE_CLIENT_SECRET ?? '';
const domain = process.env.AZURE_TENANT_ID ?? ''; // Also known as tenantId
const subscriptionId = process.env.AZURE_SUBSCRIPTION_ID ?? '';
async function authenticateAndListSubscriptions() {
if (!clientId || !secret || !domain || !subscriptionId) {
console.error('Please set AZURE_CLIENT_ID, AZURE_CLIENT_SECRET, AZURE_TENANT_ID, and AZURE_SUBSCRIPTION_ID environment variables.');
return;
}
try {
console.log('Attempting to log in with Service Principal...');
const credentials = await loginWithServicePrincipalSecret(clientId, secret, domain);
console.log('Successfully authenticated.');
const client = new SubscriptionClient(credentials);
const subscriptions = await client.subscriptions.list();
console.log('Azure Subscriptions:');
for await (const sub of subscriptions) {
console.log(`- ${sub.displayName} (${sub.subscriptionId})`);
}
} catch (err) {
console.error('Authentication or API call failed:', err);
}
}
authenticateAndListSubscriptions();