We need Node.js (and NPM for convenience) - see below on how to install it. First run sudo apt update
(e.g., on Ubuntu).
Next create a new directory, and in it run npm init
, followed by
npm -g typescript
to install TypeScript globally. Create a file script.ts
in the project
root, and add the line <script src="script.js" defer></script>
to index.html
(e.g., to head).
(use html:5
in VS Code to set up a skeleton in a new index file).
Next install
npm install --save-dev lite-server
and add "start": "lite-server"
to "scripts"
in package.json
. Then run npm start
, which
should open a server on port 3000 (default).
Next run tsc --init
, which create tsconfig.json
. Finally, run your TypeScript main
file in watch-mode by tsc script.ts --watch
, and let the process run. Now each time you
save script.ts
it should automatically be compiled.
It is convenient to use Node JS (i.e., install) in combination with the version manager as it makes testing code on different versions of Node JS easy. Fetch the install script from here and run
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash
Source the shell resource file (e.g., . .bashrc
if bash
is your preferred shell) or close and re-open the terminal and verify that nvm --version
returns the version in URL above. If so, nvm
is installed. In case of problem consult the trouble-shooting section in the link.
Using nvm
we can now install first Node JS by
nvm install node
nvm install 10.16.3
nvm use 10.16.3
Next, we execute nvm list
to see what other versions are available. For instance, we can make one version the default version by nvm install <version>
, followed by nvm alias default <version>
.