{"id":16994580,"url":"https://github.com/dandv/typescript-modern-project","last_synced_at":"2026-03-10T11:05:35.933Z","repository":{"id":38844838,"uuid":"239083173","full_name":"dandv/typescript-modern-project","owner":"dandv","description":"TypeScript+ESLint+Jest project template to import modules without extension, and modules with default exports that use node built-ins","archived":false,"fork":false,"pushed_at":"2023-01-06T02:43:32.000Z","size":392,"stargazers_count":47,"open_issues_count":13,"forks_count":5,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-04T08:11:34.447Z","etag":null,"topics":["jest","node","nodejs","typescript"],"latest_commit_sha":null,"homepage":"https://dev.to/dandv/typescript-settings-for-modern-projects-4596","language":"TypeScript","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/dandv.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}},"created_at":"2020-02-08T06:51:09.000Z","updated_at":"2025-03-27T16:11:12.000Z","dependencies_parsed_at":"2023-02-05T03:30:50.853Z","dependency_job_id":null,"html_url":"https://github.com/dandv/typescript-modern-project","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/dandv/typescript-modern-project","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dandv%2Ftypescript-modern-project","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dandv%2Ftypescript-modern-project/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dandv%2Ftypescript-modern-project/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dandv%2Ftypescript-modern-project/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dandv","download_url":"https://codeload.github.com/dandv/typescript-modern-project/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dandv%2Ftypescript-modern-project/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30331654,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-10T05:25:20.737Z","status":"ssl_error","status_checked_at":"2026-03-10T05:25:17.430Z","response_time":106,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["jest","node","nodejs","typescript"],"created_at":"2024-10-14T03:45:58.009Z","updated_at":"2026-03-10T11:05:35.910Z","avatar_url":"https://github.com/dandv.png","language":"TypeScript","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"readme":"# Modern TypeScript project template\n\nMinimalistic example of configuring TypeScript and Node to:\n* emit modern ES modules code\n* import modules that use Node built-ins\n* import modules that don't have named exports (e.g. [`apollo-server`](https://github.com/apollographql/apollo-server/issues/1356#issuecomment-565277759), [`node-influx`](https://github.com/node-influx/node-influx/issues/298))\n* import your own modules without specifying an extension\n* lint with ESLint, with TypeScript support\n* test the TypeScript code instantly without having to build first\n* run the resulting JavaScript code, with support for the optional chaining operator `?.`\n\nBonus: continuous integration script for GitHub Actions. It automatically runs tests on every pushed commit.\n\nThere is no need for Babel.\n\n# Emit ES modules code\n\nIn [`tsconfig.json`](tsconfig.json), set this in `compilerOptions`:\n\n```json\n    \"target\": \"esnext\",\n    \"module\": \"esnext\",  // Output `import`/`export` ES modules\n```\n\n\n# Import modules that use Node built-ins (`http`, `url` etc.)\n\n* run `npm install --save-dev @types/node`\n* in `tsconfig.json` under `compilerOptions`, set\n  * `\"moduleResolution\": \"node\"`, so `tsc` can find modules [when targeting ES6+](https://github.com/Microsoft/TypeScript/issues/8189) \n  * `\"types\": [\"node\"]` to avoid errors related to Node built-in modules  \n\n\n# Import modules that don't have named exports\n\nNormally we could write in TypeScript\n\n    import { InfluxDB } from 'influx';\n\nbut when generating ES modules code, that statement will be passed through as is, and will cause Node to fail with\n\n\u003e SyntaxError: The requested module 'influx' does not provide an export named 'InfluxDB'\n\nbecause [`node-influx` doesn't provide named exports](https://github.com/node-influx/node-influx/issues/298) (and neither does an even more popular module, [`apollo-server`](https://github.com/apollographql/apollo-server/issues/1356#issuecomment-565277759)).\n\nOne alternative would be to generate old ugly commonjs modules code by,\n\n* removing the `\"type\": \"module\"` line from `package.json`, and\n* changing the module line to `\"module\": \"CommonJS\"` in `tsconfig.json` (`allowSyntheticDefaultImports` also becomes unnecessary)\n\nWhat we'll do is import the entire module:\n\n```js\nimport Influx from 'influx';\nconst influx = new Influx.InfluxDB();\n```\n\nHowever, this will generate `Error TS1192: Module '...' has no default export.` To prevent that, set `\"allowSyntheticDefaultImports\": true` in `tsconfig.json`.\n\n\n# Import your own modules without specifying an extension\n\nWhen transpiling, [TypeScript won't generate an extension for you](https://github.com/microsoft/TypeScript/issues/16577). Run Node with the [`node --experimental-specifier-resolution=node` parameter](https://nodejs.org/api/cli.html#cli_experimental_specifier_resolution_mode):\n\n    node --experimental-specifier-resolution=node run.js\n    \nOtherwise, [node mandates that you specify the extension](https://nodejs.org/api/esm.html#esm_mandatory_file_extensions) in the `import` statement.\n\n\n# Optional chaining\n\nTo support optional chaining, add the `--harmony` flag to the node command line.\n\n\n# Run the resulting JavaScript code\n\nAdd `\"type\": \"module\"` to `package.json`, because [TypeScript can't generate files with the .mjs extension](https://github.com/microsoft/TypeScript/issues/18442#issuecomment-581738714).\n\n\n# ESLint\n\nTo be able to run `eslint`, we must create an `.eslintrc.cjs` file, rather than a `.js` one (due to `\"type\": \"module\"` in `package.json`). Then, install the required dependencies:\n\n    npm i -D eslint @typescript-eslint/eslint-plugin @typescript-eslint/parser\n\nHere's the [diff to add ESLint support](https://github.com/dandv/typescript-modern-project/commit/f816fe6e8d83ce554bd3066ac6638fb4406e917f).\n \n\n# Testing with Jest\n\nThe cleanest way to fully support Jest with TypeScript, ES Modules and ESLint, is to use `ts-jest`, which has [a number of advantages over using Babel](https://github.com/kulshekhar/ts-jest#ts-jest). What we need to do:\n\n* `npm install --save-dev jest @types/jest eslint-plugin-jest` - for ES Lint support\n* add `\"jest\"` to the `types` array in `tsconfig.json`\n* add the `'jest'` plugin to `.eslintrc.cjs` and also add `'jest/globals': true` to its `env` key\n* use the [`jest.config.cjs`](jest.config.cjs) generated by ts-jest `config:init` (just renamed .js -\u003e .cjs).\n\nNormally, to run Jest from `package.json`, we'd add a `\"test\": \"jest\"` line. That won't be sufficient, because we need to pass the `--harmony` flag to node (for optional chaining support). \nTo pass parameters to Node when running Jest, we'll add the following `test` line:\n\n    \"test\": \"node --harmony node_modules/.bin/jest\"\n\nThe only caveat here is that Jest seems to prefer generated `.js` files over their `.ts` originals, so we'll exclude them via [`jest.config.cjs`](jest.config.cjs):\n\n```\n  testRegex: '.*.test.ts',  // test filenames matching this regex\n  moduleFileExtensions: ['ts', 'js'],  // modules are only in .ts files, but 'js' *must* be specified too\n``` \n\n\n# Source maps\n\nIf your script generates an error, you'll see the line numbers from the generated `.js` files, which is not helpful. We want to see the original paths and line numbers from the `.ts` files. To do that, we'll add `sourceMap: true` to `tsconfig.json`, install [`source-map-support`](https://www.npmjs.com/package/source-map-support) and run node with the `-r source-map-support/register` parameter. Note that Jest already takes care of source mapping so you'll see the `.ts` line numbers without having to do anything extra.\n \nHere's [the diff to add source map support](https://github.com/dandv/typescript-modern-project/commit/4e31278833f2ce07f474d9c6348bb4509082ee97).\n\n\n## CI testing\n\nUsing [GitHub Actions](https://github.com/features/actions), we can configure automatic testing via `.yml` files under [.github/workflows](.github/workflows).\n\n\n## TODO\n\nThe `tsconfig.json` settings generate `.js` built files, and `.js.map` source map files, next to the original `.ts` file. While some IDEs conveniently hide these files, it may be desirable to output them in a separate directory, typically `dist`.\n\n This can be done using the `rootDir`/`outDir` settings in `tsconfig.json`, but that sort of setup comes with an [annoying limitation](https://github.com/microsoft/TypeScript/issues/9858) of Typescript that [forbids importing files outside the `rootDir`](https://stackoverflow.com/questions/52121725/maintain-src-folder-structure-when-building-to-dist-folder-with-typescript-3). That can be a [problem with monorepos](https://github.com/microsoft/TypeScript/issues/17611).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdandv%2Ftypescript-modern-project","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdandv%2Ftypescript-modern-project","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdandv%2Ftypescript-modern-project/lists"}