From 7a4d21881d803e654bf78dafd7a9583229fd7cc7 Mon Sep 17 00:00:00 2001 From: Lapo Luchini Date: Mon, 29 Apr 2024 16:03:15 +0000 Subject: [PATCH] Improve ESM usage examples. --- README.md | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 3250bfe..56d5c9b 100644 --- a/README.md +++ b/README.md @@ -26,10 +26,27 @@ import { ASN1 } from '@lapo/asn1js'; A submodule of this package can also be imported: ```js -import { Hex } from '@lapo/asn1js/hex'; +import { Hex } from '@lapo/asn1js/hex.js'; ``` -Unfortunately until [`require(esm)` gets released](https://joyeecheung.github.io/blog/2024/03/18/require-esm-in-node-js/) it is necessary to use async `import()` when used from CommonJS (legacy NodeJS) code. +Once [`require(esm)` gets released](https://joyeecheung.github.io/blog/2024/03/18/require-esm-in-node-js/) (probably in Node 22) it will be possible to `require` it normally: + +```js +const ASN1 = require('@lapo/asn1js'); +const Hex = require('@lapo/asn1js/hex.js'); +console.log(ASN1.decode(Hex.decode('06032B6570')).content()); +``` + +Until then, it is necessary to use async `import()` when used from CommonJS (legacy NodeJS) code: + +```js +async function main() { + const { ASN1 } = await import('@lapo/asn1js'); + const { Hex } = await import('@lapo/asn1js/hex.js'); + console.log(ASN1.decode(Hex.decode('06032B6570')).content()); +} +main(); +``` Usage on the web -------------------- -- 2.51.2