Skip to content

Commit

Permalink
Added trailing slash to Buffer import in index.ts (#898)
Browse files Browse the repository at this point in the history
* Added trailing slash to Buffer import in index.ts

As recommended by the [Buffer maintainer](https://github.com/feross/buffer?tab=readme-ov-file#usage):

>To require this module explicitly, use require('buffer/') which tells the node.js module lookup algorithm (also used by browserify) to use the npm module named buffer instead of the node.js core module named buffer!

This solves a bug found bundling NSFWJS with Bun.

```
Build failed
1 | import { Buffer } from 'buffer';
             ^
error: No matching export in "node:buffer" for import "Buffer"
    at [path]
```

More information: oven-sh/bun#8683

* Add note about Bun issue

---------

Co-authored-by: Jamon Holmgren <[email protected]>
  • Loading branch information
gboere and jamonholmgren authored Nov 5, 2024
1 parent 828fa13 commit 93667b0
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as tf from "@tensorflow/tfjs";
import { Buffer } from "buffer";
// Remove the trailing slash below if this Bun issue gets fixed: https://github.com/oven-sh/bun/issues/8683
import { Buffer } from "buffer/";
import { NSFW_CLASSES } from "./nsfw_classes";

declare global {
Expand Down

1 comment on commit 93667b0

@thinkclay
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem with this is that pnpm (and I believe yarn) treat the trailing slash as a directory.

Cannot find module 'node_modules/nsfwjs/dist/esm/buffer/index.jsx' imported from node_modules/nsfwjs/dist/esm/index.js

I think what you'll want to do is specify which Buffer to use in package.json so that bundlers explicitly pick up the correct one instead of a syntax hack that might work for Bun but breaks others.

Please sign in to comment.