James McLachlan
James McLachlan

Reputation: 1368

How to bundle and import buffer npm package with aurelia-cli?

In my ESNext aurelia-cli project, I'm trying to use an npm module called bs58. bs58 uses Node buffers, so I've included the npm buffer module for Buffer support in the browser. buffer depends on a couple of other modules, which I've also included.

In aurelia.json I've mapped out the packages and dependencies:

      ...
      {
        "name": "base64-js",
        "path": "../node_modules/base64-js",
        "main": "index"
      },
      {
        "name": "ieee754",
        "path": "../node_modules/ieee754",
        "main": "index"
      },
      {
        "name": "buffer",
        "path": "../node_modules/buffer",
        "main": "index",
        "deps": [
          "base64-js",
          "ieee754"
        ]
      },
      {
        "name": "base-x",
        "path": "../node_modules/base-x",
        "main": "index"
      },
      {
        "name": "bs58",
        "path": "../node_modules/bs58",
        "main": "index",
        "deps": [
          "base-x"
        ]
      }
      ...

Then in my code I have:

import bs58 from 'bs58';
...
bs58.decode(...);

When I call bs58.decode I get "Buffer is not defined". I've tried importing buffer a few different ways, but nothing causes it to define the global Buffer object, e.g.

import 'buffer';
import Buffer from 'buffer';
import { Buffer} from 'buffer';
require('buffer');

What's the right way to include buffer with aurelia-cli?

Upvotes: 0

Views: 1162

Answers (1)

Rabah
Rabah

Reputation: 2062

the base-x package is looking for a global Buffer so give it one:

// aurelia.json
{
    "ieee754",
    "base64-js",
    "buffer",
    "base-x",
    "bs58"
}


// some js file
import { Buffer } from 'buffer';
window.Buffer = Buffer;
import bs58 from 'bs58';

Upvotes: 2

Related Questions