question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

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:

  1. 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:closed
  • Created 3 years ago
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
AndeYashwanthcommented, Jul 29, 2020

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?

import { method } from 'commonjs-package'; // Errors
import packageMain from 'commonjs-package'; // Works
0reactions
dandvcommented, Sep 7, 2020

Unfortunately we don’t support native ESM yet

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

The requested module '@azure/arm-authorization' is expected ...
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...
Read more >
Node.js now supports named imports from CommonJS ...
This syntax allows you to only import specific named exports from an ES module – in the example code above we didn't import...
Read more >
export - JavaScript - MDN Web Docs
This re-exports all named exports from mod as the named exports of the current module, but the default export of mod is not...
Read more >
How to write CommonJS exports that can be name-imported ...
This blog post explores how to write CommonJS modules so that their exports can be name-imported from ESM modules on Node.js.
Read more >
Module Methods - webpack
No CommonJS allowed, for example, you can't use require , module.exports or exports; File extensions are required when importing, e.g, you should use...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found