Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
August 11, 2022 01:35 pm GMT

Migrating a nodejs, webpack project from JavaScript to TypeScript

Hi, I'm not used to writing blog posts but recently I was looking for ways to migrate my nodeJs project from Javascript to typescript. I realised there are not many articles for projects that uses webpack.This is my attempt to share my learnings in this topic.

Here are few simple steps:

Add tsconf.js file in the root of your project. Add following configuration to this file

{    "compilerOptions": {        "outDir": "./dist",        "allowJs": true,        "target": "es5"    },    "include": [        "./src/**/*"    ],    "exclude": [        "node_modules"    ]}

npm install awesome-typescript-loader.

npm i awesome-typescript-loader

Add following to your webpack.config.js file

module{rules:[{ test: /\.(t|j)sx?$/, use: { loader: 'awesome-typescript-loader' } },]

and

 resolve: {        extensions: ['.ts', '.js'],    },

Change the source file name from .js to .ts

Changing the file extension to .ts will highlight some type errors in your file. I would recommend going through a basic tutorial for typescript to understand why you are getting those type errors. And how to fix them.

Once you have fixed the highlighted errors in your source files run your build tool as you normally do.


Original Link: https://dev.to/jokatty/migrating-a-nodejs-webpack-project-from-javascript-to-typescript-2ckm

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To