Что нового
  • Что бы вступить в ряды "Принятый кодер" Вам нужно:
    Написать 10 полезных сообщений или тем и Получить 10 симпатий.
    Для того кто не хочет терять время,может пожертвовать средства для поддержки сервеса, и вступить в ряды VIP на месяц, дополнительная информация в лс.

  • Пользаватели которые будут спамить, уходят в бан без предупреждения. Спам сообщения определяется администрацией и модератором.

  • Гость, Что бы Вы хотели увидеть на нашем Форуме? Изложить свои идеи и пожелания по улучшению форума Вы можете поделиться с нами здесь. ----> Перейдите сюда
  • Все пользователи не прошедшие проверку электронной почты будут заблокированы. Все вопросы с разблокировкой обращайтесь по адресу электронной почте : info@guardianelinks.com . Не пришло сообщение о проверке или о сбросе также сообщите нам.

How to Add TypeScript Support to Your Node.js Project

Lomanu4

Команда форума
Администратор
Регистрация
1 Мар 2015
Сообщения
14,564
Баллы
155
TypeScript is a game-changer for Node.js development, bringing type safety and modern tooling to your JavaScript codebase. In this guide, I’ll walk you through the exact steps I used to migrate my Node.js project to TypeScript—no prior TypeScript experience required!

1. Install TypeScript and Type Definitions


First, install TypeScript and the Node.js type definitions as development dependencies:


npm install --save-dev typescript @types/node
2. Initialize TypeScript Configuration


Generate a tsconfig.json file with the following command:


npx tsc --init

This creates a base TypeScript configuration file in your project root.

3. Update tsconfig.json


Open your tsconfig.json and add or update these important fields:


{
"include": ["**/*.ts"],
"exclude": ["node_modules"],
"compilerOptions": {
"outDir": "./dist"
// ...other options
}
}
  • include tells TypeScript to compile all .ts files.
  • exclude prevents node_modules from being compiled.
  • outDir specifies where the compiled JavaScript will go.
4. Rename Your Files to .ts


Rename your existing .js files to .ts. For example:


mv server.js server.ts

Delete the old .js files to avoid confusion.

5. Compile TypeScript Files


Now, compile your TypeScript files:


npx tsc

This will generate JavaScript files in the dist directory (as specified by outDir).

6. Run the Compiled JavaScript


Run your app using Node.js, pointing to the compiled entry file in dist:


node dist/server.js

Or whatever your entry point is.

7. (Optional) Update npm Scripts


For convenience, add a script to your package.json:


"scripts": {
"build": "npx tsc",
"start": "node dist/server.js"
}

Now you can run:


npm run build
npm start
Conclusion


That’s it! You’ve successfully added TypeScript support to your Node.js project. Enjoy better type safety, editor autocompletion, and more robust code!

If you found this guide helpful, follow me for more tips on JavaScript, TypeScript, and Node.js development.


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.

 
Вверх