Search code examples
javascriptnode.jsjsontypescripttsc

Is there a simple straight forward "working" way to set up a node.js typescript project?


I'm unable to find standard instructions that help me set up a working typescript node.js project

I followed the popular tutorials like DigitalOcean & Scotch which are of no help!

  1. Ran npm run tsc -- --init to generate a well-commented tsconfig.json.

Initially tsc --init was not generating tsconfig.json but then I found that adding tsc as the script solved the problem

  1. Added some files with .ts extension to the project.

  2. Now when I run tsc to transpile .ts files to .js files - I get the following error:

error TS5014: Failed to parse file 'tsconfig.json': Unexpected token / in JSON at position 31.

What I have tried to solve this so far:

When I remove all the comments from the auto-generated config, I get the following valid JSON:

{
    "include": ["src/**/*.ts"],
    "compilerOptions": {
        "target": "es5",
        "module": "commonjs",

        "strict": true,
        "esModuleInterop": true
    }
}

With this tsconfig.json in place - when I run tsc, I get the following errors:

error TS5023: Unknown compiler option 'strict'.
error TS5023: Unknown compiler option 'esModuleInterop'.

When I use the standard config and run tsc I get the following errors:

error TS5023: Unknown compiler option 'esModuleInterop'.
error TS5023: Unknown compiler option 'moduleResolution'.

Dependencies in the package.json are:

"devDependencies": {
  "@types/express": "^4.17.6",
  "tslint": "^6.1.2",
  "typescript": "^3.9.5"
},
"dependencies": {
  "express": "^4.17.1"
}

Environment

npm -v : 6.13.4
node -v : v12.16.1
tsc -v : Version 1.5.3
Windows 10

This is a very first step towards making a typescript project and if there are so many problems - how can one make a project?

Is there a simple straight forward "working" way to set up a node.js typescript project?


Solution

  • To transpile from .ts to .js

    1. Add tsc:tsc script to package.json

    2. Run npm run tsc instead of tsc