Node cli
Introduction Node
Node.js is an open-source and cross-platform JavaScript runtime environment. It is a popular tool for almost any kind of project! Node.js runs the V8 JavaScript engine, the core of Google Chrome, outside of the browser. This allows Node.js to be very performant. A Node.js app runs in a single process, without creating a new thread for every request. Node.js provides a set of asynchronous I/O primitives in its standard library that prevent JavaScript code from blocking and generally, libraries in Node.js are written using non-blocking paradigms, making blocking behavior the exception rather than the norm.
Install NodeJS
To get the latest version, you can use the APT package manager. Refresh your local package index first:
sudo apt update
Then install Node.js:
sudo apt install nodejs
Check that the install was successful by querying node for its version number:
node -v
If the package in the repositories suits your needs, this is all you need to do to get set up with Node.js. In most cases, you’ll also want to also install npm, the Node.js package manager. You can do this by installing the npm package with apt:
sudo apt install npm
This allows you to install modules and packages to use with Node.js. At this point, you have successfully installed Node.js and npm using apt and the default Ubuntu software repositories.
First project with NodeJS
As first project with node cli this video is recommended, it give you the first instractions to start with node CLI and explain a lot of concepts : https://www.youtube.com/watch?v=dfTpFFZwazI (opens in a new tab)
Command Line
NodeJS comes with a variety of CLI options. These options expose built-in debugging, multiple ways to execute scripts, and other helpful runtime options.
Run project
We have to make sure that your terminal is pointed directly in to the project. To run node project :
node .
or,
./index.js
Specific command
Create command
Create function in index.js :
if (input.includes('flag')) {
console.log('Start Flag');
console.log('output flags :', flags );
console.log('output flag type :', flags.type );
console.log('output flag clean :', flags.clean );
console.log('End Flag');
}
Execute command
After creating the function in index.js, we can test our function :
./index.js `function name` [flag]
Parameter command
Create flag
The creation of a new flag for NodeJS is in the file : ./utils/cli.js The structure have to be the following :
clear: {
type: `boolean`, // type of value
default: false, // default value
alias: `c`, // shortcut used to call the flag
desc: `Clear the console` // description explain the purpose of this flag
},
Executing flag
There is two way to execute the flag :
./index.js [--flag]
example : ./index.js --clear true
or,
./index.js [-alias]
example : ./index.js -c true
References
- Official documentation of Node CLI : https://nodejs.org/api/cli.html (opens in a new tab)
- Documentation wiki for Node : https://en.wikipedia.org/wiki/Node.js (opens in a new tab)
- CRUD Generator documentation : CRUD Generator