Back to Ts Node

npx and yarn dlx

website/docs/recipes/npx-and-yarn-dlx.md

10.9.21.3 KB
Original Source

Using npx or yarn dlx is a great ways to publish reusable TypeScript tools to GitHub without precompiling or packaging.

Check out our working example: TypeStrong/ts-node-npx-example

shell
npx typestrong/ts-node-npx-example --help
npx typestrong/ts-node-npx-example --first Arthur --last Dent

TODO publish demo and link to it TODO test demo:

  • uninstall global ts-node
  • try running demo
  • does ts-node need to be installed globally?

This boilerplate is a good starting point:

json
{
  "bin": "./cli.ts",
  "dependencies": {
    "ts-node": "latest",
    "@swc/core": "latest",
    "@swc/helpers": "latest",
    "@tsconfig/node16": "latest"
  }
}
json
{
  "extends": "@tsconfig/node16/tsconfig.json",
  "ts-node": {
    "swc": true
  }
}
typescript
#!/usr/bin/env ts-node

console.log("Hello world!")

If you require native ESM support, use ts-node-esm in your shebang and follow the configuration instructions for ESM: Native ECMAScript modules

typescript
#!/usr/bin/env ts-node-esm