Posted by admin at March 20, 2020
This is the most common use case because it allows you to save long Node programs and run them. To run a Node.js script from a file, simply type node filename
.
For example, to launch code from a program.js file which is located in a current folder, simply execute
node program.js
The file must be in the same folder. If you need to execute code from a file which is in a different folder, just provide the relative or absolute path:
node ./app/server.js node /var/www/app/server.js
The .js
is optional as long as you have a file with such extension. In other words, if you have server.js
you can execute node server
.
If you have index.js
in a folder, you can simply execute with a dot/period/full stop which in POSIX means the current folder or with a path to that folder.
For example:node . node /var/www/app
Are equivalent tonode index.js node index
andnode /var/www/app/index.js node /var/www/app/index
Despite being modeled after one standard, Node.js and browser JavaScript differ when it comes to globals. As you might know, in browser JavaScript we have a window
object. However, in Node.js, it is absent (obviously we don’t deal with a browser window), but developers are provided with new objects/keywords:
process
global
module.exports
and exports
So, let’s take a look at the main differences between Node.js and JavaScript.
There is a variable named global
which is accessible by any Node script or program. It refers to the global object. This object has properties. For example global.process
or global.require
or global.console
.
Any first level property of the global
object is accessible without the global
prefix. For example, global.process
and just process
will be the same.
The GLOBAL
alias for global
can be seen in older project but is deprecated. Use global
instead of GLOBAL
.
These are the main properties of the global
object and are known as globals:
process
require()
module
and module.exports
console
and console.log()`setTimeout()
and setInterval()
__dirname
and __filename
console.log()
and setTimeout()
work similarly to the browser methods. We will cover process
, require()
and module.exports
in the following lessons.
__dirname
, __filename
and process.cwd
__dirname
is an absolute path to the file in which the global variable is called, whereas process.cwd
is an absolute path to the process that runs the script. The latter might not be the same as the former if we started the program from a different folder, such as node ./code/program.js
.
__filename
is similar to __dirname
but only with the file name attached to the absolute path of the currently running file/script.
Each Node.js script that runs is, in essence, a process. For example, ps aux | grep 'node'
outputs all Node.js programs running on a machine. Conveniently, developers can access useful process information in code with the process
object, e.g., node -e "console.log(process.pid)"
will print the process ID.
Other useful process
information includes:
env
: Environment variablesargv
: Command-line argumentsexit()
: Method to exit/terminate processLet’s see how to use each of them.
Environment variables can be accessed via the env
attribute:console.log(process.env) { SHELL: '/bin/bash', USER: 'jordan', HOME: '/home/jordan', ... }
A short one-liner can set the environment variable in bash, and then run Node eval to print the value. This is a bash/Terminal/ Command Prompt command which will print “development”:NODE_ENV=development node -e "console.log(process.env.NODE_ENV)"
NODE_ENV is a convention. Common values include:
This is just a convention but some libraries and frameworks will augment their behavior to hide error messages, e.g., Express.
To access CLI arguments, use the process.argv
property which is an array.
For example, if the command isnode app.js arg1 arg2 arg3=val3
The first two elements are ‘node’ and the application’s name while the rest are the command-line arguments. Thus, process.argv
:[ 'node', 'app.js', 'arg1', 'arg2', 'arg3=val3' ]
To exit a process, use the exit
function:process.exit()
When your application encounters an error, you want to exit with errors. Exit codes can also be specified// this process failed process.exit(1) // this process failed with a different code process.exit(129) // this process exits successfully process.exit(0)
Different failure codes can be used to differentiate types of failure. And knowing how an application failed allows the developers the means to program an appropriate response.
Comments