gpt4 book ai didi

node.js - 请求的模块 '@azure/arm-authorization' 应该是 CommonJS 类型

转载 作者:行者123 更新时间:2023-12-05 07:03:49 25 4
gpt4 key购买 nike

我正在使用 Node 14.5.0 和默认的 Express.JS 安装。我需要使用 Imports 来利用 Azure SDK for Node,并更改了默认的 express require from:

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 } 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 importedby 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)

我已经将“type”:“module”添加到我的 package.json 并安装了 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 文档中的一个错误,它不能像示例那样使用导入,但需要更改为要求,或者更可能是我做错了什么?

最佳答案

参见 How to include commonjs module in ES6 module node app?

import { method } from 'commonjs-package';  // Errors
import packageMain from 'commonjs-package'; // Works

在您的情况下,azure sdk 不是用 "type": "module" 编写的,因此 Node 将其视为 commonjs 模块,而不是 es6 模块。因此,您必须分两个单独的步骤进行导入和解构:

import armAuth from "@azure/arm-authorization";
let { AuthorizationManagementClient, AuthorizationManagementModels } = armAuth;

另请参阅:requested module does not provide an export named '*'

关于node.js - 请求的模块 '@azure/arm-authorization' 应该是 CommonJS 类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63108391/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com