Error when using: import msal from "@azure/msal-node"
See original GitHub issueCore Library
MSAL Node (@azure/msal-node)
Core Library Version
1.7.0
Wrapper Library
Not Applicable
Wrapper Library Version
None
Description
When using the import statement for msal-node, all types and interfaces are available but upon compilation msal is undefined.
If I use the require statement then I don’t get the importation of the types.
Error Message
server-web | /app/src/routes/Login.ts:85
server-web | logLevel : msal.LogLevel.Verbose
server-web | ^
server-web | TypeError: Cannot read properties of undefined (reading 'LogLevel')
server-web | at new Login (/app/src/routes/Login.ts:85:31)
server-web | at Object.<anonymous> (/app/src/index.ts:82:16)
server-web | at Module._compile (node:internal/modules/cjs/loader:1103:14)
server-web | at Module.m._compile (/app/node_modules/ts-node/src/index.ts:1475:23)
server-web | at Module._extensions..js (node:internal/modules/cjs/loader:1155:10)
server-web | at Object.require.extensions.<computed> [as .ts] (/app/node_modules/ts-node/src/index.ts:1478:12)
server-web | at Module.load (node:internal/modules/cjs/loader:981:32)
server-web | at Function.Module._load (node:internal/modules/cjs/loader:822:12)
server-web | at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
server-web | at node:internal/main/run_main_module:17:47
server-web | [nodemon] app crashed - waiting for file changes before starting...
If I implicitly assign the LogLevel then it just triggers the next error…
server-web | [nodemon] starting `node -r ts-node/register --inspect=0.0.0.0:9229 ./src/index.ts`
server-web | Debugger listening on ws://0.0.0.0:9229/c8860d59-2b53-40dc-8cd6-dd64935864ff
server-web | For help, see: https://nodejs.org/en/docs/inspector
server-web | /app/src/routes/Login.ts:91
server-web | this.msalClientApp = new msal.ConfidentialClientApplication( this.msalConfig );
server-web | ^
server-web | TypeError: Cannot read properties of undefined (reading 'ConfidentialClientApplication')
server-web | at new Login (/app/src/routes/Login.ts:91:33)
server-web | at Object.<anonymous> (/app/src/index.ts:82:16)
server-web | at Module._compile (node:internal/modules/cjs/loader:1103:14)
server-web | at Module.m._compile (/app/node_modules/ts-node/src/index.ts:1475:23)
server-web | at Module._extensions..js (node:internal/modules/cjs/loader:1155:10)
server-web | at Object.require.extensions.<computed> [as .ts] (/app/node_modules/ts-node/src/index.ts:1478:12)
server-web | at Module.load (node:internal/modules/cjs/loader:981:32)
server-web | at Function.Module._load (node:internal/modules/cjs/loader:822:12)
server-web | at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
server-web | at node:internal/main/run_main_module:17:47
server-web | [nodemon] app crashed - waiting for file changes before starting...
Msal Logs
N/A
MSAL Configuration
this.msalConfig =
{
auth :
{
clientId : env.AUTH_APP_ID,
authority : `https://login.microsoftonline.com/${ env.AUTH_TENANT_ID }/`,
clientSecret : env.AUTH_CLIENT_SECRET,
},
system :
{
loggerOptions :
{
loggerCallback : this.log,
piiLoggingEnabled : true,
logLevel : msal.LogLevel.Verbose
}
}
};
Relevant Code Snippets
// Application Imports
import { IRoute } from "./IRoute";
import { Environment } from "../config/Environment";
import { Logger } from "../lib/logger/Logger";
// Node Module Imports
import express from "express";
import msal from "@azure/msal-node";
// Get environment variables.
const env : Environment = new Environment();
// Create logger.
const appLog : Logger = new Logger();
/**
* Redirects user to login to Azure Identity.
*/
export class Login implements IRoute
{
/**
* Express router object.
*
* @type { express.Router }
*/
public route : express.Router;
/**
* Path of the route.
*
* @type { string }
*/
public path : string;
/**
* Configuration object for MSAL
*
* @type { msal.Configuration }
*/
protected msalConfig : any;
/**
* MSAL confidential client application object.
*
* @type { msal.Configuration }
*/
protected msalClientApp : any;
/**
* @class Configures Login route.
* @constructs Login
*
* @example
* import { Login } from "./routes/Login";
*
* appServer.use( new Login().route );
*
* @returns { Login } - Object of the Login class.
*/
constructor ()
{
// Set configuration.
this.msalConfig =
{
auth :
{
clientId : env.AUTH_APP_ID,
authority : `https://login.microsoftonline.com/${ env.AUTH_TENANT_ID }/`,
clientSecret : env.AUTH_CLIENT_SECRET,
},
system :
{
loggerOptions :
{
loggerCallback : this.log,
piiLoggingEnabled : true,
logLevel : msal.LogLevel.Verbose
}
}
};
// Initialize MSAL client application.
this.msalClientApp = new msal.ConfidentialClientApplication( this.msalConfig );
this.path = env.AUTH_PATH_LOGIN;
this.route = express.Router().get( this.path, this.task );
}
/**
* The task(s) that are performed when the route is hit.
*
* @param req - Request object.
* @param res - Response object.
*/
public async task ( req : express.Request, res : express.Response ) : Promise<void>
{
const authCodeUrlParameters : any =
{
scopes : ["user.read"],
redirectUri : env.AUTH_PATH_REDIRECT,
};
try
{
// Request the redirection URL for user login.
const response = await this.msalClientApp.getAuthCodeUrl( authCodeUrlParameters );
// Redirect the user.
res.redirect( response );
}
catch ( err )
{
req.cookies.pageError = JSON.stringify( err );
// Redirect to error page.
res.redirect( '/error.html' );
}
}
/**
* Logs a MSAL event.
*
* @param level - Log message level.
* @param message - Message to be logged.
* @param pii - Flag indicating if personal information is included.
*
*/
protected log ( level : any, message : string, pii : boolean ) : void
{
appLog.asInfo( `[${ msal.LogLevel[ level ] }] ${ message }` );
}
}
Reproduction Steps
- Use import statement:
import msal from "@azure/msal-node";
- Write any MSAL code.
Expected Behavior
// Request the redirection URL for user login.
const response = await this.msalClientApp.getAuthCodeUrl( authCodeUrlParameters );
The above should return a promise.
Identity Provider
Other
Browsers Affected (Select all that apply)
Chrome, Firefox
Regression
No response
Source
External (Customer)
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:5 (3 by maintainers)
Top Results From Across the Web
[msal-node] Issues when server is behind proxy #2600 - GitHub
We're struggling with that too: we've tried using global-agent library, put simply importing it and calling bootstrap does not look sufficient.
Read more >How to migrate a Node.js app from ADAL to MSAL
When working with ADAL Node, you were likely using the Azure AD v1.0 endpoint. Apps migrating from ADAL to MSAL should switch to...
Read more >@azure/msal-node - npm
MSAL Node enables applications to authenticate users using Azure AD work and school accounts (AAD), Microsoft personal accounts (MSA) and social ...
Read more >Unable to create UserAgentApplication with Microsoft ...
I am trying to create a UserAgentApplication in my node project with @azure/msal-node library but i am getting an error TypeError: Msal.
Read more >@azure/msal-browser | microsoft-authentication-libraries-for-js
PopupRequest: Request object passed by user to retrieve a Code from the server (first leg of authorization code grant flow) with a popup...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
It was indeed installed OK.
It appears the solution is to import in this fashion:
However, It’s strange as when I use the normal import msal from “@azure/msal-node”; VSCode recognizes and validates everything without issue. I only get the error when I compile.
I agree @jamesdruhan, that’s very strange.
I will go ahead and close the issue for now, as we have found a resolution but feel free to reopen the issue if the problem persists.