How can I generate a tsconfig.json file?
Asked Answered
D

18

470

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

Directorate answered 28/4, 2016 at 14:1 Comment(0)
M
737

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.

Mccammon answered 28/4, 2016 at 14:17 Comment(7)
if, like me, this didn't quite work for you - try the solution provided in this answer: https://mcmap.net/q/81245/-how-to-upgrade-typescript-to-the-latest-version Cheers!Samphire
I was able to make it work without a global installation: npm i typescript npx tsc --initCombustion
for a local installation node_modules/.bin/tsc --initGoddaughter
Is this answer out of date? npx tsc --init returns "Unknown compiler option 'init'."Kirmess
@AndyRay This fixed it for me: sudo npm install typescript -g --forceInenarrable
@Inenarrable 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 nvmKirmess
@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.Inenarrable
I
90

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 
Immeasurable answered 15/8, 2019 at 13:29 Comment(2)
Why do we need the two dashes in between tsc and --init?Fanatical
@Fanatical That's a delimiter so everything after -- is treated as an argument to a script itself and is not parsed by npm.Immeasurable
R
83

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.

Rambunctious answered 14/7, 2020 at 7:42 Comment(1)
Why not just use npx tsc --init? If Typescript is installed as a local dev dependency, tsc will exist in node_modules/.bin/tsc.Majewski
Z
58

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.

Zach answered 8/11, 2020 at 14:3 Comment(1)
I had this error: "This is not the tsc command you are looking for". Using npx typescript --init worked, though.Brack
D
29

this worked for me:

tsc --init
Displeasure answered 9/9, 2018 at 7:10 Comment(0)
B
18

install TypeScript :

npm install typescript

add tsc script to package.json:

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

run this:

npm run tsc -- --init
Bronchi answered 31/5, 2020 at 2:4 Comment(0)
S
11

i am using this,

yarn add -D typescript
yarn tsc --init

this fixed it for me

Sepaloid answered 9/8, 2020 at 10:59 Comment(0)
P
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.

Paradis answered 20/10, 2022 at 18:48 Comment(1)
simple typo. It should tsc --init not int :-)Temperate
P
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

Politian answered 16/2, 2019 at 14:43 Comment(0)
B
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

Burck answered 30/8, 2021 at 12:10 Comment(0)
N
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

Newby answered 25/1, 2023 at 11:57 Comment(0)
S
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"
  },
Sontag answered 3/1, 2021 at 2:38 Comment(1)
This only works if you have "tsc": "tsc" in the scripts section of your package.jsonOdell
H
2

$ tsc --init this command works for me

Heedless answered 24/7, 2023 at 6:57 Comment(0)
U
1

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

Ungraceful answered 14/8, 2023 at 16:42 Comment(0)
L
1

npx tsc --init For to run this command you must have the typescript installed

Longrange answered 30/12, 2023 at 13:50 Comment(1)
That is the same thing as what the top voted answers said...Dewberry
B
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.

Babita answered 30/5, 2020 at 17:39 Comment(1)
This is for windows only.Novobiocin
H
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
Hiedihiemal answered 20/5, 2021 at 21:40 Comment(0)
L
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 ^^"

Lookin answered 4/7, 2023 at 10:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.