Skip to content

ts-node/register not found when using global install #3

@ariburaco

Description

@ariburaco

Describe the bug

When running a code hook with Node.js v20 or v22 and ts-node installed globally, the following error occurs:

Error: Cannot find module 'ts-node/register'
Require stack:
- internal/preload
    at Function._resolveFilename (node:internal/modules/cjs/loader:1401:15)
    ...
Node.js v22.15.0 (also happens on v20.12.2)

To Reproduce

Steps to reproduce the behavior:

  1. Install ts-node globally:
    npm install -g ts-node
  2. Run a hook with:
    node -r ts-node/register --no-warnings "/path/to/hooks.user.ts" __run_hook Stop
    
  3. Observe the error above.

Expected behavior

The hook should run, and ts-node/register should be found as it was in older Node.js versions or when installed locally.

Additional context

  • This is not just a Node.js 22+ issue; even with Node.js 20, global modules are not resolved for the -r flag unless you are in a directory with a local install.
  • The workaround is to use the absolute path to the global ts-node/register/index.js, but this is not ergonomic.
  • Installing ts-node locally in the working directory also works, but is not always desirable.

Environment

  • Node.js: v22.15.0 and v20.12.2
  • ts-node: 10.9.2 (globally installed)
  • OS: macOS

Possible solutions

  • Document this Node.js behavior and recommend using the absolute path or local install.
  • Consider supporting a config option for specifying the preload path.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions