99

I want to install and run Typescript (i.e. no global dependencies).

Here is my package.json file:

{
  "name": "foo",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "tsc": "tsc"
  },
  "devDependencies": {
    "typescript": "^1.8.10"
  },
  "author": "",
  "license": "ISC"
}

I then run:

npm install
npm run tsc

However when I run the second command I get sooo many errors it cannot display all it. Most of it is like the following:

../foo/node_modules/typescript/lib/lib.d.ts(5015,5): error TS2300: Duplicate identifier 'webkitTransformOrigin'.
../foo/node_modules/typescript/lib/lib.d.ts(5016,5): error TS2300: Duplicate identifier 'webkitTransformStyle'.
../foo/node_modules/typescript/lib/lib.d.ts(5017,5): error TS2300: Duplicate identifier 'webkitTransition'.
../foo/node_modules/typescript/lib/lib.d.ts(5018,5): error TS2300: Duplicate identifier 'webkitTransitionDelay'.
../foo/node_modules/typescript/lib/lib.d.ts(5019,5): error TS2300: Duplicate identifier 'webkitTransitionDuration'.
../foo/node_modules/typescript/lib/lib.d.ts(5020,5): error TS2300: Duplicate identifier 'webkitTransitionProperty'.

In npm-debug.log I get:

0 info it worked if it ends with ok
1 verbose cli [ '/usr/bin/nodejs', '/usr/bin/npm', 'run', 'tsc' ]
2 info using npm@3.10.2
3 info using node@v5.12.0
4 verbose run-script [ 'pretsc', 'tsc', 'posttsc' ]
5 info lifecycle foo@1.0.0~pretsc: foo@1.0.0
6 silly lifecycle foo@1.0.0~pretsc: no script for pretsc, continuing
7 info lifecycle foo@1.0.0~tsc: foo@1.0.0
8 verbose lifecycle foo@1.0.0~tsc: unsafe-perm in lifecycle true
9 verbose lifecycle foo@1.0.0~tsc: PATH: /usr/lib/node_modules/npm/bin/node-gyp-bin:/home/vagrant/foo/node_modules/.bin:/usr/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
10 verbose lifecycle foo@1.0.0~tsc: CWD: /home/vagrant/foo
11 silly lifecycle foo@1.0.0~tsc: Args: [ '-c', 'tsc' ]
12 silly lifecycle foo@1.0.0~tsc: Returned: code: 2  signal: null
13 info lifecycle foo@1.0.0~tsc: Failed to exec tsc script
14 verbose stack Error: foo@1.0.0 tsc: `tsc`
14 verbose stack Exit status 2
14 verbose stack     at EventEmitter.<anonymous> (/usr/lib/node_modules/npm/lib/utils/lifecycle.js:242:16)
14 verbose stack     at emitTwo (events.js:100:13)
14 verbose stack     at EventEmitter.emit (events.js:185:7)
14 verbose stack     at ChildProcess.<anonymous> (/usr/lib/node_modules/npm/lib/utils/spawn.js:40:14)
14 verbose stack     at emitTwo (events.js:100:13)
14 verbose stack     at ChildProcess.emit (events.js:185:7)
14 verbose stack     at maybeClose (internal/child_process.js:850:16)
14 verbose stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:215:5)
15 verbose pkgid foo@1.0.0
16 verbose cwd /home/vagrant/foo
17 error Linux 3.13.0-88-generic
18 error argv "/usr/bin/nodejs" "/usr/bin/npm" "run" "tsc"
19 error node v5.12.0
20 error npm  v3.10.2
21 error code ELIFECYCLE
22 error foo@1.0.0 tsc: `tsc`
22 error Exit status 2
23 error Failed at the foo@1.0.0 tsc script 'tsc'.
23 error Make sure you have the latest version of node.js and npm installed.
23 error If you do, this is most likely a problem with the foo package,
23 error not with npm itself.
23 error Tell the author that this fails on your system:
23 error     tsc
23 error You can get information on how to open an issue for this project with:
23 error     npm bugs foo
23 error Or if that isn't available, you can get their info via:
23 error     npm owner ls foo
23 error There is likely additional logging output above.
24 verbose exit [ 1, true ]

Note that removing the package and then installing typescript globally solves the problem. However if I then use npm install to install the local packages again, it reintroduces the problem.

halfer
  • 19,824
  • 17
  • 99
  • 186
Yahya Uddin
  • 26,997
  • 35
  • 140
  • 231

7 Answers7

103

It took me a while to figure out the solution to this problem - it's in the original question. You need to have a script that calls tsc in your package.json file so that you can run:

npm run tsc 

Include -- before you pass in options (or just include them in the script):

npm run tsc -- -v

Here's an example package.json:

{
  "name": "foo",
  "scripts": {
    "tsc": "tsc"
  },
  "dependencies": {
    "typescript": "^1.8.10"
  }
}
ubershmekel
  • 11,864
  • 10
  • 72
  • 89
  • 9
    Hard issue to google! Works for cli packages that suggest you install it globally but only want them locally, thanks! – Camathon Mar 19 '17 at 16:07
  • 1
    thanks.,, i have been searching for solution and your answer works for me. – Phil Jul 05 '20 at 20:59
  • 2
    Why would you want to install a development tool under `"dependencies"` instead of `"devDependencies"`? – Roko C. Buljan Nov 29 '21 at 20:12
  • And for whom is interested on what -- is about: "--" Indicates the end of node options. Pass the rest of the arguments to the script. If no script filename or eval/print script is supplied prior to this, then the next argument will be used as a script filename. – Sam Jan 26 '23 at 08:41
90

As of npm 5.2.0, once you've installed locally via

npm i typescript --save-dev

...you no longer need an entry in the scripts section of package.json -- you can now run the compiler with npx:

npx tsc

Now you don't have to update your package.json file every time you want to compile with different arguments.

Faust
  • 15,130
  • 9
  • 54
  • 111
  • 1
    This is a brilliant suggestion. Saved me a ton of time – Devin Jan 23 '19 at 21:02
  • 3
    `npx tsc` is definitely the way to go if you need a lightweight, minimal-config, local project solution. It can even be used without `npm i` for infrequently run commands, though without running `npm i` first, `npx` will need to install the necessary packages into a temp folder each time. More info on `npx` here: https://medium.com/@maybekatz/introducing-npx-an-npm-package-runner-55f7d4bd282b – ian.pvd Mar 05 '19 at 16:24
  • 2
    This was the answer for me! – the chad Mar 20 '19 at 03:45
  • 1
    This is the way also to check for the version, that indeed will match the one in the package.json 'npx tsc --version' – Carmine Tambascia Mar 04 '22 at 09:26
47

To install TypeScript local in project as a development dependency you can use --save-dev key

npm install --save-dev typescript

It's also writes the typescript into your package.json

You also need to have a tsconfig.json file. For example

{
  "compilerOptions": {
    "target": "ES5",
    "module": "system",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": false
  },
  "exclude": [
    "node_modules",
    ".npm"
  ]
}

For more information about the tsconfig you can see here http://www.typescriptlang.org/docs/handbook/tsconfig-json.html

Yahya Uddin
  • 26,997
  • 35
  • 140
  • 231
Mikhail
  • 11,067
  • 7
  • 28
  • 53
  • 1
    how is that any different what I did in my `package.json`. All that does is install type script and save it in my dependencies. It's also a dev dependency (not a normal dependency) so it should be `npm install --dev-save` – Yahya Uddin Jun 25 '16 at 15:53
  • @YahyaUddin are you have a `tsconfig.json` ? – Mikhail Jun 25 '16 at 15:54
  • I tried different ways to cause the error you have provided in your question or something like that, but I did not succeed – Mikhail Jun 25 '16 at 16:15
  • This solution seem to work for now. However note that you are not allows to use `files` and `exclude` at the same time according to: https://github.com/TypeStrong/atom-typescript/blob/master/docs/tsconfig.md Thanks for your help. I'll give an update if this works! – Yahya Uddin Jun 25 '16 at 16:17
  • @YahyaUddin You mean it https://github.com/TypeStrong/atom-typescript/blob/master/docs/tsconfig.md#exclude? Thank you, I'm I will consider it for the future – Mikhail Jun 25 '16 at 16:29
  • When attempting to repeat the issue yourself, did you try it without the "exclude" part of the tsconfig – Yahya Uddin Jun 25 '16 at 16:30
  • @YahyaUddin When I attempting to repeat the issue myself, I use `exclude` in tsconfig. As in my answer – Mikhail Jun 25 '16 at 16:32
  • 1
    That's why you didn't get the problem. Having the `exclude` fixes the issue! – Yahya Uddin Jun 25 '16 at 16:37
20

You need to tell npm that "tsc" exists as a local project package (via the "scripts" property in your package.json) and then run it via npm run tsc. To do that (at least on Mac) I had to add the path for the actual compiler within the package, like this

{
  "name": "foo"
  "scripts": {
    "tsc": "./node_modules/typescript/bin/tsc"
  },
  "dependencies": {
    "typescript": "^2.3.3",
    "typings": "^2.1.1"
  }
}

After that you can run any TypeScript command like npm run tsc -- --init (the arguments come after the first --).

Yahya Uddin
  • 26,997
  • 35
  • 140
  • 231
Jim Doyle
  • 2,302
  • 1
  • 16
  • 12
  • If you execute tsc from the root directory of your repo (where you have your node_modules), this is not necessary, you should be able to run tsc without an additional script. But when you have a monorepo with nested package.json files and want to run tsc there without installing typescript in the child-repo-directory, this is very useful! Thanks! – Phil Apr 29 '20 at 08:37
9

You can now use ts-node, which makes your life as simple as

npm install -D ts-node
npm install -D typescript

ts-node script.ts
Yanis.F
  • 612
  • 6
  • 18
3

tsc requires a config file or .ts(x) files to compile.

To solve both of your issues, create a file called tsconfig.json with the following contents:

{
    "compilerOptions": {
        "outFile": "../../built/local/tsc.js"
    },
    "exclude": [
        "node_modules"
    ]
}

Also, modify your npm run with this

tsc --config /path/to/a/tsconfig.json
Yahya Uddin
  • 26,997
  • 35
  • 140
  • 231
Bikas
  • 2,709
  • 14
  • 32
  • I only needed the exclude part for it to work. You also don't need to use `-- config` parameter as typescript looks there automatically provided its at the root of your project. – Yahya Uddin Jun 25 '16 at 16:06
  • Thanks. I wasn't aware of that – Bikas Jun 26 '16 at 18:54
1

Note if you are using typings do the following:

rm -r typings
typings install

If your doing the angular 2 tutorial use this:

rm -r typings
npm run postinstall
npm start

if the postinstall command dosen't work, try installing typings globally like so:

npm install -g typings

you can also try the following as opposed to postinstall:

typings install

and you should have this issue fixed!

Yahya Uddin
  • 26,997
  • 35
  • 140
  • 231