【发布时间】:2020-11-16 09:00:02
【问题描述】:
我正在使用带有默认 Express.JS 安装的 Node 14.5.0。我需要使用 Imports 来利用 Azure SDK for Node,并更改了默认的 express 要求:
const express = require('express')
到:
import express from 'express';
Express 能够加载,但是当我添加示例 SDK 以进行授权 (https://github.com/Azure/azure-sdk-for-js/tree/master/sdk/authorization/arm-authorization) 时,它会引发以下错误:
import { AuthorizationManagementClient, AuthorizationManagementModels, AuthorizationManagementMappers } 来自“@azure/arm-authorization”; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ SyntaxError: The requested module '@azure/arm-authorization' is expected to be of CommonJS, 它不支持命名导出。可以导入 CommonJS 模块 通过导入默认导出。例如:从导入 pkg '@azure/arm-authorization'; const { AuthorizationManagementClient, AuthorizationManagementModels, AuthorizationManagementMappers } = pkg; 在 ModuleJob._instantiate (internal/modules/esm/module_job.js:98:21) 在异步 ModuleJob.run (internal/modules/esm/module_job.js:137:5) 在异步 Loader.import (internal/modules/esm/loader.js:162:24)
我已经在我的 package.json 中添加了“type”:“module”,并安装了 Azure SDK 页面中列出的模块。
我的App.JS页面如下:
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);
});
我的Package.json如下:
{
"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"
}
这看起来像 SDK 文档中的错误,它无法使用示例中的导入,但需要更改为 require,或者更有可能是我做错了什么?
【问题讨论】: