digitalbazaar / cborld

A Javascript CBOR-LD processor for web browsers and Node.js apps.
https://json-ld.github.io/cbor-ld-spec/
BSD 3-Clause "New" or "Revised" License
16 stars 12 forks source link

Examples don't work with node v16.123.1 #53

Closed jandrieu closed 1 year ago

jandrieu commented 2 years ago

I've tried running the examples, but can get neither encode nor decode to work.

I set up a new npm init and installed @digitalbazaar/cborld.

When I tried the first code,


import { encode } from '@digitalbazaar/cborld';

const jsonldDocument = {
  '@context': 'https://www.w3.org/ns/activitystreams',
  type: 'Note',
  summary: 'CBOR-LD',
  content: 'CBOR-LD is awesome!'
};

// encode a JSON-LD Javascript object into CBOR-LD bytes
const cborldBytes = await encode({ jsonldDocument, documentLoader });

console.log(cborldBytes);

Unfortunately, that complained about the use of import outside a module

^^^^^^

SyntaxError: Cannot use import statement outside a module

If I add "type"="module" in my package.json, I get passed that error, only to get this one:

import { encode } from '@digitalbazaar/cborld';
         ^^^^^^
SyntaxError: Named export 'encode' not found. The requested module '@digitalbazaar/cborld' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from '@digitalbazaar/cborld';
const { encode } = pkg;

FWIW, I tried this alternative import and it didn't work.

On a lark, I tried the "type":"module" hack in the cborld package.json, I get a different error:

import { encode } from '@digitalbazaar/cborld';
         ^^^^^^
SyntaxError: The requested module '@digitalbazaar/cborld' does not provide an export named 'encode'

Any suggestions?

p.s. This was on a windows bash shell, running node v16.123.1

davidlehn commented 2 years ago

I assume you mean v16.13.1.

It does look like examples need some work. I think those examples may have come from code run through 'webpack' and/or 'esm'. One solution is to jump through an 'esm' hoop first:

index.js:

require = require('esm')(module);
module.exports = require('./main.js');

and in main.js:

import { encode } from '@digitalbazaar/cborld';

const jsonldDocument = {
  '@context': 'https://www.w3.org/ns/activitystreams',
  type: 'Note',
  summary: 'CBOR-LD',
  content: 'CBOR-LD is awesome!'
};

// FIXME create documentLoader

async function main() {
  // encode a JSON-LD Javascript object into CBOR-LD bytes
  const cborldBytes = await encode({ jsonldDocument, documentLoader });

  console.log(cborldBytes);
}
main();

You'll need to create a documentLoader that can handle contexts you use. (https://github.com/digitalbazaar/cborld/issues/54)

The module part will be fixed at some point. It's a bit tricky to handle CommonJS and ESM modules. I have a possible fix, but it needs to be vetted and then applied on a whole bunch of modules all at once so they all play together nicely. In this case, it would at least need to be applied to base58-universal dependency too. If you're interested in exploring that, I can show you the patches, but the above hack is easier for now. You could also switch to CommonJS and use require instead.

If you just wanted to see the encoded output, you might consider the in-progress branch of cborld-cli tool.

awoie commented 1 year ago

We ran into the same issue. Is there any executable Node JS example that we can just test and run?

dmitrizagidulin commented 1 year ago

We ran into the same issue. Is there any executable Node JS example that we can just test and run?

Hey Oliver, I think your best bet is to use the DCC fork of this lib (https://github.com/digitalcredentials/cborld), which we made work with TS and ReactNative. We use it in our VPQR fork (which creates CBOR-LD QR codes from VPs), so that might be your best bet of using executable Node/TS-compatible code: https://github.com/digitalcredentials/vpqr/blob/main/test/mock-data.js / https://github.com/digitalcredentials/vpqr/blob/main/test/vpqr.spec.js

dlongley commented 1 year ago

Closing as this library no longer uses esm.js and is a proper ESM library.