I always have this doubt and I don't know why the file inside the bin folder which create a simple http server doesn't have a .js extension.
Is there a reason behind this?
The bin/ directory serves as a location where you can define your various startup scripts. The www is an example to start the express app as a web server.
The Express Application Generator allows you to create a project that is configured to use the most common CSS stylesheet engines: LESS, SASS, Compass, Stylus.
It's tradition on unix that executables don't have extension.
For example, on Linux and MacOS to list a directory you type:
ls
you don't type
ls.exe
Another example, to launch the Dropbox service on Linux you can type
dropbox
you don't type
dropbox.py
even though dropbox is just a text file containing Python code.
Unix (and also bash terminal on Windows) have a feature where if a file is marked as executable (using the chmod
command) and the first line contains:
#!
.. then the shell (the program controlling the command line) will remove the first two characters (#!
) and execute the rest of that first line. This is often called the shbang line (sh = shell, !
= bang).
Therefore, if you want to develop a command-line program in node.js all you need to do is start the file with #! /usr/bin/env node
:
#! /usr/bin/env node
// ^
// |
// the 'env' command will find the correct install path of node
console.log('hello world');
Then use chmod
to make the file executable:
chmod +x my-script.js
Of course, creating a program that ends in .js
does not look "professional". For example you don't type gulp.js
when you run gulp and you don't type npm.js
when you run npm
. So people follow tradition and make their executable scripts have no extension - it makes it harder for people to realise that you didn't write the program in C or assembly language.
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