How to run TypeScript files from command line?

前端 未结 13 1404
死守一世寂寞
死守一世寂寞 2020-11-27 10:13

I\'m having a surprisingly hard time finding an answer to this. With plain Node.JS, you can run any js file with node path/to/file.js, with CoffeeScript it\'s <

相关标签:
13条回答
  • 2020-11-27 10:23

    Just in case anyone is insane like me and wants to just run typescript script as though it was a .js script, you can try this. I've written a hacky script that appears to execute the .ts script using node.

    #!/usr/bin/env bash
    
    NODEPATH="$HOME/.nvm/versions/node/v8.11.3/bin" # set path to your node/tsc
    
    export TSC="$NODEPATH/tsc"
    export NODE="$NODEPATH/node"
    
    TSCFILE=$1 # only parameter is the name of the ts file you created.
    
    function show_usage() {
        echo "ts2node [ts file]"
        exit 0
    }
    
    if [ "$TSCFILE" == "" ]
    then
        show_usage;
    fi
    
    JSFILE="$(echo $TSCFILE|cut -d"." -f 1).js"
    
    $TSC $TSCFILE && $NODE $JSFILE
    

    You can do this or write your own but essentially, it creates the .js file and then uses node to run it like so:

    # tsrun myscript.ts
    

    Simple. Just make sure your script only has one "." else you'll need to change your JSFILE in a different way than what I've shown.

    0 讨论(0)
  • 2020-11-27 10:24

    None of the other answers discuss how to run a TypeScript script that uses modules, and especially modern ES Modules.

    First off, ts-node doesn't work in that scenario, as of March 2020. So we'll settle for tsc followed by node.

    Second, TypeScript still can't output .mjs files. So we'll settle for .js files and "type": "module" in package.json.

    Third, you want clean import lines, without specifying the .js extension (which would be confusing in .ts files):

    import { Lib } from './Lib';
    

    Well, that's non-trivial. Node requires specifying extensions on imports, unless you use the experimental-specifier-resolution=node flag. In this case, it would enable Node to look for Lib.js or Lib/index.js when you only specify ./Lib on the import line.

    Fourth, there's still a snag: if you have a different main filename than index.js in your package, Node won't find it.

    Transpiling makes things a lot messier than running vanilla Node.

    Here's a sample repo with a modern TypeScript project structure, generating ES Module code.

    0 讨论(0)
  • 2020-11-27 10:25

    How do I do the same with Typescript

    You can leave tsc running in watch mode using tsc -w -p . and it will generate .js files for you in a live fashion, so you can run node foo.js like normal

    TS Node

    There is ts-node : https://github.com/TypeStrong/ts-node that will compile the code on the fly and run it through node

    0 讨论(0)
  • 2020-11-27 10:26

    We have following steps:

    1. First you need to install typescript

      npm install -g typescript
      
    2. Create one file helloworld.ts

      function hello(person){
         return "Hello, " + person;
      }
      let user = "Aamod Tiwari";
      const result = hello(user);
      console.log("Result", result)
      
    3. Open command prompt and type the following command

      tsc helloworld.ts
      
    4. Again run the command

      node helloworld.js
      
    5. Result will display on console

    0 讨论(0)
  • 2020-11-27 10:28

    Run the below commands and install the required packages globally:

    npm install -g ts-node
    npm install -g typescript
    

    Now run the following command to execute a typescript file:

    ts-node typescript-file.ts
    
    0 讨论(0)
  • 2020-11-27 10:28

    Write yourself a simple bash wrapper may helps.

    #!/bin/bash
    npx tsc $1 && node ${1%%.ts}
    
    0 讨论(0)
提交回复
热议问题