Thanks to visit codestin.com
Credit goes to github.com

Skip to content

When using pnpm, a module that uses Buffer.isBuffer fails to bundle #2063

@wmartins

Description

@wmartins

Context

Hi browserify friends! I'm migrating a monorepo to use pnpm instead of yarn. The monorepo has a frontend package which is bundled with browserify. After migrating from yarn, the application started to throw an error when bundling.

The error happened when attempting to bundle filestack-js:

Error: Can't walk dependency graph: Cannot find module '../../../../../../node_modules/.pnpm/[email protected]/node_modules/is-buffer/index.js' from '../../node_modules/.pnpm/[email protected][email protected]/node_modules/filestack-js/build/browser/filestack.esm.js'

Investigation

The error seems to be related to the is-buffer package, however, it wasn't even a dependency for filestack-js. After some time debugging, it seemed to be related with a call that it does to Buffer.isBuffer. This led me to the insert-module-globals package:

https://github.com/browserify/insert-module-globals/blob/0549e4372523ec2c967058b0bfce51fad9038b0f/index.js#L38-L41

I believe somehow it is not resolving the path correctly. It may be related to the fact that pnpm symlinks the packages. Maybe there's something incorrect when trying resolve the path to is-buffer.

To prove that, if I go to the filestack-js directory:

cd node_modules/.pnpm/[email protected][email protected]/node_modules/filestack-js/build/browser/

And then, if I try requiring that path on the error, I can confirm the same problem occurs:

node -e "console.log(require('../../../../../../node_modules/.pnpm/[email protected]/node_modules/is-buffer/index.js'))"

However, the actual path should have been one level lower, meaning this works:

node -e "console.log(require('../../../../../../../node_modules/.pnpm/[email protected]/node_modules/is-buffer/index.js'))"

Reproducing

I've put up a repo that can be used to reproduce it: https://github.com/wmartins/browserify-pnpm-investigation.

Current workaround

My current workaround for this is to provide my own implementation of Buffer.isBuffer with insertGlobalVars.


I'm not sure if this should be a browserify/browserify issue or a browserify/insert-module-globals issue, but I've decided to add it here. If this is not the right place, I'm happy to move it.

Do you have any idea how to solve this problem? Is there anything else that I can do?

In case there's something missing or any extra information is needed, I'm happy to provide it!

I can also provide a PR, but honestly I don't have much context on how to navigate it and to point exactly where the issue is.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions