I am trying to use one npm package which is ESM
https://www.npmjs.com/package/key-did-resolver
我正在尝试使用一个npm包,即esm https://www.npmjs.com/package/key-did-resolver。
I can not require the 'key-did-resolver' package after installation as it's ESM , So I am trying to load in using async import
我不能在安装后需要‘Key-DID-Resolver’程序包,因为它是ESM,所以我正在尝试使用异步导入加载
The logic I am trying to achieve at the end is as mentioned in the package something like
我试图在最后实现的逻辑是在包中提到的,类似于
import * as KeyDidResolver from 'key-did-resolver'
import {Resolver} from 'did-resolver'
const keyDidResolver = KeyDIDResolver.getResolver()
console.log(keyDidResolver)
const didResolver = new Resolver(keyDidResolver)
const doc = await didResolver.resolve('did:key:z6MktvqCyLxTsXUH1tUZncNdVeEZ7hNh7npPRbUU27GTrYb8')
console.log(doc)
console.log(doc.didDocument.verificationMethod)
So I tried to import on top of my JS class file in my node application as
因此,我尝试在节点应用程序中的JS类文件上导入为
const {Resolver} = require('did-resolver'); // commonJs supported
const didResolve = (...args) => import('key-did-resolver').then(({default: resolver}) => {const didResolver = new Resolver(resolver.getResolver);return didResolver.resolve(...args) });
and in one of the class function I am trying to call it :
在我尝试调用的一个类函数中:
async myresolve(didKey){
try {
await didResolve([didKey]);
} catch (err) {
console.error('Error importing key-did-resolver:', err);
}
}
So when I start the server , it's started however when I execute the flow app crashes when it hits " await didResolve([didKey]);"
因此,当我启动服务器时,它是启动的,但是当我执行Flow应用程序时,当它点击“await didResolve([didKey]);”时崩溃。
I'm using node 16
我使用的是节点16
enter image description here
在此输入图像描述
How Can I use the loaded package of const didResolve =...etc
如何使用已加载的const didResolve=...等包
更多回答
What is the full error you are getting?
您得到的完整错误是什么?
优秀答案推荐
Problem identification
问题识别
const divResolve
is the promise object returned by then
in the promise chain of its assigned value:
Const divResolve是当时在其赋值的Promise链中返回的Promise对象:
const didResolve = (...args) =>
import('key-did-resolver')
.then( ({default: resolver}) => {
const didResolver = new Resolver(resolver.getResolver);
return didResolver.resolve(...args)
}
);
which when fulfilled without error will hold the value that was promised: didResolver.resolve(...args)
.
当没有错误地完成时,它将保持承诺的值:didResolver.Resolver.Resolver.args。
Discussion
讨论
await didResolve([didKey]);
should throw a run time error because didResolve
is not a function.
等待didResolve([didKey]);应引发运行时错误,因为didResolve不是函数。
As a blanket statement for node: CommonJS modules can't explicitly or implicitly call any code imported from an ES6 module, within CommonJS code run synchronously at application startup.
作为node:CommonJS模块不能在应用程序启动时同步运行的CommonJS代码中显式或隐式调用从ES6模块导入的任何代码。
Potential Solutions
潜在的解决方案
There are two major approaches to solving the problem if mixed ES6/CommonJS module usage is to be maintained:
如果要保持ES6/CommonJS模块的混合使用,有两种主要方法可以解决该问题:
Put all application code, currently in the main module, into its own application module. Also write a new main (CommonJS) module that loads all ES6 modules, using import()
and waits for all of them to be loaded before requiring the app module and passing it a single imported value for one module, or name space object for the default export of multiple ES6 modules.
As for 1, but write the main module in ES6 and invoke the application module using module.createRequire(filename)`
更多回答
我是一名优秀的程序员,十分优秀!