{"id":26738295,"url":"https://github.com/devthefuture-org/node-ts-modules","last_synced_at":"2025-03-28T03:04:15.524Z","repository":{"id":284591942,"uuid":"954757378","full_name":"devthefuture-org/node-ts-modules","owner":"devthefuture-org","description":null,"archived":false,"fork":false,"pushed_at":"2025-03-26T16:40:53.000Z","size":9,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-03-26T17:41:29.787Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/devthefuture-org.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-03-25T15:12:41.000Z","updated_at":"2025-03-26T16:40:57.000Z","dependencies_parsed_at":"2025-03-26T17:41:35.065Z","dependency_job_id":"b28c8ddc-4dbe-493e-af9f-64a716392462","html_url":"https://github.com/devthefuture-org/node-ts-modules","commit_stats":null,"previous_names":["devthefuture-org/node-ts-modules"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devthefuture-org%2Fnode-ts-modules","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devthefuture-org%2Fnode-ts-modules/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devthefuture-org%2Fnode-ts-modules/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devthefuture-org%2Fnode-ts-modules/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/devthefuture-org","download_url":"https://codeload.github.com/devthefuture-org/node-ts-modules/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245954618,"owners_count":20699869,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-03-28T03:02:19.157Z","updated_at":"2025-03-28T03:04:15.484Z","avatar_url":"https://github.com/devthefuture-org.png","language":"JavaScript","readme":"# node-ts-modules\n\nEnable direct TypeScript imports from node_modules using Node.js `--experimental-strip-types` flag without requiring a separate compilation step.\n\n## Motivation\n\nNode.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.\n\nThis project was inspired by discussions in:\n- [Node.js Issue #57215: Support for importing TypeScript files from node_modules](https://github.com/nodejs/node/issues/57215)\n- [Reddit Discussion on TypeScript publishing practices](https://www.reddit.com/r/programmingcirclejerk/comments/1epsups/to_discourage_package_authors_from_publishing/)\n\n## How It Works\n\n### Key Concepts\n\n#### 1. Decentralized Registration\n\nEach 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.\n\n#### 2. Custom Module Resolution\n\nA custom loader intercepts module imports:\n- For CommonJS: Monkey-patches Node's resolution to check `ts_modules` first\n- For ESM: Uses an experimental loader hook\n\nWhen 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.\n\n#### 3. Directory Resolution\n\nIf the symlinked package is a directory, the loader looks for a defined entry file using:\n1. A custom `\"ts:main\"` field in package.json\n2. Falling back to `\"main\"` field\n3. Looking for `index.ts` or `index.js`\n\nThis ensures Node loads a file, not a directory.\n\n## Installation\n\n```bash\n# Using npm\nnpm install node-ts-modules\n\n# Using Yarn\nyarn add node-ts-modules\n```\n\n## Usage\n\n### For Application Developers\n\n1. Install the package as shown above.\n\n2. Run your application with the appropriate loader:\n\n   **For CommonJS:**\n   ```bash\n   # Directly with Node.js\n   node -r ./node_modules/node-ts-modules/loader-cjs.js your-script.js\n   ```\n\n   **For ESM:**\n   ```bash\n   # Directly with Node.js\n   node --experimental-loader=node_modules/node-ts-modules/loader-esm.mjs --experimental-strip-types your-script.js\n   ```\n\n### For Package Authors\n\nTo make your TypeScript package compatible with `node-ts-modules`:\n\n1. Add `node-ts-modules` as a dependency:\n   ```bash\n   # Using npm\n   npm install node-ts-modules\n   \n   # Using Yarn\n   yarn add node-ts-modules\n   ```\n\n2. Add a postinstall script to your package.json:\n   ```json\n   {\n     \"scripts\": {\n       \"postinstall\": \"node-ts-modules-postinstall\"\n     }\n   }\n   ```\n\n3. Optionally, add a `\"ts:main\"` field to your package.json to specify the TypeScript entry point:\n   ```json\n   {\n     \"main\": \"dist/index.js\",\n     \"ts:main\": \"src/index.ts\"\n   }\n   ```\n\n## How Module Resolution Works\n\n1. When a bare import is encountered (e.g., `import { something } from 'package-name'`), the loader intercepts it.\n2. The loader checks if a corresponding symlink exists in the `ts_modules` directory.\n3. If found, it resolves the import to the TypeScript source file instead of the compiled JavaScript in `node_modules`.\n4. Node.js then uses the `--experimental-strip-types` flag to strip the types and execute the TypeScript file directly.\n\n## Limitations\n\n- Requires Node.js version 22 or higher\n- The `--experimental-strip-types` flag is still experimental\n- Not all TypeScript features are supported by the type stripping mechanism\n- Package authors need to explicitly opt-in by adding the postinstall script\n\n## Best Practices\n\n### Add ts_modules to .gitignore\n\nThe `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:\n\n```\n# ts-modules specific\nts_modules/\n```\n\n## License\n\nMIT\n","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevthefuture-org%2Fnode-ts-modules","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdevthefuture-org%2Fnode-ts-modules","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevthefuture-org%2Fnode-ts-modules/lists"}