LoginSignup
2
3

More than 1 year has passed since last update.

Denoでshebangを使って実行する

Last updated at Posted at 2020-11-26

JavaScrptでもTypeScriptでも、
#!/usr/bin/env -S deno runでOK!

hello.js
#!/usr/bin/env -S deno run
console.log('hello deno!');
hello.ts
#!/usr/bin/env -S deno run
const message: string = 'hello deno!'
console.log(message);

deno runで実行する仕様なので、
#!/usr/bin/env nodeと同じ感覚で
#!/usr/bin/env denoと書いてもダメなんですねー。

error: Found argument './hello.js' which wasn't expected, or isn't valid in this context

USAGE:
    deno [OPTIONS] [SUBCOMMAND]

For more information try --help

#!/usr/bin/env deno runでも叱られます。
シェバンにオプションを渡すときは-Sが必要。

/usr/bin/env: ‘deno run’: No such file or directory
/usr/bin/env: use -[v]S to pass options in shebang lines

参考
https://stackoverflow.com/questions/63043929/how-do-i-invoke-deno-from-a-shell-script

追記)拡張子について

僕はシェバン付けてコマンドとして実行する場合も、ファイルの拡張子は付ける派なのですが、
拡張子を省略した場合は、DenoではJavaScriptと見なされるようです。
現状、TypeScriptと認識させたい場合は、.ts拡張子はしっかり付けるのが安全です。

TypeScriptでも拡張子を省略したいという需要はあるようで、ここで議論されています。
https://github.com/denoland/deno/issues/5088

2
3
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3