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

[napi] current require() implement stackTrace may not correct #19130

Closed
loynoir opened this issue May 15, 2023 · 5 comments · Fixed by #24727
Closed

[napi] current require() implement stackTrace may not correct #19130

loynoir opened this issue May 15, 2023 · 5 comments · Fixed by #24727
Assignees
Labels
bug Something isn't working correctly node compat

Comments

@loynoir
Copy link

loynoir commented May 15, 2023

Given

module.exports.foobar = require('bindings')('foobar')

Actual

Looking for wrong bindings/1.5.0/build/foobar.node.

detail
> var mod = await import('npm:[email protected]')
Uncaught Error: Could not locate the bindings file. Tried:
  /home/vscode/.cache/deno/npm/private-npm/bindings/1.5.0/build/foobar.node
  /home/vscode/.cache/deno/npm/private-npm/bindings/1.5.0/build/Debug/foobar.node
  /home/vscode/.cache/deno/npm/private-npm/bindings/1.5.0/build/Release/foobar.node
  /home/vscode/.cache/deno/npm/private-npm/bindings/1.5.0/out/Debug/foobar.node
  /home/vscode/.cache/deno/npm/private-npm/bindings/1.5.0/Debug/foobar.node
  /home/vscode/.cache/deno/npm/private-npm/bindings/1.5.0/out/Release/foobar.node
  /home/vscode/.cache/deno/npm/private-npm/bindings/1.5.0/Release/foobar.node
  /home/vscode/.cache/deno/npm/private-npm/bindings/1.5.0/build/default/foobar.node
  /home/vscode/.cache/deno/npm/private-npm/bindings/1.5.0/compiled/18.12.1/linux/x64/foobar.node
  /home/vscode/.cache/deno/npm/private-npm/bindings/1.5.0/addon-build/release/install-root/foobar.node
  /home/vscode/.cache/deno/npm/private-npm/bindings/1.5.0/addon-build/debug/install-root/foobar.node
  /home/vscode/.cache/deno/npm/private-npm/bindings/1.5.0/addon-build/default/install-root/foobar.node
  /home/vscode/.cache/deno/npm/private-npm/bindings/1.5.0/lib/binding/node-v108-linux-x64/foobar.node
    at bindings (file:///home/vscode/.cache/deno/npm/private-npm/bindings/1.5.0/bindings.js:126:9)
    at Object.<anonymous> (file:///home/vscode/.cache/deno/npm/private-npm/napi-example/1.0.0/internal/extension/cjs-wrapper.cjs:1:286)
    at Object.<anonymous> (file:///home/vscode/.cache/deno/npm/private-npm/napi-example/1.0.0/internal/extension/cjs-wrapper.cjs:17:4)
    at Module._compile (ext:deno_node/01_require.js:968:34)
    at Object.Module._extensions..js (ext:deno_node/01_require.js:1001:10)
    at Module.load (ext:deno_node/01_require.js:879:32)
    at Function.Module._load (ext:deno_node/01_require.js:713:12)
    at Module.require (ext:deno_node/01_require.js:901:19)
    at require (ext:deno_node/01_require.js:1041:16)
    at file:///home/vscode/.cache/deno/npm/private-npm/napi-example/1.0.0/internal/extension/cjs-wrapper.cjs:3:13

Expected

Looking for correct napi-example/1.0.0/build/foobar.node.

detail
 → /home/vscode/.cache/deno/npm/private-npm/napi-example/1.0.0/build/foobar.node
 → /home/vscode/.cache/deno/npm/private-npm/napi-example/1.0.0/build/Debug/foobar.node
 → /home/vscode/.cache/deno/npm/private-npm/napi-example/1.0.0/build/Release/foobar.node
 → /home/vscode/.cache/deno/npm/private-npm/napi-example/1.0.0/out/Debug/foobar.node
 → /home/vscode/.cache/deno/npm/private-npm/napi-example/1.0.0/Debug/foobar.node
 → /home/vscode/.cache/deno/npm/private-npm/napi-example/1.0.0/out/Release/foobar.node
 → /home/vscode/.cache/deno/npm/private-npm/napi-example/1.0.0/Release/foobar.node
 → /home/vscode/.cache/deno/npm/private-npm/napi-example/1.0.0/build/default/foobar.node
 → /home/vscode/.cache/deno/npm/private-npm/napi-example/1.0.0/compiled/18.12.1/linux/x64/foobar.node
 → /home/vscode/.cache/deno/npm/private-npm/napi-example/1.0.0/addon-build/release/install-root/foobar.node
 → /home/vscode/.cache/deno/npm/private-npm/napi-example/1.0.0/addon-build/debug/install-root/foobar.node
 → /home/vscode/.cache/deno/npm/private-npm/napi-example/1.0.0/addon-build/default/install-root/foobar.node
 → /home/vscode/.cache/deno/npm/private-npm/napi-example/1.0.0/lib/binding/node-v108-linux-x64/foobar.node

Related

https://github.com/TooTallNate/node-bindings/blob/1.5.0/bindings.js#L82

Workaround

module.exports.foobar = require('../hard/coded/path/to/foobar.node')
@ealib
Copy link

ealib commented May 16, 2023

Perhaps it is related to the missing functionality of importing native Node.js modules. See issue #17246 .

@loynoir
Copy link
Author

loynoir commented May 16, 2023

@ealib

No, I think core logic behind two issues are totally different.


First, I think you made a mistake that testing deno and node without controling variates.

  • Node CJS require('/path/to/reproduce.node')

  • Deno ESM import('/path/to/reproduce.node')

Did you try Node ESM import('/path/to/reproduce.node')?

$ node 
Welcome to Node.js v18.15.0.
Type ".help" for more information.
> await import('./node_modules/foobar/build/Release/foobar.node')
Uncaught:
TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".node" for /path/to/node_modules/foobar/build/Release/foobar.node
    at __node_internal_captureLargerStackTrace (node:internal/errors:490:5)
    at new NodeError (node:internal/errors:399:5)
    at Object.getFileProtocolModuleFormat [as file:] (node:internal/modules/esm/get_format:79:11)
    at defaultGetFormat (node:internal/modules/esm/get_format:121:38)
    at defaultLoad (node:internal/modules/esm/load:81:20)
    at nextLoad (node:internal/modules/esm/loader:163:28)
    at ESMLoader.load (node:internal/modules/esm/loader:605:26)
    at ESMLoader.moduleProvider (node:internal/modules/esm/loader:457:22)
    at new ModuleJob (node:internal/modules/esm/module_job:64:26)
    at #createModuleJob (node:internal/modules/esm/loader:480:17)
    at ESMLoader.getModuleJob (node:internal/modules/esm/loader:434:34)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  code: 'ERR_UNKNOWN_FILE_EXTENSION'
}

That is not supported in Node, which makes it no sense to let deno support it.


Perhaps it is related to the missing functionality of importing native Node.js modules

No, it is not missing. It is supported, but not perfect.

$ deno repl -A 
Deno 1.33.3
exit using ctrl+d, ctrl+c, or close()
> var mod = await import('npm:[email protected]/cjs-wrapper.cjs')
undefined
> mod.some_fn('...')
"...ok..."

The reason why I say not perfect, is the reason why I open this issue.

In node, require('bindings')('foobar')

In deno, should use workaround require('../relative/path/to/Release/foobar.node')

@bartlomieju bartlomieju added bug Something isn't working correctly node compat labels May 16, 2023
@bartlomieju bartlomieju self-assigned this May 16, 2023
@ealib
Copy link

ealib commented May 16, 2023

@loynoir excuse me, but I do not understand what you mean by "controling variates".

I understand, however, that the bug you report here has nothing to do with #17246 , but is related to the implementation of import in Deno.

I did not intentionally try Node ESM import, because I do not want to maintain double-sided code since Deno should make the use of Node.js modules prefixed with "npm:..." transparent.

I mean, if @bartlomieju says that import ... from "npm: ..." will never be able to distinguish between ES/TS source modules and native binary modules, then I'll be content and use import.

@bartlomieju
Copy link
Member

@loynoir any chance you could provide a reproducible example (or a repo) that I could try for debugging this problem?

@reggi
Copy link

reggi commented Mar 29, 2024

@bartlomieju This?

➜  ~ deno eval 'import "npm:sqlite3"'
error: Uncaught (in promise) Error: Could not locate the bindings file. Tried:
 → /Users/thomasreggi/Library/Caches/deno/npm/registry.npmjs.org/bindings/1.5.0/build/node_sqlite3.node
 → /Users/thomasreggi/Library/Caches/deno/npm/registry.npmjs.org/bindings/1.5.0/build/Debug/node_sqlite3.node
 → /Users/thomasreggi/Library/Caches/deno/npm/registry.npmjs.org/bindings/1.5.0/build/Release/node_sqlite3.node
 → /Users/thomasreggi/Library/Caches/deno/npm/registry.npmjs.org/bindings/1.5.0/out/Debug/node_sqlite3.node
 → /Users/thomasreggi/Library/Caches/deno/npm/registry.npmjs.org/bindings/1.5.0/Debug/node_sqlite3.node
 → /Users/thomasreggi/Library/Caches/deno/npm/registry.npmjs.org/bindings/1.5.0/out/Release/node_sqlite3.node
 → /Users/thomasreggi/Library/Caches/deno/npm/registry.npmjs.org/bindings/1.5.0/Release/node_sqlite3.node
 → /Users/thomasreggi/Library/Caches/deno/npm/registry.npmjs.org/bindings/1.5.0/build/default/node_sqlite3.node
 → /Users/thomasreggi/Library/Caches/deno/npm/registry.npmjs.org/bindings/1.5.0/compiled/20.11.1/darwin/arm64/node_sqlite3.node
 → /Users/thomasreggi/Library/Caches/deno/npm/registry.npmjs.org/bindings/1.5.0/addon-build/release/install-root/node_sqlite3.node
 → /Users/thomasreggi/Library/Caches/deno/npm/registry.npmjs.org/bindings/1.5.0/addon-build/debug/install-root/node_sqlite3.node
 → /Users/thomasreggi/Library/Caches/deno/npm/registry.npmjs.org/bindings/1.5.0/addon-build/default/install-root/node_sqlite3.node
 → /Users/thomasreggi/Library/Caches/deno/npm/registry.npmjs.org/bindings/1.5.0/lib/binding/node-v108-darwin-arm64/node_sqlite3.node
    at bindings (file:///Users/thomasreggi/Library/Caches/deno/npm/registry.npmjs.org/bindings/1.5.0/bindings.js:126:9)
    at Object.<anonymous> (file:///Users/thomasreggi/Library/Caches/deno/npm/registry.npmjs.org/sqlite3/5.1.7/lib/sqlite3-binding.js:1:291)
    at Object.<anonymous> (file:///Users/thomasreggi/Library/Caches/deno/npm/registry.npmjs.org/sqlite3/5.1.7/lib/sqlite3-binding.js:3:4)
    at Module._compile (node:module:718:34)
    at Object.Module._extensions..js (node:module:732:10)
    at Module.load (node:module:643:32)
    at Function.Module._load (node:module:524:12)
    at Module.require (node:module:662:19)
    at require (node:module:776:16)
    at Object.<anonymous> (file:///Users/thomasreggi/Library/Caches/deno/npm/registry.npmjs.org/sqlite3/5.1.7/lib/sqlite3.js:2:17)
➜  ~ 

dsherret pushed a commit that referenced this issue Jul 26, 2024
Adds support for `npm:bindings` and `npm:callsites` packages because of
changes in
denoland/deno_core#838.

This `deno_core` bump causes us to stop prepending `file://` scheme for
locations
in stack traces that are for local files.

Fixes #24462 , fixes
#22671 , fixes
#15717 , fixes
#19130 , fixes
WiseLibs/better-sqlite3#1205 , fixes
WiseLibs/better-sqlite3#1034 , fixes
#20936

---------

Co-authored-by: Nathan Whitaker <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working correctly node compat
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants