Skip to main content

npx and yarn dlx

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
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:

package.json
json
{
"bin": "./cli.ts",
"dependencies": {
"ts-node": "latest",
"@swc/core": "latest",
"@swc/helpers": "latest",
"@tsconfig/node16": "latest"
}
}
package.json
json
{
"bin": "./cli.ts",
"dependencies": {
"ts-node": "latest",
"@swc/core": "latest",
"@swc/helpers": "latest",
"@tsconfig/node16": "latest"
}
}
tsconfig.json
json
{
"extends": "@tsconfig/node16/tsconfig.json",
"ts-node": {
"swc": true
}
}
tsconfig.json
json
{
"extends": "@tsconfig/node16/tsconfig.json",
"ts-node": {
"swc": true
}
}
cli.ts
typescript
#!/usr/bin/env ts-node
 
console.log("Hello world!")
cli.ts
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

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