Skip to content

tincho70/getting-started-node-typescript

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Simple Guide to Start a Project with Node.js and TypeScript

Guide available in Spanish

Features

Table of Contents

Configuration

  1. Create a directory for the project
mkdir node_project
cd node_project
  1. Initialize the project with pnpm
pnpm init

Copy the following and paste it into package.json.

{
  "name": "node_project",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "tsc && node dist/index.js",
    "lint": "eslint .",
    "format": "pnpm exec prettier . --write",
    "format-spec": "prettier --write",
    "check": "pnpm exec prettier . --check",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@eslint/js": "^9.9.1",
    "eslint": "^9.9.1",
    "globals": "^15.9.0",
    "prettier": "3.3.3",
    "typescript": "^5.5.4",
    "typescript-eslint": "^8.3.0"
  }
}
  1. Create a configuration file for TypeScript
touch tsconfig.json

Copy the following and paste it into tsconfig.json.

{
  "compilerOptions": {
    "module": "commonjs",
    "esModuleInterop": true,
    "target": "es6",
    "moduleResolution": "node",
    "sourceMap": true,
    "outDir": "dist",
    "strict": true,
    "noImplicitAny": true,
    "skipLibCheck": true
  },
  "lib": ["es2015"],
  "include": ["**/*"],
  "exclude": ["node_modules", "dist"]
}
  1. Install eslint
pnpm install --save-dev eslint

Initialize eslint

pnpm eslint --init

Copy the following and paste it into eslint.config.mjs.

import globals from 'globals';
import pluginJs from '@eslint/js';
import tseslint from 'typescript-eslint';

export default [
  { files: ['**/*.{js,mjs,cjs,ts}'] },
  { ignores: ['node_modules', 'dist'] },
  { languageOptions: { globals: globals.node } },
  pluginJs.configs.recommended,
  ...tseslint.configs.recommended,
];
  1. Install prettier
pnpm install --save-dev prettier

Create a configuration file for prettier

touch .prettierrc

Copy the following and paste it into .prettierrc.

{
  "printWidth": 80,
  "tabWidth": 4,
  "useTabs": false,
  "semi": true,
  "singleQuote": true,
  "trailingComma": "es5",
  "bracketSpacing": true,
  "jsxBracketSameLine": false,
  "arrowParens": "always"
}

Create an ignore file for Prettier

touch .prettierignore

Copy the following and paste it into .prettierignore.

node_modules
dist

Prettier will also follow rules specified in .gitignore if it exists in the same directory from which it is executed.

Usage

ESLint

pnpm lint

Prettier

Format the code

pnpm format

Format the specific folder or file

pnpm format-spec <path>

Format the specific test file

pnpm format-spec <ruta/**/*.test.js>

Check if the code is formatted correctly

pnpm check

Run the project

Prerequisites:

  • Have a src/index.ts, etc.
  • Install the dependencies
pnpm start

Compiles the project and runs the file dist/index.js


Made with 👐 by Rapax

Tips are welcome through Lightning Zap to ⚡rapax@lawallet.ar.

About

A simple guide for setting up a Node.js project with TypeScript.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%