'The requested module '@azure/arm-authorization' is expected to be of type CommonJS
I am using Node 14.5.0 with a default Express.JS installation. I need to leverage the Azure SDK for Node using Imports and have changed the default express require from:
const express = require('express')
to:
import express from 'express';
Express is able to load, but when I add the example SDK for authorization (https://github.com/Azure/azure-sdk-for-js/tree/master/sdk/authorization/arm-authorization) it is throwing the following error:
import { AuthorizationManagementClient, AuthorizationManagementModels, AuthorizationManagementMappers } from "@azure/arm-authorization"; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ SyntaxError: The requested module '@azure/arm-authorization' is expected to be of type CommonJS, which does not support named exports. CommonJS modules can be imported by importing the default export. For example: import pkg from '@azure/arm-authorization'; const { AuthorizationManagementClient, AuthorizationManagementModels, AuthorizationManagementMappers } = pkg; at ModuleJob._instantiate (internal/modules/esm/module_job.js:98:21) at async ModuleJob.run (internal/modules/esm/module_job.js:137:5) at async Loader.import (internal/modules/esm/loader.js:162:24)
I have already added "type": "module", to my package.json and installed the modules listed in the Azure SDK page.
My App.JS page is as follows:
import express from 'express';
import * as msRest from "@azure/ms-rest-js";
import * as msRestAzure from "@azure/ms-rest-azure-js";
import * as msRestNodeAuth from "@azure/ms-rest-nodeauth";
import { AuthorizationManagementClient, AuthorizationManagementModels, AuthorizationManagementMappers } from "@azure/arm-authorization";
const subscriptionId = process.env["myguideforsubhere"];
const app = express()
const port = 5000
app.get('/', (req, res) => res.send('Hello World!'))
app.listen(port, () => console.log(`Example app listening at http://localhost:${port}`))
msRestNodeAuth.interactiveLogin().then((creds) => {
const client = new AuthorizationManagementClient(creds, subscriptionId);
client.classicAdministrators.list().then((result) => {
console.log("The result is:");
console.log(result);
});
}).catch((err) => {
console.error(err);
});
My Package.json is as follows:
{
"name": "myproject",
"version": "1.0.0",
"description": "myproject",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Me",
"license": "ISC",
"dependencies": {
"@azure/arm-authorization": "^8.3.2",
"@azure/ms-rest-nodeauth": "^3.0.5",
"express": "^4.17.1"
},
"type": "module"
}
Does this look like an error in the SDK documentation where it can't use the imports as exampled but needs to be changed to require, or more likely, what have I done wrong?
Solution 1:[1]
See How to include commonjs module in ES6 module node app?
import { method } from 'commonjs-package'; // Errors
import packageMain from 'commonjs-package'; // Works
In your case, the azure sdk isn't written with "type": "module"
so node treats it as a commonjs module, not an es6 module. So, you'll have to import and destructure in two separate steps:
import armAuth from "@azure/arm-authorization";
let { AuthorizationManagementClient, AuthorizationManagementModels } = armAuth;
See Also: requested module does not provide an export named '*'
Solution 2:[2]
Try upgrading your node version to 16 or above. It fixed my errors
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | KyleMit |
Solution 2 | roneo |