Skip to content

Instantly share code, notes, and snippets.

@Acry
Last active July 25, 2020 15:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Acry/3569feefe7548d85f0fceb36714ba5f5 to your computer and use it in GitHub Desktop.
Save Acry/3569feefe7548d85f0fceb36714ba5f5 to your computer and use it in GitHub Desktop.

TypeScript in the backend - Most Wanted FAQ

Node.js

What is it? Googles v8 JS-engine for the desktop.

How does versioning work? Odd-numbered releases (9, 11, etc.) become unsupported after six months. Even-numbered releases (10, 12, etc.) move to Active LTS status and are ready for general use.

https://nodejs.org/en/about/releases/ https://endoflife.date/nodejs

Nodemon

https://github.com/remy/nodemon

nodejs – using typescript with nodemon https://bjdejongblog.nl/nodejs-using-typescript-with-nodemon/ https://stackoverflow.com/questions/37979489/how-to-watch-and-reload-ts-node-when-typescript-files-change

Node.js with TypeScript, Debug inside VSCode and Nodemon https://dev.to/oieduardorabelo/nodejs-with-typescript-debug-inside-vscode-and-nodemon-23o7

TypeScript

Typed JavaScript

Typescript 3.9.7

npm install -g typescript

Modern TypeScript project template There is no need for Babel. https://dev.to/dandv/typescript-settings-for-modern-projects-4596
https://github.com/dandv/typescript-modern-project

https://dev.to/patarapolw/some-typescript-tricks-4nlm

tsc - the TypeScript compiler

Use tsc to build and run with node or use ts-node. tsc --watch https://www.typescriptlang.org/docs/handbook/tsconfig-json.html
https://www.typescriptlang.org/docs/handbook/compiler-options.html

TypeScript and Node.js 12 (ES2019)

released: 23 Apr 2019

If you are running Node.js 12: https://stackoverflow.com/a/59787575/148072

TypeScript and Node.js 14 (ES2020)

released: 21 Apr 2020

TypeScript configuration that produces output closest to Node.js 14 capabilities. https://stackoverflow.com/questions/61305578/what-typescript-configuration-produces-output-closest-to-node-js-14-capabilities/61305579#61305579

{
  "compilerOptions": {
    "allowSyntheticDefaultImports": true,
    "lib": ["ES2020"],
    "module": "ES2020",
    "moduleResolution": "node",
    "target": "ES2020"
  }
}

In addition to this, we also need to tell Node.js to treat .js files in this project as ES Modules. The reason for this is that Node.js had to maintain backwards compatibility with code written for older Node.js versions. This can be done by adding "type": "module" to your package.json:

{
  "type": "module"
}

Another change if you are comming from an earlier version of Node.js is that the file extension when importing files are now mandatory. This means that you must write out .js at the end of your local imports. Note that this is .js even though you are importing a TypeScript file that actually has the file extension .ts. This might seem a bit confisuing but this comment from one of the TS contributors explains why that is.

Some examples of how to write your import statements:

// Built-in Node.js modules
import { readFileSync } from 'fs'

// CommonJS packages from Npm
import md5File from 'md5-file'

// The local file "a.ts"
import { a } from './a.js'

If you want to stick with CommonJS for now, to avoid the caveats explained above, you can use the following config:

{
  "compilerOptions": {
    "lib": ["ES2020"],
    "module": "CommonJS",
    "target": "ES2020"
  }
}

TypeScript-Node

TypeScript REPL https://github.com/TypeStrong/ts-node https://www.npmjs.com/package/ts-node

Use ts-node instead of node. https://acry.github.io/2020/07/19/use-modern-ts-node.html

For distributing libraries, I agree you should compile it, but for everything else there isn't really any reason not to use ts-node (except for slightly higher startup time). I'm using 100% ts-node for a pretty large project, both for dev and in production.

Cannot run ts-node due to import syntaxes? Try this ts-node -O '{"module": "commonjs"}' scripts/example.ts

use require("ts-node/register") mocha --require ts-node/register 'test/**/*.spec.{ts,tsx}'

ts-node 8.10.2 npm install -g ts-node

.exit

#!/usr/bin/env ts-node-script
console.log("Hello, world!")
#!/usr/bin/env ts-node --script-mode --transpile-only --files

WARN ts-node@8.10.2 requires a peer of typescript@>=2.7 but none is installed. You must install peer dependencies yourself. npm install -g npm-install-peers

Watching and Restarting / Hot reloading

https://devimalplanet.com/how-to-build-and-run-typescript-watch-mode

Use ts-node-dev instead of nodemon

TypeScript Node compiles source code via require(), watching files and code reloads are out of scope for the project. If you want to restart the ts-node process on file change, existing node.js tools such as nodemon, onchange and node-dev work.

There's also ts-node-dev, a modified version of node-dev using ts-node for compilation and won't restart the process on file change.

https://fireship.io/snippets/hot-reload-node-typescript-server/ Concurrently runs multiple commands at the same time.

NPM - node package manager

npm 6.14.6 npm cache clean --force npm list -g // tree npm list -g --depth 0

The automatic installation of peer dependencies was explicitly removed with npm 3. http://blog.npmjs.org/post/110924823920/npm-weekly-5 https://github.com/npm/npm/releases/tag/v3.0.0 https://stackoverflow.com/questions/35207380/how-to-install-npm-peer-dependencies-automatically https://stackoverflow.com/questions/46053414/npm-warn-requires-a-peer-of-but-none-is-installed-you-must-install-peer

global path: ~/.node_modules

ESLint - Pluggable JavaScript linter

.eslintignore

*.*
!*.ts
!*.js
!*.tsx
!*.jsx
!*.vue
node_modules
dist
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment