npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

node-ts-modules

v0.0.3

Published

Enable direct TypeScript imports from node_modules using Node.js --experimental-strip-types

Downloads

12

Readme

node-ts-modules

Enable direct TypeScript imports from node_modules using Node.js --experimental-strip-types flag without requiring a separate compilation step.

Motivation

Node.js introduced the --experimental-strip-types flag which allows running TypeScript files directly without compilation. However, Node.js doesn't allow direct importing of TypeScript files from node_modules. This package provides a workaround by creating a dedicated ts_modules folder that mirrors TypeScript-enabled packages.

This project was inspired by discussions in:

How It Works

Key Concepts

1. Decentralized Registration

Each TypeScript-enabled package adds a postinstall step (using the CLI tool from this package) to create its own symlink in the project's ts_modules folder. This avoids scanning the entire node_modules directory.

2. Custom Module Resolution

A custom loader intercepts module imports:

  • For CommonJS: Monkey-patches Node's resolution to check ts_modules first
  • For ESM: Uses an experimental loader hook

When a bare import (e.g., require('my-package')) is made, the loader checks if a corresponding symlink exists in ts_modules and, if so, resolves the package from there.

3. Directory Resolution

If the symlinked package is a directory, the loader looks for a defined entry file using:

  1. A custom "ts:main" field in package.json
  2. Falling back to "main" field
  3. Looking for index.ts or index.js

This ensures Node loads a file, not a directory.

Installation

# Using npm
npm install node-ts-modules

# Using Yarn
yarn add node-ts-modules

Usage

For Application Developers

  1. Install the package as shown above.

  2. Run your application with the appropriate loader:

    For CommonJS:

    # Directly with Node.js
    node -r ./node_modules/node-ts-modules/loader-cjs.js your-script.js

    For ESM:

    # Directly with Node.js
    node --experimental-loader=node_modules/node-ts-modules/loader-esm.mjs --experimental-strip-types your-script.js

For Package Authors

To make your TypeScript package compatible with node-ts-modules:

  1. Add node-ts-modules as a dependency:

    # Using npm
    npm install node-ts-modules
       
    # Using Yarn
    yarn add node-ts-modules
  2. Add a postinstall script to your package.json:

    {
      "scripts": {
        "postinstall": "node-ts-modules-postinstall"
      }
    }
  3. Optionally, add a "ts:main" field to your package.json to specify the TypeScript entry point:

    {
      "main": "dist/index.js",
      "ts:main": "src/index.ts"
    }

How Module Resolution Works

  1. When a bare import is encountered (e.g., import { something } from 'package-name'), the loader intercepts it.
  2. The loader checks if a corresponding symlink exists in the ts_modules directory.
  3. If found, it resolves the import to the TypeScript source file instead of the compiled JavaScript in node_modules.
  4. Node.js then uses the --experimental-strip-types flag to strip the types and execute the TypeScript file directly.

Limitations

  • Requires Node.js version 22 or higher
  • The --experimental-strip-types flag is still experimental
  • Not all TypeScript features are supported by the type stripping mechanism
  • Package authors need to explicitly opt-in by adding the postinstall script

Best Practices

Add ts_modules to .gitignore

The ts_modules directory is created at runtime and contains symlinks specific to your environment. It should not be committed to version control. Make sure to add it to your .gitignore file:

# ts-modules specific
ts_modules/

License

MIT