{"id":17028411,"url":"https://github.com/bennycode/ts2esm","last_synced_at":"2025-04-05T14:04:41.197Z","repository":{"id":203570123,"uuid":"709773543","full_name":"bennycode/ts2esm","owner":"bennycode","description":"Transforms CommonJS projects into ESM.","archived":false,"fork":false,"pushed_at":"2025-03-01T01:47:48.000Z","size":892,"stargazers_count":69,"open_issues_count":5,"forks_count":3,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-29T13:05:16.735Z","etag":null,"topics":["cjs","codemod","declaration","esm","extension","hacktoberfest","js","tsc","typescript"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/ts2esm","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bennycode.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":null,"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":"2023-10-25T11:23:51.000Z","updated_at":"2025-03-13T02:16:00.000Z","dependencies_parsed_at":null,"dependency_job_id":"717ac37a-d4cc-4bb0-b299-f8f8c9e65880","html_url":"https://github.com/bennycode/ts2esm","commit_stats":null,"previous_names":["bennycode/ts2esm"],"tags_count":27,"template":false,"template_full_name":"bennycode/ts-node-starter","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bennycode%2Fts2esm","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bennycode%2Fts2esm/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bennycode%2Fts2esm/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bennycode%2Fts2esm/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bennycode","download_url":"https://codeload.github.com/bennycode/ts2esm/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247345850,"owners_count":20924102,"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":["cjs","codemod","declaration","esm","extension","hacktoberfest","js","tsc","typescript"],"created_at":"2024-10-14T07:54:09.355Z","updated_at":"2025-04-05T14:04:41.180Z","avatar_url":"https://github.com/bennycode.png","language":"TypeScript","readme":"# ts2esm\n\nYou want to transform your TypeScript project into an ECMAScript module (ESM)? Look no further! This tool (`ts2esm`) converts your import and export declarations into ESM-compatible ones. It's the ideal tool for converting a CommonJS project to ESM. It also [works with plain JavaScript](https://github.com/bennycode/ts2esm/issues/20#issuecomment-1894702085) projects! 🪄\n\n## Installation\n\nSimply run this command to install `ts2esm` globally on your machine:\n\n```bash\nnpm i -g ts2esm\n```\n\nYou can also run it locally (without being globally installed):\n\n```bash\nnpx ts2esm\n```\n\n## Usage\n\nConvert your CommonJS projects (TypeScript or JavaScript) into ECMAScript modules with a single command. Just launch the program inside the directory of your project (it will ask you for your `tsconfig.json` path):\n\n```bash\nts2esm\n```\n\nYou can also provide a list of tsconfigs (no prompt):\n\n```bash\nts2esm packages/foo/tsconfig.json packages/bar/tsconfig.json\n```\n\nNote: The path can be specified absolutely (i.e. `/home/user/cornerstone3D/tsconfig.json`) or relative (i.e. `../../cornerstone3D/tsconfig.json`).\n\nThere is also a debug mode with verbose logging:\n\n```bash\nts2esm --debug\n```\n\n\u003e [!WARNING]  \n\u003e Make sure you have a backup (in Git or similar) of your code as \"ts2esm\" will modify your source code.\n\n\u003e [!IMPORTANT]  \n\u003e Use TypeScript 5.2 or later as there have been [breaking changes to the Node.js settings](https://devblogs.microsoft.com/typescript/announcing-typescript-5-2/#breaking-changes-and-correctness-fixes), which you don't want to miss.\n\n\u003e [!IMPORTANT]  \n\u003e Since TypeScript 5.3 import assertions are [replaced with import attributes](https://devblogs.microsoft.com/typescript/announcing-typescript-5-3-beta/#import-attributes).\n\n## Step-by-Step Guide\n\nThis workflow migrates a CommonJS project and checks its types:\n\n```bash\n# Build your project\nnpx tsc\n\n# Check your types\nnpx @arethetypeswrong/cli --pack .\n\n# Convert to ESM\nnpx ts2esm tsconfig.json\n\n# Rebuild your project\nnpx tsc\n\n# Check your types again\nnpx @arethetypeswrong/cli --pack . --ignore-rules cjs-resolves-to-esm\n```\n\n## Video Tutorial\n\nWatch this 5-minute video and learn how to migrate from CommonJS to ESM:\n\n[\u003cimg src=\"https://i.ytimg.com/vi_webp/bgGQgSQSpI8/mqdefault.webp\"\u003e](https://youtu.be/bgGQgSQSpI8)\n\n## Examples\n\nHere you can see the transformations that `ts2esm` applies.\n\n### Require Statements\n\nBefore:\n\n```ts\nconst fs = require('node:fs');\nconst path = require('path');\n```\n\nAfter:\n\n```ts\nimport fs from 'node:fs';\nimport path from 'path';\n```\n\n### Module Exports\n\nBefore:\n\n```ts\nconst Benny = 1;\nconst Code = 2;\n\nmodule.exports = Benny;\nmodule.exports.Code = Code;\n```\n\nAfter:\n\n```ts\nconst Benny = 1;\nconst Code = 2;\n\nexport default Benny;\nexport {Code};\n```\n\n### Import Declarations\n\nBefore:\n\n```ts\nimport {AccountAPI} from '../account';\nimport {RESTClient} from './client/RESTClient';\nimport {removeSuffix} from '@helpers/removeSuffix';\n```\n\nAfter:\n\n```ts\nimport {AccountAPI} from '../account/index.js';\nimport {RESTClient} from './client/RESTClient.js';\nimport {removeSuffix} from '@helpers/removeSuffix.js';\n```\n\n### Export Declarations\n\nBefore:\n\n```ts\nexport * from './account';\nexport * from './UserAPI';\n```\n\nAfter:\n\n```ts\nexport * from './account/index.js';\nexport * from './UserAPI.js';\n```\n\n### JSON Import Attributes\n\nBefore:\n\n```ts\nimport listAccounts from '../test/fixtures/listAccounts.json';\n```\n\nAfter:\n\n```ts\nimport listAccounts from '../test/fixtures/listAccounts.json' with {type: 'json'};\n```\n\n### CSS Import Attributes\n\nBefore:\n\n```ts\nimport styles from './MyComponent.module.css';\n```\n\nAfter:\n\n```ts\nimport styles from './MyComponent.module.css' with {type: 'css'};\n```\n\n## How it works\n\nThe `ts2esm` program adjusts your relative imports, adding extensions like `index.js` or `.js` to make them ESM-compatible. Say goodbye to import errors such as **TS2305**, **TS2307**, **TS2834**, and [**TS2835**](https://typescript.tv/errors/#ts2835)!\n\nErrors that get automatically fixed (🛠️):\n\n\u003e TypeError [ERR_IMPORT_ASSERTION_TYPE_MISSING]: Module needs an import assertion of type \"json\"\n\n\u003e error TS2834: Relative import paths need explicit file extensions in EcmaScript imports when '--moduleResolution' is 'node16' or 'nodenext'. Consider adding an extension to the import path.\n\n\u003e error TS2835: Relative import paths need explicit file extensions in EcmaScript imports when '--moduleResolution' is 'node16' or 'nodenext'.\n\n## Noteworthy\n\nWith ESM, you can no longer use Node.js objects like `__filename` or `__dirname`. Here is a simple snippet to replicate their behavior using the [import.meta property](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import.meta):\n\n```ts\nimport path from 'node:path';\nimport url from 'node:url';\n\nconst __filename = url.fileURLToPath(import.meta.url);\nconst __dirname = path.dirname(__filename);\n```\n\n## Credits\n\nThis program was born from an [inspiring conversation](https://twitter.com/bennycode/status/1693362836695585084) I had with [Basarat Ali Syed](https://twitter.com/basarat). I recommend checking out [Basarat's coding tutorials](https://www.youtube.com/@basarat). 👍\n\n## Attributions\n\n- ts2esm got highlighted in Deno's article on [How to convert CommonJS to ESM](https://deno.com/blog/convert-cjs-to-esm#tools-for-migrating)\n- ts2esm helped migrating [cornerstonejs/cornerstone3D](https://github.com/cornerstonejs/cornerstone3D) from CommonJS to ESM\n\n## Used By\n\n[\u003cimg src=\"https://ohif.org/static/c99ccbad57599dbf9f3490519c9b444f/63739/ohif-logo-dark.png\" width=\"256\"/\u003e](https://ohif.org/)\n\n## Vision\n\nIdeally, the extension change would be available as a [codefix in TypeScript](https://github.com/microsoft/TypeScript/tree/v5.3.3/src/services/codefixes) itself. Then all conversions could be applied using [ts-fix](https://github.com/microsoft/ts-fix).\n\n## References\n\n- [TypeScript's Module Resolution](https://www.typescriptlang.org/docs/handbook/modules/theory.html#module-resolution-is-host-defined)\n- [TypeScript AST Viewer](https://ts-ast-viewer.com/)\n- [Are the types wrong?](https://github.com/arethetypeswrong/arethetypeswrong.github.io)\n","funding_links":[],"categories":["typescript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbennycode%2Fts2esm","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbennycode%2Fts2esm","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbennycode%2Fts2esm/lists"}