browserify / sha.js

Streamable SHA hashes in pure javascript
Other
288 stars 60 forks source link

Unable to resolve module `buffer` from `xxx/xxx`: Module does not exist in the module map #54

Closed Jancat closed 6 years ago

Jancat commented 6 years ago

safe-buffer package issue?

My node is v9.2.1

fanatid commented 6 years ago

Please provide more info =\ How you install it, how you use it.

Jancat commented 6 years ago
yarn add sha.js

import shajs from 'shajs'
shajs('sha256').update('42').digest('hex')

Than it throws the error:

Unable to resolve module buffer from ./node-modules/safe-buff/index.js: Module does not exist in the module map

Benjamin-Dobell commented 6 years ago

There's two problems at play here:

  1. For some reason this project doesn't have a runtime dependency on buffer, when it should.

It's listed in devDependencies instead, which is incorrect.

I think it's an attempt to make this project Node compatible, because Node comes with a built-in 'buffer'. However, seems as this project is specifically designed to target browsers that doesn't seem to make a heap of sense.

Given we're specifically targeting browsers there's actually no reason to use safe-buffer at all, just go ahead and use buffer directly.

  1. @Jancat, a problem with your setup.

I'm assuming you're using Rollup here. rollup-plugin-node-resolve has some really weird default behaviour when targeting a browser.

In the case of a browser there's no such thing as built-ins, the setting should really be false if you're targeting a browser. You need to update your rollup.config.js to look like:

import nodeResolve from 'rollup-plugin-node-resolve';

/* ... */

export default {
  /* ... */
  plugins: [
    nodeOptions({
      /* ... */
      preferBuiltins: false
    }),
    /* ... */
  ],
  /* ... */
}
dcousens commented 6 years ago

For some reason this project doesn't have a runtime dependency on buffer, when it should.

@Benjamin-Dobell the project depends on safe-buffer, how buffer is resolved in that package... is potentially frustrating, but, it was better than trying to resolve that issue everywhere else.

@Jancat maybe open the issue there?

Benjamin-Dobell commented 6 years ago

@dcousens Is this package meant to run inside Node?

Because if not, I'd suggest replacing the safe-buffer dependency with a direct dependency on buffer. safe-buffer is just an abstraction that allows a project to be compatible with both Node and browser; if this project isn't targeting Node then the abstraction is not necessary.

dcousens commented 6 years ago

@Benjamin-Dobell it doesn't not target Node? Whether it should, is probably up for discussion, but the same question exists for all of the implementations in https://github.com/crypto-browserify .

In the past, several were targeting Node to support newer APIs in older versions, but, that may not be the case now...

Benjamin-Dobell commented 6 years ago

Fair enough 👍

dcousens commented 6 years ago

@Benjamin-Dobell up to you if you want to chase that rabbit

scscgit commented 1 year ago

@dcousens I'd suggest replacing the safe-buffer dependency with a direct dependency on buffer.

Bump, a dependency issue still occurs (e.g. in latest Nuxt 3 that uses Vite instead of Webpack). There is an open PR since 2021 at https://github.com/feross/safe-buffer/pull/33...