The requested module '@azure/arm-authorization' is expected to be of type CommonJS, which does not support named exports.
See original GitHub issue-
Package Name: @azure/arm-authorization
-
Package Version: 8.3.2"
-
OS: Google Chromebook OS Version 84.0.4147.94 (Official Build) (64-bit)
-
nodejs
- version: 14.5.0
-
browser
- name/version: Chrome Version 84.0.4147.94 (Official Build) (64-bit)
-
typescript
- version:
-
Is the bug related to documentation in
- README.md
- source code documentation
- SDK API docs on https://docs.microsoft.com
Describe the bug 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"
}
To Reproduce Steps to reproduce the behavior:
- Run the above via node app.js and it will produce error
Expected behavior A clear and concise description of what you expected to happen. I expected the interactive login to prompt.
Screenshots If applicable, add screenshots to help explain your problem.
Additional context Add any other context about the problem here.
Issue Analytics
- State:
- Created 3 years ago
- Comments:11 (4 by maintainers)
Top GitHub Comments
I just landed here googling the same issue but with express. You got that error because
azure-sdk-for-js
doesn’t have"type": "module"
in it’s paackage.json. Which means node treats it as a commonjs module and not es6 module. It has nothing to do with"type": "module"
in your own project as it is scope based. Solution: Is it possible to es6 import a commonjs module?If it helps, I’ve just published a sample package written in TypeScript, with dual exports: native ESM, and CommonJS. By using conditional exports, the code can be name-imported from TypeScript as well, avoiding the
The requested module '...' is expected to be of type CommonJS, which does not support named exports
error.The package is local-iso-dt, and this is the diff that adds supports for conditional exports.