[node.js] How to install and run Typescript locally in npm?

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 [email protected]
3 info using [email protected]
4 verbose run-script [ 'pretsc', 'tsc', 'posttsc' ]
5 info lifecycle [email protected]~pretsc: [email protected]
6 silly lifecycle [email protected]~pretsc: no script for pretsc, continuing
7 info lifecycle [email protected]~tsc: [email protected]
8 verbose lifecycle [email protected]~tsc: unsafe-perm in lifecycle true
9 verbose lifecycle [email protected]~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 [email protected]~tsc: CWD: /home/vagrant/foo
11 silly lifecycle [email protected]~tsc: Args: [ '-c', 'tsc' ]
12 silly lifecycle [email protected]~tsc: Returned: code: 2  signal: null
13 info lifecycle [email protected]~tsc: Failed to exec tsc script
14 verbose stack Error: [email protected] 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 [email protected]
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 [email protected] tsc: `tsc`
22 error Exit status 2
23 error Failed at the [email protected] 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.

This question is related to node.js typescript npm

The answer is


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!


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

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.


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"
  }
}

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

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 --).


Examples related to node.js

Hide Signs that Meteor.js was Used Querying date field in MongoDB with Mongoose SyntaxError: Cannot use import statement outside a module Server Discovery And Monitoring engine is deprecated How to fix ReferenceError: primordials is not defined in node UnhandledPromiseRejectionWarning: This error originated either by throwing inside of an async function without a catch block dyld: Library not loaded: /usr/local/opt/icu4c/lib/libicui18n.62.dylib error running php after installing node with brew on Mac internal/modules/cjs/loader.js:582 throw err DeprecationWarning: Buffer() is deprecated due to security and usability issues when I move my script to another server Please run `npm cache clean`

Examples related to typescript

TS1086: An accessor cannot be declared in ambient context Element implicitly has an 'any' type because expression of type 'string' can't be used to index Angular @ViewChild() error: Expected 2 arguments, but got 1 Typescript: No index signature with a parameter of type 'string' was found on type '{ "A": string; } Understanding esModuleInterop in tsconfig file How can I solve the error 'TS2532: Object is possibly 'undefined'? Typescript: Type 'string | undefined' is not assignable to type 'string' Typescript: Type X is missing the following properties from type Y length, pop, push, concat, and 26 more. [2740] Can't perform a React state update on an unmounted component TypeScript and React - children type?

Examples related to npm

What does 'x packages are looking for funding' mean when running `npm install`? error: This is probably not a problem with npm. There is likely additional logging output above Module not found: Error: Can't resolve 'core-js/es6' Browserslist: caniuse-lite is outdated. Please run next command `npm update caniuse-lite browserslist` ERROR in The Angular Compiler requires TypeScript >=3.1.1 and <3.2.0 but 3.2.1 was found instead DeprecationWarning: Buffer() is deprecated due to security and usability issues when I move my script to another server Please run `npm cache clean` What exactly is the 'react-scripts start' command? On npm install: Unhandled rejection Error: EACCES: permission denied Difference between npx and npm?