Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

error: No matching export in "node:buffer" for import "Buffer" #12272

Closed
davidstevens37 opened this issue Jul 1, 2024 · 7 comments · Fixed by #15389
Closed

error: No matching export in "node:buffer" for import "Buffer" #12272

davidstevens37 opened this issue Jul 1, 2024 · 7 comments · Fixed by #15389
Labels
bug Something isn't working typescript Something for TypeScript

Comments

@davidstevens37
Copy link
Contributor

davidstevens37 commented Jul 1, 2024

What version of Bun is running?

1.1.17+bb66bba1b

What platform is your computer?

Darwin 23.2.0 arm64 arm

What steps can reproduce the bug?

While using bun to run a typescript file that, imports the Buffer named export from the node:buffer module, the Buffer is imported. However, when using bun build on the same file: error: No matching export in "node:buffer" for import "Buffer"

index.ts

import { Buffer } from "node:buffer";
console.log(Buffer);

While trying to transpile:
> bun build index.ts

> bun build index.ts 
1 | import { Buffer } from "node:buffer";
             ^
error: No matching export in "node:buffer" for import "Buffer"
    at /Users/home/index.ts:1:10

While executiong the TS directly:
> bun run index.ts

[class Function]

What is the expected behavior?

No response

What do you see instead?

No response

Additional information

No response

@davidstevens37 davidstevens37 added bug Something isn't working typescript Something for TypeScript labels Jul 1, 2024
@CodingMarkus
Copy link

In which version has hat been fixed? I still see exactly this behavior in version 1.1.33, released just 5 days ago.

@davidstevens37
Copy link
Contributor Author

davidstevens37 commented Oct 29, 2024

@CodingMarkus it wasn’t a bug, I just wasn’t including the right arguments to the build command. The default target is “browser” you need to pass an explicit “node” value to the target argument.

https://bun.sh/docs/bundler#target

@CodingMarkus
Copy link

But I'm seeing this issue with this Buffer implementation:

https://github.com/feross/buffer

And as the page says:

"The buffer module from node.js, for the browser."

When running

bun add buffer

it downloads exactly this implementation and I can use it in TypeScript with

import { Buffer } from "buffer"

but I cannot use

bun build index.ts

As that yields exactly the same error.

Note that the same project has no issue when I install the package with npm and then bundle it with tsc.

@davidstevens37
Copy link
Contributor Author

Hmm, I’m not sure. I wonder if it’s due to a difference in import precedence when the external package name matches a built in package name.

Are you able to import using a relative path “./node_modules/path-to-package” instead of “buffer”?

Does tsc default to browser also, or is tsc targeting a node bundle?

@CodingMarkus
Copy link

tsc without any arguments reads all information from tsconfig.json, which looks like this:

{
	"compilerOptions": {
		"strict": true,

		"noImplicitAny": true,
		"noImplicitReturns": true,
		"noImplicitOverride": true,
		"noUncheckedIndexedAccess": true,
		"noFallthroughCasesInSwitch": true,
		"noPropertyAccessFromIndexSignature": true,

		"lib": [ "ES2020", "DOM" ],
		"target": "ES2015",
		"module": "ES2015",
		"moduleResolution": "bundler",

		"sourceMap": true,
		"removeComments": true,
		"preserveConstEnums": true,

		"outDir": "dist"
	},

	"include": [
		"src/*.ts",
		"wasm/*.d.ts"
	],
  }

There is no node or browser with tsc.

ES2015 is the target that pretty much any browser still relevant supports (aka ES6, aka ESM). Newest browsers also support ES2020 (aka ESNEXT). If you want to target Node, you'd either also use ES2020 or COMMONJS. COMMONJS is the default module system of Node, but since ES2015-modules are a language feature of ES6, every runtime that supports ES6 must support those, regardless if browser or Node.

@davidstevens37
Copy link
Contributor Author

@CodingMarkus hey, sorry i completely forgot to respond to this.

  • bun add buffer

Scenario A

import { Buffer } from "buffer";
  • bun build index.js

Scenario B

import { Buffer } from "./node_modules/buffer/index.js";
  • bun build index.js

Scenario A outputs the error you're experiencing. Scenario B builds successfully. I'm assuming the node builtins are taking precedence over node_modules. To overcome you're issue, simply reference the library directly instead of using the default module resolution.

I'm not sure what the correct resolution precedence is supposed to be, but both bun and node behave the same way in this scenario. Perhaps tsc deviates in the module resolution ordering.


import { Buffer as thirdPartyBuffer } from "./node_modules/buffer/index.js";
console.log(thirdPartyBuffer === global.Buffer);
  • bun run index.js // outputs false
  • node index.js // outputs false

import { Buffer as maybeThirdPartyBuffer } from "buffer";
console.log(maybeThirdPartyBuffer === global.Buffer);
  • bun run index.js // outputs true
  • node index.js // outputs true

@7flash
Copy link

7flash commented Nov 24, 2024

I'm currently experiencing this issue, and cannot reference buffer by relative path, because its being referenced inside of another library dependency. Please, any workaround I can apply to resolve this?

Jarred-Sumner added a commit that referenced this issue Nov 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working typescript Something for TypeScript
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants