2024-11-07
3063
#node#typescript
Aman Mittal
22050
Nov 7, 2024 β‹… 10 min read

How to set up TypeScript with Node.js and Express

Aman Mittal πŸ‘¨β€πŸ’» Developer πŸ‘‰ Node.js, React, React Native | Tech Blogger with 1M+ views on Medium

Recent posts:

Vue logo over a brown background.

A guide to two-way binding in Vue

Learn how to implement one-way and two-way data binding in Vue.js, using v-model and advanced techniques like defineModel for better apps.

David Omotayo
Nov 22, 2024 β‹… 10 min read
TypeScript logo over a pink and white background.

Drizzle vs. Prisma: Which ORM is best for your project?

Compare Prisma and Drizzle ORMs to learn their differences, strengths, and weaknesses for data access and migrations.

Temitope Oyedele
Nov 21, 2024 β‹… 10 min read
Practical Implementation Of The Rule Of Least Power For Developers

Practical implementation of the Rule of Least Power for developers

It’s easy for devs to default to JavaScript to fix every problem. Let’s use the RoLP to find simpler alternatives with HTML and CSS.

Timonwa Akintokun
Nov 21, 2024 β‹… 8 min read
Rust logo over black marble background.

Handling memory leaks in Rust

Learn how to manage memory leaks in Rust, avoid unsafe behavior, and use tools like weak references to ensure efficient programs.

Ukeje Goodness
Nov 20, 2024 β‹… 4 min read
View all posts

45 Replies to "How to set up TypeScript with Node.js and Express"

  1. What’s the best command setup for deploying and running something like this in production? There are now compiled JS files in build/, but other files (e.g., package.json, static HTML, etc.) are all in their original locations. And, we haven’t created a script in package.json to run the built app.

  2. okayy,, then how can i use other plugin to work with my express typescript. am i should install plugin that support in typescript or not ?

  3. Choose any middleware you need, but make sure DefinitelyTyped has the corresponding Typescript @types/* mappings available. Most popular NodeJS middleware packages are supported in the DefinitelyTyped project.

    1. Hey, if you are running localhost, you do not require “https” until you want to intentionally use HTTPS.

    1. In the article above, the “outDir” is referring to the config option. The “./dist” is the value.

      Are you referring to something specific? Can you please point me to that?

    1. The `main` poperty in the `package.json` indicates the entry point and it should point out to Javascript compiled output. So it should be “main”:”dist/index.js” as we are using typescript here, if it was just just Javascript we can just use the index.js.

      1. Thanks. Ok just so everyone is 100% clear. package.json should be changed to:
        “main”: “dist/index.js”,

  4. Excellent tutorial! One minor note is that the protocol should not be `https` since express is not secure by default, e.g, this line should be changed to just `http` — `console.log(`⚑️[server]: Server is running at http://localhost:${port}`)`

  5. had to remove “module”: “commonjs” from tsconfig.js as the error “ReferenceError: exports is not defined in ES module scope”

  6. anyone has a sample github repo for this? please include an example repo so we know where to place the files at. im guessing all the files should be in an dist folder?

  7. This is an excellent article that helped me complete what I sought out to do. I have one suggestion, where there is a suggestion to update nodemon.json with this script:
    {
    “watch”: [“src”],
    “ext”: “ts”,
    “exec”: “concurrently \”npx tsc –watch\” \”ts-node src/index.ts\””
    },
    I would have found it helpful to add a comment about installing concurrently. It was suggested earlier in the article but not in the same general vicinity of the nodemon.json change.

  8. Right after Running TypeScript in Node with ts-node
    if you face errors modules not found like dotenv or express you need to install again them and add to types: npm install dotenv; npm install @types/dotenv –save-dev and
    npm install express; npm install @types/express –safe-dev

  9. Supplement:
    Update nodemon.json for alias path as follows:
    “exec”: “concurrently \”npx tsc –watch\” \”ts-node -r tsconfig-paths/register src/index.ts\””

    1. Hey there! David supplied an explanation for this above. Take a look, and let us know if you have any questions.

  10. Hi, i have an error with the line “exec”: “concurrently \”tsc –watch \” \”ts-node src/index.ts\”” in the nodemon.json file, when remove this line the comand “pnpm dev” running successfully but when this line is present, catch the error “Cannot find module”

    1. Hello! I asked David about this, and he said this is usually caused by packages whose type definitions are not included in the base package and must be installed separately. Most packages include their type definitions by default, so you’re likely using an older version of those packages. Best of luck!

Leave a Reply