Procedure for moving a NodeJS project to TypeScript

node typescript
typescript project structure
typescript require
convert javascript to typescript
converting a project to typescript
add typescript to existing react project
porting to typescript
import typescript into javascript

It seems that just installing it and then changing file extensions to .ts is not enough to effectively move to TypeScript with a NodeJS project. I get about 400 errors that seem to stem mostly from module resolution issues (but not only).

There are a couple of guides covering this topic, but they do not seem to help, maybe because they are one year old at best. Therefor, I want to ask for recent advice.

What steps do I need to take to transition a NodeJS project to Typescript?

1.Add typings to your devDependencies in package.json

{
  "devDependencies": {
    "typings": "latest"
  },
  "scripts": {
    "postinstall": "typings install --save"
  }
}

2.Add typings.json (alongside your package.json), note postinstall action in the package.json above - this will install typescript definitions upon each npm install

{
  "globalDependencies": {
    "node": "github:DefinitelyTyped/DefinitelyTyped/node/node.d.ts"
  }
}

3.Add tsconfig.json

{ 
     "compilerOptions": { 
         "emitDecoratorMetadata": true, 
         "experimentalDecorators": true,
         "moduleResolution": "node",
         "module": "commonjs", 
         "target": "es6",
         "sourceMap": true,
         "outDir": "dist",
         "declaration": true,
         "allowJs": true,
         "forceConsistentCasingInFileNames": true
     },
     "exclude": [
         "node_modules",
         "dist",
         ".vscode",
         "docs"
     ]
} 

Note that `allowJs' option will help you to keep some javascript files unconverted if necessary.

  1. Make transpiling of ts files part of your build process

This should get you going. After that step by step convert javascript to typescript in order to leverage its benefits.

Hope this helps.

Migrate to Typescript on Node.js, It tells the Typescript compiler on how to process you Typescript code and how to compile them into Javascript. my config look like this: { "� This ends Episode 0 of our journey to convert our application from normal Javascript Code to Typescript as I took this chance to do a proper configuration that will help us throughout our project. If you liked this article please feel free to check my other articles and follow me over Medium :

Migrating Js to TS

You create a tsconfig.json with allowJs set to true. This allows you to use the .js as it is!. Next you start changing the file extension from .ts to .js one by one, declaring what you find missing as you go along.

I did a video about this about a week ago https://www.youtube.com/watch?v=gmKXXI_ck7w

More :

maybe because they are one year old at best.

I keep this up to date : https://basarat.gitbooks.io/typescript/content/docs/types/migrating.html

Of course if you read it as a book I assume you already have seen the nodejs quickstart : https://basarat.gitbooks.io/typescript/content/docs/quick/nodejs.html

Handbook - Migrating from JavaScript, Undergraduates: Migrate a NodeJS project to TypeScript. Luis Vidal protected accesibleForChildren(): void {} // A method is public by default Awesome, you have successsfully set up your Node project to use TypeScript! Conclusion. In this tutorial, we went over a few reasons that make TypeScript important to writing reliable JavaScript code. We also looked at some benefits of working with TypeScript. Finally, we followed simple steps and learnt how to set up a Node project with

There also official documentation on how to do this

https://www.typescriptlang.org/docs/handbook/migrating-from-javascript.html

Undergraduates: Migrate a NodeJS project to TypeScript., Decorative header image "How to move your project to TypeScript - at your own pace The reason is that process.env.PORT is The compiler is published to npm as typescript and exposes a command-line utility called tsc . If you don't want to constantly pass a bunch of configuration options via command-line arguments to the TypeScript compiler, I'd recommend creating a tsconfig.jsonfile for your project. There are two ways to do this. You can either create the file by hand or run tsc --initto create a default configuration.

How to move your project to TypeScript - at your own pace, Switching from a normal Javascript code base to Typescript can going to be a fairly easy process and we're going to transfer each module, file individually. For me this is its content, the one that suited me for this project :� We talk about a lot of advanced Node.js and TypeScript concepts on this blog, particularly focused around Domain-Driven Design and large-scale enterprise application patterns. However, I received a few emails from readers that were interested in seeing what a basic TypeScript starter project looks like. So I've put together just that. Prequisites

Converting a NodeJS Application to TypeScript. Ep : 0, TypeScript is a typed (optional) super-set of JavaScript that can help with building and npm init -y. Now that your project space is set up, you are ready to move on to install the module : Specifies the module code generation method. Node � But TypeScript is moving to improve the JavaScript development experience (read up on #4793 and #4789). An early step is for the TypeScript tooling to play nicely with .js files, which is shipping

How To Set Up a Node Project With Typescript, you know patterns and build tools (e.g. webpack) used in the project. With that assumption out of the way, in general the process consists of the following steps: Third Party NPM modules If you want to be more silent about your upgrade because you couldn't get team buy in to move to TypeScript, TypeScript has a blog�

Comments
  • Thank you for these detailed steps, it has been hard to know which config options to choose. However, I did try setting allowJs to true to do it incrementally (and add a build directory where the output should go), but then, the ts compiler complained that it cannot compile because it would overwrite the files in the build directory. But I did add the build directory to the excludes, just like node_modules, so I did not know what to do and just changed all files to .ts. Did I miss something there?
  • You did not forget to specify outDir in tsconfig? Also this might be connected with how your build is setup, are you using gulp/grunt or compiling directly via tsc. I have used both and both work just fine with allowJs option. I would suggest you to try and reduce your project to minimum in order to clearly see where the problem will disappear.
  • I did eventually get it to work today morning, by specifying both "exclude" (build, node_modules) and "include" (src, where I moved my code files)(and yes, outDir="build"). This surprised me a lot, but without it (ie. with only "exclude" or "include" I was getting those errors and tsc was setting up the file structure inside build in a very weird way.
  • Also, with your specific tsconfig.json I get the error TS5053: Option 'allowJs' cannot be specified with option 'declaration'.
  • Quite strange, I do use this two options together without any issues. Although you are correct and there is open issue: github.com/Microsoft/TypeScript/issues/7546. Thanks for the info though.
  • Thank you for maintaining that guide. I did try setting allowJs to true to do it incrementally (and add a build directory where the output should go), but then, the ts compiler complained that it cannot compile because it would overwrite the files in the build directory. But I did add the build directory to the excludes, just like node_modules, so I did not know what to do and just changed all files to .ts. Did I miss something there?
  • Please also use outDir. This is shown in the video as well :)
  • I did use it then, but I recently got it working by adding both "include" and "exclude" (and outDir is still there). Weird that I had to use both, but oh well