├── .github └── FUNDING.yml ├── .gitignore ├── README.md ├── eslint.config.mjs ├── jest.config.cjs ├── package-lock.json ├── package.json ├── src ├── index.test.ts └── index.ts └── tsconfig.json /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | github: [beforesemicolon]github: [beforesemicolon] 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # IntelliJ project files 2 | .idea 3 | *.iml 4 | out 5 | gen 6 | 7 | # VSCode 8 | .vscode/* 9 | !.vscode/settings.json 10 | !.vscode/tasks.json 11 | !.vscode/launch.json 12 | !.vscode/extensions.json 13 | !.vscode/*.code-snippets 14 | 15 | # Local History for Visual Studio Code 16 | .history/ 17 | 18 | # Built Visual Studio Code Extensions 19 | *.vsix 20 | 21 | # Folders 22 | dist 23 | build 24 | node_modules 25 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # node-typescript-project-template 2 | A simple template for a Node + Typescript server project 3 | 4 | This template offers additional setups. Check other branches or click following links: 5 | - [NPM TypeScript Node Package Template](https://github.com/beforesemicolon/node-typescript-project-template/tree/npm-package) 6 | - [TypeScript Node Server Template](https://github.com/beforesemicolon/node-typescript-project-template/tree/node-server) 7 | 8 | 9 | Read the Article about the initial setup 10 | 11 | [Blog Post](https://medium.com/before-semicolon/how-to-setup-a-typescript-nodejs-server-2023-16f3874f2ce5) 12 | -------------------------------------------------------------------------------- /eslint.config.mjs: -------------------------------------------------------------------------------- 1 | import typescriptEslint from "@typescript-eslint/eslint-plugin"; 2 | import tsParser from "@typescript-eslint/parser"; 3 | import path from "node:path"; 4 | import { fileURLToPath } from "node:url"; 5 | import js from "@eslint/js"; 6 | import { FlatCompat } from "@eslint/eslintrc"; 7 | 8 | const __filename = fileURLToPath(import.meta.url); 9 | const __dirname = path.dirname(__filename); 10 | const compat = new FlatCompat({ 11 | baseDirectory: __dirname, 12 | recommendedConfig: js.configs.recommended, 13 | allConfig: js.configs.all 14 | }); 15 | 16 | export default [ 17 | ...compat.extends("eslint:recommended", "plugin:@typescript-eslint/recommended", "prettier"), 18 | { 19 | plugins: { 20 | "@typescript-eslint": typescriptEslint, 21 | }, 22 | 23 | languageOptions: { 24 | parser: tsParser, 25 | }, 26 | }, 27 | ]; -------------------------------------------------------------------------------- /jest.config.cjs: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | transform: { 3 | '^.+\\.ts?$': 'ts-jest', 4 | }, 5 | testEnvironment: 'node', 6 | testRegex: './src/.*\\.(test|spec)?\\.(js|ts)$', 7 | moduleFileExtensions: ['ts', 'js', 'json', 'node'], 8 | roots: ['/src'], 9 | }; 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "node-typescript-project-template", 3 | "version": "1.0.0", 4 | "description": "", 5 | "type": "module", 6 | "scripts": { 7 | "clean": "rimraf ./build", 8 | "build": "npm-run-all check test clean && tsc", 9 | "start": "node ./build/src", 10 | "local": "tsx ./src", 11 | "local:watch": "nodemon ./src -e ts,json --exec 'npm run local'", 12 | "check": "eslint ./src && npx prettier --check ./src", 13 | "format": "eslint ./src --fix && npx prettier --write ./src", 14 | "test": "jest" 15 | }, 16 | "keywords": [ 17 | "typescript", 18 | "node", 19 | "eslint", 20 | "prettier", 21 | "jest", 22 | "supertest" 23 | ], 24 | "author": "Elson Correia @ Before Semicolon", 25 | "license": "MIT", 26 | "devDependencies": { 27 | "@types/jest": "^29.5.12", 28 | "@types/node": "^22.4.0", 29 | "@types/supertest": "^6.0.2", 30 | "@typescript-eslint/eslint-plugin": "^8.1.0", 31 | "@typescript-eslint/parser": "^8.1.0", 32 | "eslint": "^9.9.0", 33 | "eslint-config-prettier": "^9.1.0", 34 | "jest": "^29.7.0", 35 | "nodemon": "^3.1.4", 36 | "npm-run-all": "^4.1.5", 37 | "prettier": "^3.3.3", 38 | "rimraf": "^6.0.1", 39 | "supertest": "^7.0.0", 40 | "ts-jest": "^29.2.4", 41 | "tsx": "^4.17.0", 42 | "typescript": "^5.5.4" 43 | }, 44 | "dependencies": {} 45 | } 46 | -------------------------------------------------------------------------------- /src/index.test.ts: -------------------------------------------------------------------------------- 1 | import supertest from "supertest"; 2 | import { server } from "./index"; 3 | 4 | describe("Server", function () { 5 | const request = supertest.agent(server); 6 | 7 | afterAll((done) => { 8 | server.close(done); 9 | }); 10 | 11 | it("should get /", async () => { 12 | const res = await request.get("/"); 13 | 14 | expect(res.status).toBe(200); 15 | expect(res.body).toEqual({ data: "It Works!" }); 16 | }); 17 | }); 18 | -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | import http from "http"; 2 | 3 | const PORT = process.env.PORT || 3000; 4 | 5 | export const server = http.createServer((req, res) => { 6 | res.writeHead(200, { "Content-Type": "application/json" }); 7 | res.end( 8 | JSON.stringify({ 9 | data: "It Works!", 10 | }), 11 | ); 12 | }); 13 | 14 | server.listen(PORT, () => { 15 | console.log(`Server running on http://localhost:${PORT}/`); 16 | }); 17 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | /* Visit https://aka.ms/tsconfig to read more about this file */ 4 | 5 | /* Projects */ 6 | // "incremental": true, /* Save .tsbuildinfo files to allow for incremental compilation of projects. */ 7 | // "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */ 8 | // "tsBuildInfoFile": "./.tsbuildinfo", /* Specify the path to .tsbuildinfo incremental compilation file. */ 9 | // "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects. */ 10 | // "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */ 11 | // "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */ 12 | 13 | /* Language and Environment */ 14 | "target": "ESNext", 15 | /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */ 16 | "lib": [], 17 | /* Specify a set of bundled library declaration files that describe the target runtime environment. */ 18 | // "jsx": "preserve", /* Specify what JSX code is generated. */ 19 | // "experimentalDecorators": true, /* Enable experimental support for TC39 stage 2 draft decorators. */ 20 | // "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */ 21 | // "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h'. */ 22 | // "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */ 23 | // "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using 'jsx: react-jsx*'. */ 24 | // "reactNamespace": "", /* Specify the object invoked for 'createElement'. This only applies when targeting 'react' JSX emit. */ 25 | // "noLib": true, /* Disable including any library files, including the default lib.d.ts. */ 26 | // "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */ 27 | // "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */ 28 | 29 | /* Modules */ 30 | "module": "ESNext", 31 | /* Specify what module code is generated. */ 32 | "rootDir": "./", 33 | /* Specify the root folder within your source files. */ 34 | "moduleResolution": "node", 35 | /* Specify how TypeScript looks up a file from a given module specifier. */ 36 | // "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */ 37 | // "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */ 38 | // "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */ 39 | // "typeRoots": [], /* Specify multiple folders that act like './node_modules/@types'. */ 40 | // "types": [], /* Specify type package names to be included without being referenced in a source file. */ 41 | // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */ 42 | // "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */ 43 | "resolveJsonModule": true, 44 | /* Enable importing .json files. */ 45 | // "noResolve": true, /* Disallow 'import's, 'require's or ''s from expanding the number of files TypeScript should add to a project. */ 46 | 47 | /* JavaScript Support */ 48 | // "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */ 49 | // "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */ 50 | // "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */ 51 | 52 | /* Emit */ 53 | // "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */ 54 | // "declarationMap": true, /* Create sourcemaps for d.ts files. */ 55 | // "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */ 56 | "sourceMap": true, 57 | /* Create source map files for emitted JavaScript files. */ 58 | // "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */ 59 | "outDir": "./build", 60 | /* Specify an output folder for all emitted files. */ 61 | // "removeComments": true, /* Disable emitting comments. */ 62 | // "noEmit": true, /* Disable emitting files from a compilation. */ 63 | // "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */ 64 | // "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types. */ 65 | // "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */ 66 | // "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */ 67 | // "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */ 68 | // "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */ 69 | // "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */ 70 | // "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */ 71 | // "newLine": "crlf", /* Set the newline character for emitting files. */ 72 | // "stripInternal": true, /* Disable emitting declarations that have '@internal' in their JSDoc comments. */ 73 | // "noEmitHelpers": true, /* Disable generating custom helper functions like '__extends' in compiled output. */ 74 | // "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */ 75 | // "preserveConstEnums": true, /* Disable erasing 'const enum' declarations in generated code. */ 76 | // "declarationDir": "./", /* Specify the output directory for generated declaration files. */ 77 | // "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */ 78 | 79 | /* Interop Constraints */ 80 | // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */ 81 | // "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */ 82 | "esModuleInterop": true, 83 | /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */ 84 | // "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */ 85 | "forceConsistentCasingInFileNames": true, 86 | /* Ensure that casing is correct in imports. */ 87 | 88 | /* Type Checking */ 89 | "strict": true, 90 | /* Enable all strict type-checking options. */ 91 | "noImplicitAny": true, 92 | /* Enable error reporting for expressions and declarations with an implied 'any' type. */ 93 | // "strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */ 94 | // "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */ 95 | // "strictBindCallApply": true, /* Check that the arguments for 'bind', 'call', and 'apply' methods match the original function. */ 96 | // "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */ 97 | // "noImplicitThis": true, /* Enable error reporting when 'this' is given the type 'any'. */ 98 | // "useUnknownInCatchVariables": true, /* Default catch clause variables as 'unknown' instead of 'any'. */ 99 | // "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */ 100 | // "noUnusedLocals": true, /* Enable error reporting when local variables aren't read. */ 101 | // "noUnusedParameters": true, /* Raise an error when a function parameter isn't read. */ 102 | // "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */ 103 | // "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */ 104 | // "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */ 105 | // "noUncheckedIndexedAccess": true, /* Add 'undefined' to a type when accessed using an index. */ 106 | // "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */ 107 | // "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type. */ 108 | // "allowUnusedLabels": true, /* Disable error reporting for unused labels. */ 109 | // "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */ 110 | 111 | /* Completeness */ 112 | // "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */ 113 | "skipLibCheck": true 114 | /* Skip type checking all .d.ts files. */ 115 | }, 116 | "include": [ 117 | "src", 118 | "package.json" 119 | ], 120 | "exclude": [ 121 | "src/**/*.test.ts" 122 | ] 123 | } 124 | --------------------------------------------------------------------------------