Skip to content

Instantly share code, notes, and snippets.

@bonniss
Last active June 4, 2024 07:24
Show Gist options
  • Save bonniss/a7497bdd672eb2a4338ff10a365dfdb5 to your computer and use it in GitHub Desktop.
Save bonniss/a7497bdd672eb2a4338ff10a365dfdb5 to your computer and use it in GitHub Desktop.
A lean Typescript setup for Node.js development

Typescript setup for Node.js development

1. Initialize the Project

First, create a new directory for your project and initialize it with npm.

mkdir my-tool
cd my-tool
npm init -y

2. Install Dependencies

npm install --save-dev typescript tsx

# More deps here
# `ofetch` for http calls, for example
npm install --save ofetch

3. Typescript config

Create a tsconfig.json file in the root of your project.

{
  "compilerOptions": {
    "target": "ES2020",
    "module": "commonjs",
    "outDir": "./dist",
    "rootDir": "./src",
    "strict": true,
    "resolveJsonModule": true,
    "esModuleInterop": true,
    "skipLibCheck": true
  },
  "include": ["src"],
  "exclude": ["node_modules"]
}

4. .gitignore

Create a .gitignore file in the root of your project.

node_modules
dist

5. Start coding

Project structure:

my-tool/
├── src/
│   └── index.ts
├── package.json
├── tsconfig.json
└── .gitignore

src/index.ts

import { ofetch } from 'ofetch';

async function main() {
  console.info(await ofetch('http://dummyjson.com/test'));
}

main();

6. Setup scripts

"scripts": {
  "start": "tsx src/index.ts",
  "build": "tsc",
  "dev": "tsx watch src/index.ts"
}

7. Run

npm run dev

8. Build

npm run build

Output located in folder dist.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment