438

How can I generate a tsconfig.json via the command line? I tried command tsc init, but this doesn't work.

mikemaccana
  • 110,530
  • 99
  • 389
  • 494
Rajab Shakirov
  • 7,265
  • 7
  • 28
  • 42

17 Answers17

695

It is supported since the release of TypeScript 1.6.

The correct command is --init not init:

$ tsc --init

Try to run in your console the following to check the version:

$ tsc -v

If the version is older than 1.6 you will need to update:

$ npm install -g typescript

Remember that you need to install node.js to use npm.

mikemaccana
  • 110,530
  • 99
  • 389
  • 494
Remo H. Jansen
  • 23,172
  • 11
  • 70
  • 93
  • 3
    if, like me, this didn't quite work for you - try the solution provided in this answer: http://stackoverflow.com/a/32532656/1732184 Cheers! – prestonsmith Apr 20 '17 at 21:02
  • 16
    I was able to make it work without a global installation: `npm i typescript` `npx tsc --init` – Andreas Warberg Dec 10 '19 at 14:21
  • 2
    for a local installation `node_modules/.bin/tsc --init` – burntsugar Jan 30 '20 at 00:22
  • 16
    Is this answer out of date? `npx tsc --init` returns "Unknown compiler option 'init'." – Andy Ray Mar 15 '20 at 21:50
  • @AndyRay This fixed it for me: `sudo npm install typescript -g --force` – nathanfranke Jul 03 '20 at 21:43
  • 1
    @nathanfranke you should NEVER install libraries as sudo, and --force isn't related to this instal issue. That probably means you're installing it to your system installed node, instead of a user (you) installed node using nvm – Andy Ray Jul 06 '20 at 01:50
  • @AndyRay Running npm global without sudo: "or try running the command again as root/Administrator". However you're right that it's probably a better idea to modify the permissions. – nathanfranke Jul 06 '20 at 03:43
84

For those who have TypeScript installed as a local package (and possibly as a dev dependency) via:

$ npm install typescript --save-dev

...and who have added tsc script to package.json:

"scripts": {
   ...
   "tsc": "tsc"
},

You can call tsc --init via npm:

$ npm run tsc -- --init 
Bojan Komazec
  • 9,216
  • 2
  • 41
  • 51
74

If you don't want to install TypeScript globally (which makes sense to me, so you don't need to update it constantly), you can use npx:

npx -p typescript tsc --init

The key point is using the -p flag to inform npx that the tsc binary belongs to the typescript package.

Pang
  • 9,564
  • 146
  • 81
  • 122
Luca Faggianelli
  • 2,032
  • 20
  • 23
  • 12
    Why not just use `npx tsc --init`? If Typescript is installed as a local dev dependency, tsc will exist in `node_modules/.bin/tsc`. – tenni Jan 22 '21 at 00:35
54

You need to have typescript library installed and then you can use

npx tsc --init 

If the response is error TS5023: Unknown compiler option 'init'. that means the library is not installed

yarn add --dev typescript

or for npm

npm i --save-dev typescript

and run npx command again

I recommend not to install dependencies globally as it is easier to manage the versioning for each project. It is also easier for coworkers to see an install what is needed to run the project with one command.

Black
  • 9,541
  • 3
  • 54
  • 54
  • I had this error: "This is not the tsc command you are looking for". Using npx typescript --init worked, though. – Boris K Nov 26 '21 at 14:26
29

this worked for me:

tsc --init
11

install TypeScript :

npm install typescript

add tsc script to package.json:

"scripts": {
  "tsc": "tsc"
 },

run this:

npm run tsc -- --init
Sina Lotfi
  • 3,044
  • 1
  • 23
  • 30
AissaDevLab
  • 584
  • 5
  • 9
10
npm install -g typescript 
tsc --init 

Then sometimes after you globally updated typescript you probably again need to add it to your current project.

npm install typecsript ts-node nodemon - if you need it.

nicodp
  • 2,362
  • 1
  • 11
  • 20
sushmitha reddy
  • 111
  • 1
  • 5
9

i am using this,

yarn add -D typescript
yarn tsc --init

this fixed it for me

rofrol
  • 14,438
  • 7
  • 79
  • 77
8

Setup a ts project as following steps:

  • install typescript yarn global add typescript
  • create a package.json: run yarn init or setting defaults yarn init -yp
  • create a tsconfig.json: run tsc --init
  • (*optional) add tslint.json

The project structure seems like:

│  package.json
│  tsconfig.json
│  tslint.json
│  yarn.lock
│
├─dist
│      index.js
│
└─src
       index.ts

Little Roys
  • 5,383
  • 3
  • 30
  • 28
3

the Normal way to generate the tsconfig file is to create a file with a name tsconfig.json then open {} ,inside this {} hit

ctrl + spacebar ( Windows)

cmd + spacebar (mac)

then choose compilationOptions then choose what options you need


For auto-generation (tsconfig.json) using VSCode terminal or any cmd on the same directory you wanna create :

tsc --init

Muhammed
  • 29
  • 3
3

In case someone comes here looking for what I was looking for, the tsconfig.json file with more options

npx tsconfig.json 

was not working for me so instead:

npx tsconfig.json.cmd

gave me the tsconfig.json file I was looking for which offers more options

this worked for me on Windows

fulo
  • 95
  • 1
  • 7
2

$ npm run tsc -- --init
This worked for the below package.json

  "devDependencies": {
    "@types/jasmine": "^3.6.2",
    "@types/node": "^14.14.19",
    "jasmine": "^3.6.3",
    "protractor": "^7.0.0",
    "typescript": "^4.1.3"
  },
Anurag A S
  • 725
  • 10
  • 23
ChinmoyJ
  • 61
  • 5
  • This only works if you have "tsc": "tsc" in the scripts section of your package.json – ThdK Jan 14 '22 at 19:50
0

I recommend to uninstall typescript first with the command:

npm uninstall -g typescript

then use the chocolatey package in order to run:

choco install typescript

in PowerShell.

β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
Sloka Roy
  • 17
  • 5
0

From 2ality. You can add a function in your .zshrc or .bashrc to shortcut to the local bin path.

# in .zshrc:
function npm-do { (PATH=$(npm bin):$PATH; eval $@;) }

# working inside your directory:
npm-do tsc --init
Luke Miles
  • 941
  • 9
  • 19
0

I did not used npm install -g typescript I only entered the command from the official documentation npm install typescript --save-dev, this is why I could not in my case use tsc -v or tsc --init ^^"

0

$ tsc --init this command works for me

0

You can simply run npx tsc --init without having to install any packages locally.

Shah
  • 2,126
  • 1
  • 16
  • 21