\href{https://npmjs.org/package/ts-node}{\texttt{ }} \href{https://npmjs.org/package/ts-node}{\texttt{ }} \href{https://travis-ci.org/TypeStrong/ts-node}{\texttt{ }} \href{https://coveralls.io/r/TypeStrong/ts-node?branch=master}{\texttt{ }}

\begin{quote}
Type\+Script execution and REPL for node.\+js, with source map support. {\bfseries{Works with {\ttfamily typescript@$>$=2.\+0}}}. \end{quote}
\hypertarget{md_sfcode_node_modules_ts_node_README_autotoc_md30659}{}\doxysection{Installation}\label{md_sfcode_node_modules_ts_node_README_autotoc_md30659}

\begin{DoxyCode}{0}
\DoxyCodeLine{\# Locally in your project.}
\DoxyCodeLine{npm install -\/D ts-\/node}
\DoxyCodeLine{npm install -\/D typescript}
\DoxyCodeLine{}
\DoxyCodeLine{\# Or globally with TypeScript.}
\DoxyCodeLine{npm install -\/g ts-\/node}
\DoxyCodeLine{npm install -\/g typescript }

\end{DoxyCode}


{\bfseries{Tip\+:}} Installing modules locally allows you to control and share the versions through {\ttfamily package.\+json}. TS Node will always resolve the compiler from {\ttfamily cwd} before checking relative to its own installation.\hypertarget{md_sfcode_node_modules_ts_node_README_autotoc_md30660}{}\doxysection{Usage}\label{md_sfcode_node_modules_ts_node_README_autotoc_md30660}

\begin{DoxyCode}{0}
\DoxyCodeLine{\# Execute a script as `node` + `tsc`.}
\DoxyCodeLine{ts-\/node script.ts}
\DoxyCodeLine{}
\DoxyCodeLine{\# Starts a TypeScript REPL.}
\DoxyCodeLine{ts-\/node}
\DoxyCodeLine{}
\DoxyCodeLine{\# Execute code with TypeScript.}
\DoxyCodeLine{ts-\/node -\/e 'console.log("{}Hello, world!"{})'}
\DoxyCodeLine{}
\DoxyCodeLine{\# Execute, and print, code with TypeScript.}
\DoxyCodeLine{ts-\/node -\/p -\/e '"{}Hello, world!"{}'}
\DoxyCodeLine{}
\DoxyCodeLine{\# Pipe scripts to execute with TypeScript.}
\DoxyCodeLine{echo "{}console.log('Hello, world!')"{} | ts-\/node }

\end{DoxyCode}


\hypertarget{md_sfcode_node_modules_ts_node_README_autotoc_md30661}{}\doxysubsection{Programmatic}\label{md_sfcode_node_modules_ts_node_README_autotoc_md30661}
You can require {\ttfamily ts-\/node} and register the loader for future requires by using `require(\textquotesingle{}ts-\/node').register(\{ /$\ast$ options $\ast$/ \}){\ttfamily . You can also use file shortcuts -\/}node -\/r ts-\/node/register{\ttfamily or}node -\/r ts-\/node/register/transpile-\/only\`{} -\/ depending on your preferences.

{\bfseries{Note\+:}} If you need to use advanced node.\+js CLI arguments (e.\+g. {\ttfamily -\/-\/inspect}), use them with {\ttfamily node -\/r ts-\/node/register} instead of the {\ttfamily ts-\/node} CLI.\hypertarget{md_sfcode_node_modules_ts_node_README_autotoc_md30662}{}\doxysubsection{Mocha}\label{md_sfcode_node_modules_ts_node_README_autotoc_md30662}

\begin{DoxyCode}{0}
\DoxyCodeLine{mocha -\/-\/require ts-\/node/register -\/-\/watch-\/extensions ts,tsx "{}test/**/*.\{ts,tsx\}"{} [...args] }

\end{DoxyCode}


{\bfseries{Note\+:}} {\ttfamily -\/-\/watch-\/extensions} is only used in {\ttfamily -\/-\/watch} mode.\hypertarget{md_sfcode_node_modules_ts_node_README_autotoc_md30663}{}\doxysubsection{Tape}\label{md_sfcode_node_modules_ts_node_README_autotoc_md30663}

\begin{DoxyCode}{0}
\DoxyCodeLine{ts-\/node node\_modules/tape/bin/tape [...args] }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_ts_node_README_autotoc_md30664}{}\doxysubsection{Gulp}\label{md_sfcode_node_modules_ts_node_README_autotoc_md30664}

\begin{DoxyCode}{0}
\DoxyCodeLine{\# Create a `gulpfile.ts` and run `gulp`.}
\DoxyCodeLine{gulp }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_ts_node_README_autotoc_md30665}{}\doxysubsection{Visual Studio Code}\label{md_sfcode_node_modules_ts_node_README_autotoc_md30665}
Create a new node.\+js configuration, add {\ttfamily -\/r ts-\/node/register} to node args and move the {\ttfamily program} to the {\ttfamily args} list (so VS Code doesn\textquotesingle{}t look for {\ttfamily out\+Files}).


\begin{DoxyCode}{0}
\DoxyCodeLine{\{}
\DoxyCodeLine{    "{}type"{}: "{}node"{},}
\DoxyCodeLine{    "{}request"{}: "{}launch"{},}
\DoxyCodeLine{    "{}name"{}: "{}Launch Program"{},}
\DoxyCodeLine{    "{}runtimeArgs"{}: [}
\DoxyCodeLine{        "{}-\/r"{},}
\DoxyCodeLine{        "{}ts-\/node/register"{}}
\DoxyCodeLine{    ],}
\DoxyCodeLine{    "{}args"{}: [}
\DoxyCodeLine{        "{}\$\{workspaceFolder\}/index.ts"{}}
\DoxyCodeLine{    ]}
\DoxyCodeLine{\} }

\end{DoxyCode}


{\bfseries{Note\+:}} If you are using the {\ttfamily -\/-\/project $<$tsconfig.\+json$>$} command line argument as per the \href{\#configuration-options}{\texttt{ Configuration Options}}, and want to apply this same behavior when launching in VS Code, add an \char`\"{}env\char`\"{} key into the launch configuration\+: {\ttfamily \char`\"{}env\char`\"{}\+: \{ \char`\"{}\+TS\+\_\+\+NODE\+\_\+\+PROJECT\char`\"{}\+: \char`\"{}$<$tsconfig.\+json$>$\char`\"{} \}}.\hypertarget{md_sfcode_node_modules_ts_node_README_autotoc_md30666}{}\doxysection{How It Works}\label{md_sfcode_node_modules_ts_node_README_autotoc_md30666}
{\bfseries{Type\+Script Node}} works by registering the Type\+Script compiler for {\ttfamily .tsx?} and {\ttfamily .jsx?} (when {\ttfamily allow\+Js == true}) extensions. When node.\+js has an extension registered (via {\ttfamily require.\+extensions}), it will use the extension internally for module resolution. When an extension is unknown to node.\+js, it handles the file as {\ttfamily .js} (Java\+Script). By default, {\bfseries{Type\+Script Node}} avoids compiling files in {\ttfamily /node\+\_\+modules/} for three reasons\+:


\begin{DoxyEnumerate}
\item Modules should always be published in a format node.\+js can consume
\item Transpiling the entire dependency tree will make your project slower
\item Differing behaviours between Type\+Script and node.\+js (e.\+g. ES2015 modules) can result in a project that works until you decide to support a feature natively from node.\+js
\end{DoxyEnumerate}

{\bfseries{P.\+S.}} This means if you don\textquotesingle{}t register an extension, it is compiled as Java\+Script. When {\ttfamily ts-\/node} is used with {\ttfamily allow\+Js}, Java\+Script files are transpiled using the Type\+Script compiler.\hypertarget{md_sfcode_node_modules_ts_node_README_autotoc_md30667}{}\doxysection{Loading $<$tt$>$tsconfig.\+json$<$/tt$>$}\label{md_sfcode_node_modules_ts_node_README_autotoc_md30667}
{\bfseries{Typescript Node}} loads {\ttfamily tsconfig.\+json} automatically. Use {\ttfamily -\/-\/skip-\/project} to skip loading the {\ttfamily tsconfig.\+json}.

{\bfseries{Tip}}\+: You can use {\ttfamily ts-\/node} together with \href{https://www.npmjs.com/package/tsconfig-paths}{\texttt{ tsconfig-\/paths}} to load modules according to the {\ttfamily paths} section in {\ttfamily tsconfig.\+json}.\hypertarget{md_sfcode_node_modules_ts_node_README_autotoc_md30668}{}\doxysection{Configuration Options}\label{md_sfcode_node_modules_ts_node_README_autotoc_md30668}
You can set options by passing them before the script path, via programmatic usage or via environment variables.


\begin{DoxyCode}{0}
\DoxyCodeLine{ts-\/node -\/-\/compiler ntypescript -\/-\/project src/tsconfig.json hello-\/world.ts }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_ts_node_README_autotoc_md30669}{}\doxysubsection{CLI Options}\label{md_sfcode_node_modules_ts_node_README_autotoc_md30669}
Supports {\ttfamily -\/-\/print}, {\ttfamily -\/-\/eval} and {\ttfamily -\/-\/require} from \href{https://nodejs.org/api/cli.html}{\texttt{ node.\+js CLI options}}.


\begin{DoxyItemize}
\item {\ttfamily -\/-\/help} Prints help text
\item {\ttfamily -\/-\/version} Prints version information
\end{DoxyItemize}\hypertarget{md_sfcode_node_modules_ts_node_README_autotoc_md30670}{}\doxysubsection{CLI and Programmatic Options}\label{md_sfcode_node_modules_ts_node_README_autotoc_md30670}
{\itshape Environment variable denoted in parentheses.}


\begin{DoxyItemize}
\item {\ttfamily -\/T, -\/-\/transpile-\/only} Use Type\+Script\textquotesingle{}s faster {\ttfamily transpile\+Module} ({\ttfamily TS\+\_\+\+NODE\+\_\+\+TRANSPILE\+\_\+\+ONLY}, default\+: {\ttfamily false})
\item {\ttfamily -\/I, -\/-\/ignore \mbox{[}pattern\mbox{]}} Override the path patterns to skip compilation ({\ttfamily TS\+\_\+\+NODE\+\_\+\+IGNORE}, default\+: {\ttfamily /node\+\_\+modules/})
\item {\ttfamily -\/P, -\/-\/project \mbox{[}path\mbox{]}} Path to Type\+Script JSON project file ({\ttfamily TS\+\_\+\+NODE\+\_\+\+PROJECT})
\item {\ttfamily -\/C, -\/-\/compiler \mbox{[}name\mbox{]}} Specify a custom Type\+Script compiler ({\ttfamily TS\+\_\+\+NODE\+\_\+\+COMPILER}, default\+: {\ttfamily typescript})
\item {\ttfamily -\/D, -\/-\/ignore-\/diagnostics \mbox{[}code\mbox{]}} Ignore Type\+Script warnings by diagnostic code ({\ttfamily TS\+\_\+\+NODE\+\_\+\+IGNORE\+\_\+\+DIAGNOSTICS})
\item {\ttfamily -\/O, -\/-\/compiler-\/options \mbox{[}opts\mbox{]}} JSON object to merge with compiler options ({\ttfamily TS\+\_\+\+NODE\+\_\+\+COMPILER\+\_\+\+OPTIONS})
\item {\ttfamily -\/-\/files} Load files from {\ttfamily tsconfig.\+json} on startup ({\ttfamily TS\+\_\+\+NODE\+\_\+\+FILES}, default\+: {\ttfamily false})
\item {\ttfamily -\/-\/pretty} Use pretty diagnostic formatter ({\ttfamily TS\+\_\+\+NODE\+\_\+\+PRETTY}, default\+: {\ttfamily false})
\item {\ttfamily -\/-\/skip-\/project} Skip project config resolution and loading ({\ttfamily TS\+\_\+\+NODE\+\_\+\+SKIP\+\_\+\+PROJECT}, default\+: {\ttfamily false})
\item {\ttfamily -\/-\/skip-\/ignore} Skip ignore checks ({\ttfamily TS\+\_\+\+NODE\+\_\+\+SKIP\+\_\+\+IGNORE}, default\+: {\ttfamily false})
\item {\ttfamily -\/-\/log-\/error} Logs errors of types instead of exit the process ({\ttfamily TS\+\_\+\+NODE\+\_\+\+LOG\+\_\+\+ERROR}, default\+: {\ttfamily false})
\item {\ttfamily -\/-\/prefer-\/ts-\/exts} Re-\/order file extensions so that Type\+Script imports are preferred ({\ttfamily TS\+\_\+\+NODE\+\_\+\+PREFER\+\_\+\+TS\+\_\+\+EXTS}, default\+: {\ttfamily false})
\end{DoxyItemize}\hypertarget{md_sfcode_node_modules_ts_node_README_autotoc_md30671}{}\doxysubsection{Programmatic Only Options}\label{md_sfcode_node_modules_ts_node_README_autotoc_md30671}

\begin{DoxyItemize}
\item {\ttfamily transformers} An array of transformers to pass to Type\+Script
\item {\ttfamily read\+File} Custom Type\+Script-\/compatible file reading function
\item {\ttfamily file\+Exists} Custom Type\+Script-\/compatible file existence function
\end{DoxyItemize}\hypertarget{md_sfcode_node_modules_ts_node_README_autotoc_md30672}{}\doxysection{Help! My Types Are Missing!}\label{md_sfcode_node_modules_ts_node_README_autotoc_md30672}
{\bfseries{Type\+Script Node}} does {\itshape not} use {\ttfamily files}, {\ttfamily include} or {\ttfamily exclude}, by default. This is because a large majority projects do not use all of the files in a project directory (e.\+g. {\ttfamily Gulpfile.\+ts}, runtime vs tests) and parsing every file for types slows startup time. Instead, {\ttfamily ts-\/node} starts with the script file (e.\+g. {\ttfamily ts-\/node index.\+ts}) and Type\+Script resolves dependencies based on imports and references.

For global definitions, you can use the {\ttfamily type\+Roots} compiler option. This requires that your type definitions be structured as type packages (not loose Type\+Script definition files). More details on how this works can be found in the \href{https://www.typescriptlang.org/docs/handbook/tsconfig-json.html\#types-typeroots-and-types}{\texttt{ Type\+Script Handbook}}.

Example {\ttfamily tsconfig.\+json}\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{\{}
\DoxyCodeLine{  "{}compilerOptions"{}: \{}
\DoxyCodeLine{    "{}typeRoots"{} : ["{}./node\_modules/@types"{}, "{}./typings"{}]}
\DoxyCodeLine{  \}}
\DoxyCodeLine{\} }

\end{DoxyCode}


Example project structure\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{<project\_root>/}
\DoxyCodeLine{-\/-\/ tsconfig.json}
\DoxyCodeLine{-\/-\/ typings/}
\DoxyCodeLine{  -\/-\/ <module\_name>/}
\DoxyCodeLine{    -\/-\/ index.d.ts }

\end{DoxyCode}


Example module declaration file\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{declare module '<module\_name>' \{}
\DoxyCodeLine{    // module definitions go here}
\DoxyCodeLine{\} }

\end{DoxyCode}


For module definitions, you can use \href{https://www.typescriptlang.org/docs/handbook/module-resolution.html\#path-mapping}{\texttt{ {\ttfamily paths}}}\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{\{}
\DoxyCodeLine{  "{}compilerOptions"{}: \{}
\DoxyCodeLine{    "{}baseUrl"{}: "{}."{},}
\DoxyCodeLine{    "{}paths"{}: \{}
\DoxyCodeLine{      "{}custom-\/module-\/type"{}: ["{}types/custom-\/module-\/type"{}]}
\DoxyCodeLine{    \}}
\DoxyCodeLine{  \}}
\DoxyCodeLine{\} }

\end{DoxyCode}


An alternative approach for definitions of third-\/party libraries are \href{https://www.typescriptlang.org/docs/handbook/triple-slash-directives.html}{\texttt{ triple-\/slash directives}}. This may be helpful if you prefer not to change your Type\+Script {\ttfamily compiler\+Options} or structure your custom type definitions when using {\ttfamily type\+Roots}. Below is an example of the triple-\/slash directive as a relative path within your project\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{/// <reference types="{}./types/untyped\_js\_lib"{} />}
\DoxyCodeLine{import UntypedJsLib from "{}untyped\_js\_lib"{} }

\end{DoxyCode}


{\bfseries{Tip\+:}} If you {\itshape must} use {\ttfamily files}, enable {\ttfamily -\/-\/files} flags or set {\ttfamily TS\+\_\+\+NODE\+\_\+\+FILES=true}.\hypertarget{md_sfcode_node_modules_ts_node_README_autotoc_md30673}{}\doxysection{Watching and Restarting}\label{md_sfcode_node_modules_ts_node_README_autotoc_md30673}
{\bfseries{Type\+Script Node}} compiles source code via {\ttfamily require()}, watching files and code reloads are out of scope for the project. If you want to restart the {\ttfamily ts-\/node} process on file change, existing node.\+js tools such as \href{https://github.com/remy/nodemon}{\texttt{ nodemon}}, \href{https://github.com/Qard/onchange}{\texttt{ onchange}} and \href{https://github.com/fgnass/node-dev}{\texttt{ node-\/dev}} work.

There\textquotesingle{}s also \href{https://github.com/whitecolor/ts-node-dev}{\texttt{ {\ttfamily ts-\/node-\/dev}}}, a modified version of \href{https://github.com/fgnass/node-dev}{\texttt{ {\ttfamily node-\/dev}}} using {\ttfamily ts-\/node} for compilation and won\textquotesingle{}t restart the process on file change.\hypertarget{md_sfcode_node_modules_ts_node_README_autotoc_md30674}{}\doxysection{License}\label{md_sfcode_node_modules_ts_node_README_autotoc_md30674}
MIT 