Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make Node.js support the shebang (#!) for JavaScript files

Some scripting languages (such as Python or Bash) use # for comments.

#!/usr/bin/env python print 'hello, world' 

I can run the script:

python script.py 

Or

./script.py 

Is it possible to make JavaScript support shebang?

like image 701
kev Avatar asked May 22 '12 05:05

kev


People also ask

What is shebang in node JS?

Shebang is a OS feature and can be used to run any interpreted language: Python, Perl, etc. For Node.js it can (but often doesn't) look like this: #!/usr/bin/node. Node.

Can you use node on the front end?

This is not the case; Node. js can be used on the frontend as well as the backend. The event-driven, non-blocking nature of Node. js frameworks is one of the reasons it is a popular choice for developers designing a flexible and scalable backend.

What is usr bin env node?

#!/usr/bin/env node is an instance of a shebang line: the very first line in an executable plain-text file on Unix-like platforms that tells the system what interpreter to pass that file to for execution, via the command line following the magic #! prefix (called shebang).

Can you script with node js?

Create a NodeJS command-line script. You may already know that we can execute a NodeJS script file by running: node script. js . That is fine in most cases, but a NodeJS command-line script is a regular JavaScript file, except that it contains a special shell-instruction.


1 Answers

Yes, you can simply use #!/usr/bin/env node (or whatever the name of your JavaScript interpreter is, it works fine with js (spidermonkey), too).

[me@hades:~]> cat > test.js #!/usr/bin/env node console.log('hi'); [me@hades:~]> chmod +x test.js [me@hades:~]> ./test.js hi 

Most likely both interpreters test if the first line begins with #! and in this case it is skipped.

like image 173
ThiefMaster Avatar answered Oct 06 '22 21:10

ThiefMaster