denoland / deno

A modern runtime for JavaScript and TypeScript.
https://deno.com
MIT License
93.98k stars 5.23k forks source link

TypeError: Unable to find register Node-API module at /app/node_modules/bcrypt/lib/binding/napi-v3/bcrypt_lib.node #24906

Open ArmaanAS opened 1 month ago

ArmaanAS commented 1 month ago

Version: Deno 1.45.5

When using the --watch or --unstable-hmr flag on a script which uses the bcrypt NPM package, bcrypt fails to import after restart.

npm init -y
npm i bcrypt @types/bcrypt
// script.ts
import bcrypt from "bcrypt";

console.log(await bcrypt.hash("password", 12));
deno run -A --unstable-hmr --unstable-byonm script.ts

On first run

$2b$12$6T2wvFYrx2jLahEC.l/Z.uk8IPUsXHrMFJ0G6LEK9BFvB6qUEYV6S

After restart due to file change

TypeError: Unable to find register Node-API module at /app/node_modules/bcrypt/lib/binding/napi-v3/bcrypt_lib.node
    at Object.Module._extensions..node (node:module:774:20)
    at Module.load (node:module:655:32)
    at Function.Module._load (node:module:523:12)
    at Module.require (node:module:674:19)
    at require (node:module:785:16)
    at Object.<anonymous> (file:///app/node_modules/bcrypt/bcrypt.js:6:16)
    at Object.<anonymous> (file:///app/node_modules/bcrypt/bcrypt.js:238:4)
    at Module._compile (node:module:730:34)
    at Object.Module._extensions..js (node:module:744:10)
    at Module.load (node:module:655:32)
lucacasonato commented 1 month ago

Tenative idea to fix: add a cache to NAPI / FFI to not load the same module multiple times across isolate restarts.

lucacasonato commented 3 weeks ago

What platform are you on? The original reproducer works for me on macOS.

ArmaanAS commented 3 weeks ago

It happens on WSL but also in a Docker container using the denoland/deno image.

HMR Process started.
$2b$10$4oPUOtBp8tIzp4nUwx270O/3atKdPISkfsLt3LQySORLO/mj5j1CO
HMR Process finished. Restarting on file change...
HMR File change detected! Restarting!
error: Uncaught (in promise) TypeError: Unable to find register Node-API module at /home/user/deno/node_modules/bcrypt/lib/binding/napi-v3/bcrypt_lib.node
    at Object.Module._extensions..node (node:module:787:21)
    at Module.load (node:module:655:32)
    at Function.Module._load (node:module:523:13)
    at Module.require (node:module:674:19)
    at require (node:module:798:16)
    at Object.<anonymous> (file:///home/user/deno/node_modules/bcrypt/bcrypt.js:6:16)
    at Object.<anonymous> (file:///home/user/deno/node_modules/bcrypt/bcrypt.js:238:4)
    at Module._compile (node:module:736:34)
    at Object.Module._extensions..js (node:module:757:11)
    at Module.load (node:module:655:32)
HMR Process failed. Restarting on file change...