denoland / deno

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

node:module register is not a function #23201

Open ry opened 7 months ago

ry commented 7 months ago

Version: deno 1.42.1+92a8ada

https://nodejs.org/docs/latest/api/module.html#moduleregisterspecifier-parenturl-options

# npm init @observablehq
# cd hello-framework
# deno task dev --debug
Task dev observable preview "--debug"

Unexpected error: N.register is not a function
The full error follows

error: Uncaught (in promise) TypeError: N.register is not a function
    at C (file:///Users/ry/src/hello-framework/node_modules/.deno/@observablehq+framework@1.2.0/node_modules/tsx/dist/esm/index.mjs:1:695)
    at file:///Users/ry/src/hello-framework/node_modules/.deno/@observablehq+framework@1.2.0/node_modules/tsx/dist/esm/index.mjs:5:1913
bartlomieju commented 7 months ago

This is trouble, it's essentially custom loaders: https://nodejs.org/docs/latest/api/module.html#customization-hooks that need to be executed before the actual code is executed.

marvinhagemeister commented 7 months ago

I wonder if it might be best to bypass this module somehow. The sole purpose of the tsx module is to allow node to work natively with TypeScript files.

marvinhagemeister commented 3 months ago

The the observable package only used module.register() to add support for running TS code, this works now thanks to https://github.com/denoland/deno/pull/24965 . There the function is merely stubbed, but that's enough to unblock observable.

terrablue commented 3 months ago

I would benefit from Deno supporting that. @bartlomieju FWIW, Node has a static and dynamic mode for that. With the --import flag, it indeed needs to run before any code is loaded, but programmatically (this issue's case), it can be executed at any time in the lifecycle of the program, but then only applies to thereafter dynamically imported modules.

Interestingly enough, normal (static) imports from post-registration dynamically loaded modules seem to also work in my tests.