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"
}
}
Answer from ubershmekel on Stack OverflowIt 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"
}
}
Answer from ubershmekel on Stack OverflowIt 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"
}
}
As of npm 5.2.0 you no longer need an entry in the scripts section of package.json. You can now run the compiler with npx:
npx tsc
...and you don't have to update package.json with a new script every time you want to compile with different arguments.
Videos
I'm learning typescript and I'm trying to create a script that will execute js files created by tsc. This is my package.json:
{
"bin": "bin/tutorial.js",
"scripts": {
"start": "node bin/tutorial.js",
"prestart": "npm run build",
"build": "tsc"
}bin/tutorial.js is just a custom js file that I wrote that will execute the compiled js files in lib (tsconfig has outDir set to lib):
#!/usr/bin/env node
require('../lib/tutorial');I am able to run the script using npm start, since it will build beforehand. However if I clean the lib directory and do 'npx .' then the tsc build will never happen. This means that the lib directory will be empty and my bin/tutorial.js file won't be able to import the compiled js.
How do I ensure that my scripts executed with npx can run the build step beforehand?
» npm install tsc-watch
» npm install tsc-init
I think what you want is something like this (my current setup):
"scripts": {
"compile": "tsc && node app.js",
"dev": "./node_modules/nodemon/bin/nodemon.js -e ts --exec \"npm run compile\""
}
I created two scripts "compile" and "dev". To start developing you simply run npm run dev which starts nodemon and makes it watch .ts files (using the -e flag). Then, every time a .ts file changes nodemon will exec the compile task which basically compiles and runs the node app.
While using concurrently is a good option, my setup guarantees that tsc's work is done before attempting to execute the resulting .js files.
I have been using AlterX's solution for a while now and it has worked perfectly, but I have found it to be rather slow. Instead, I am now using tsc-watch. It makes tsc use incremental compilation similar to the -w flag, making the restart of the application much faster.
It's as easy as putting something similar to this in your package.json:
"scripts": {
"start": "tsc-watch --onSuccess \"node .\""
}
» npm install tsc-files
» npm install ts-node
A few tips in order
- restart the terminal
- restart the machine
- reinstall nodejs + then run
npm install typescript -g
If it still doesn't work run npm config get prefix to see where npm install -g is putting files (append bin to the output) and make sure that they are in the path (the node js setup does this. Maybe you forgot to tick that option).
You are all messing with the global installations and -path files. Just a little error might damage every project you have ever written, and you will spend the rest of the night trying to get a console.log('hi') to work again.
If you have run npm i typescript --save-dev in your project - just try to run:
npx tsc
And see if it works before messing with global stuff (unless of course you really know what you are doing)