I have a script I'd like to run... maybe something like this:
#!/usr/bin/env deno
console.log('hello world');
The shebang part is confusing. When I do it as above, with the filename dev
, I get:
error: Found argument './dev' which wasn't expected, or isn't valid in this context
When I try to change the shebang to #!/usr/bin/env deno run
I get
/usr/bin/env: ‘deno run’: No such file or directory
Any thoughts on how to pull this off?
In order to pass command options to deno
via env
, you need the -S
parameter for env.
For example, the following is the minimal shebang you should use for self-running a script with Deno.
#!/usr/bin/env -S deno run
The following script/shebang will run Deno as silently as possible, with all permissions and will assume there is an import_map.json
file next to the script file.
#!/usr/bin/env -S deno -q run --allow-all --import-map="${_}/../import_map.json"
// get file and directory name
const __filename = import.meta.url.replace("file://", "");
const __dirname = __filename.split("/").slice(0, -1).join("/");
The lines with __filename
and __dirname
will give you variables similar to Node's execution.
Deno also provides a convenient method for installing a script with it's permissions and dependencies in the configured distribution location.
See: Deno Manual - Script installer
As of Deno 1.6, you can now build stand-alone executables from your script as well.
See: Deno Manual - Compiler - Compiling Executables
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With