1. Install NodeJS
sudo apt-get install nodejs
2. Check NodeJS Version
node -v
3. Create a New NodeJS Project
npm init
4. Install Dependencies
npm install express
5. Create a New NodeJS File
touch app.js
6. Run a NodeJS File
node app.js
7. Use the `require` Function
const express = require('express');
8. Use the `module.exports` Object
module.exports = function() {
console.log('Hello, World!');
};
9. Use the `console.log` Function
console.log('Hello, World!');
10. Use the `process` Object
console.log(process.argv);
11. Use the `fs` Module
const fs = require('fs');
fs.readFile('file.txt', 'utf8', (err, data) => {
console.log(data);
});
12. Use the `http` Module
const http = require('http');
http.createServer((req, res) => {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello, World!\n');
}).listen(3000, () => {
console.log('Server running on port 3000');
});
13. Use the `https` Module
const https = require('https');
https.createServer((req, res) => {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello, World!\n');
}).listen(3000, () => {
console.log('Server running on port 3000');
});
14. Use the `path` Module
const path = require('path');
console.log(path.join('path', 'to', 'file'));
15. Use the `url` Module
const url = require('url');
console.log(url.parse('https://example.com/path/to/resource'));
16. Use the `querystring` Module
const querystring = require('querystring');
console.log(querystring.parse('key=value&foo=bar'));
17. Use the `punycode` Module
const punycode = require('punycode');
console.log(punycode.encode('example.com'));
18. Use the `dns` Module
const dns = require('dns');
dns.lookup('example.com', (err, address) => {
console.log(address);
});
19. Use the `net` Module
const net = require('net');
const server = net.createServer((socket) => {
socket.on('data', (data) => {
console.log(data.toString());
});
});
server.listen(3000, () => {
console.log('Server running on port 3000');
});
20. Use the `dgram` Module
const dgram = require('dgram');
const server = dgram.createSocket('udp4');
server.on('message', (msg, rinfo) => {
console.log(msg.toString());
});
server.bind(3000, () => {
console.log('Server running on port 3000');
});
21. Use the `child_process` Module
const childProcess = require('child_process');
const child = childProcess.spawn('ls', ['-l']);
child.stdout.on('data', (data) => {
console.log(data.toString());
});
22. Use the `cluster` Module
const cluster = require('cluster');
const numCPUs = require('os').cpus().length;
if (cluster.isMaster) {
console.log(`Master ${process.pid} is running`);
for (let i = 0; i < num CPUs; i++) {
cluster.fork();
}
} else {
require('./app.js');
console.log(`Worker ${process.pid} started`);
}
23. Use the `events` Module
const EventEmitter = require('events');
const myEmitter = new EventEmitter();
myEmitter.on('event', () => {
console.log('An event occurred!');
});
myEmitter.emit('event');
24. Use the `stream` Module
const { Readable } = require('stream');
const readable = new Readable({
read() {
this.push('Hello, World!');
this.push(null);
}
});
readable.pipe(process.stdout);
25. Use the `buffer` Module
const buf = Buffer.from('Hello, World!');
console.log(buf.toString());
26. Use the `os` Module
const os = require('os');
console.log(os.platform());
27. Use the `crypto` Module
const crypto = require('crypto');
const hash = crypto.createHash('sha256');
hash.update('Hello, World!');
console.log(hash.digest('hex'));
28. Use the `zlib` Module
const zlib = require('zlib');
const input = 'Hello, World!';
const buffer = Buffer.from(input);
zlib.deflate(buffer, (err, buffer) => {
console.log(buffer.toString('base64'));
});
29. Use the `vm` Module
const vm = require('vm');
const script = new vm.Script('console.log("Hello from VM!");');
script.runInThisContext();
30. Use the `timers` Module
const timers = require('timers');
timers.setTimeout(() => {
console.log('Executed after 1 second');
}, 1000);
31. Use the `readline` Module
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('What is your name? ', (answer) => {
console.log(`Hello, ${answer}!`);
rl.close();
});
32. Use the `assert` Module
const assert = require('assert');
assert.strictEqual(1, 1, 'Values are not equal');
33. Use the `util` Module
const util = require('util');
const promisified = util.promisify(setTimeout);
promisified(1000).then(() => console.log('Executed after 1 second'));
34. Use the `http2` Module
const http2 = require('http2');
const server = http2.createServer((req, res) => {
res.writeHead(200);
res.end('Hello, World!');
});
server.listen(3000);
35. Use the `inspector` Module
const inspector = require('inspector');
inspector.open(9229, null, true);
console.log('Inspector is running on port 9229');
36. Use the `module` Object
console.log(module.filename);
37. Use the `global` Object
global.myGlobalVar = 'Hello, World!';
console.log(global.myGlobalVar);
38. Use the `this` Keyword
console.log(this);
39. Use the `__dirname` Variable
console.log(__dirname);
40. Use the `__filename` Variable
console.log(__filename);
41. Use the `npm` Command
npm install <package-name>
42. Use the `npx` Command
npx create-react-app my-app
43. Use the `npm start` Command
npm start
44. Use the `npm test` Command
npm test
45. Use the `npm run` Command
npm run <script-name>
46. Use the `npm install --save` Command
npm install --save <package-name>
47. Use the `npm install --save-dev` Command
npm install --save-dev <package-name>
48. Use the `npm uninstall` Command
npm uninstall <package-name>
49. Use the `npm update` Command
npm update
50. Use the `npm outdated` Command
npm outdated
51. Use the `npm init -y` Command
npm init -y
52. Use the `npm config` Command
npm config set <key> <value>
53. Use the `npm cache` Command
npm cache clean --force
54. Use the `npm link` Command
npm link <package-name>
55. Use the `npm audit` Command
npm audit
56. Use the `npm audit fix` Command
npm audit fix
57. Use the `npm run build` Command
npm run build
58. Use the `npm run dev` Command
npm run dev
59. Use the `npm run lint` Command
npm run lint
60. Use the `npm run test` Command
npm run test
61. Use the `npm run format` Command
npm run format
62. Use the `npm run serve` Command
npm run serve
63. Use the `npm run watch` Command
npm run watch
64. Use the `npm run clean` Command
npm run clean
65. Use the `npm run deploy` Command
npm run deploy
66. Use the `npm run prestart` Command
npm run prestart
67. Use the `npm run postinstall` Command
npm run postinstall
68. Use the `npm run pretest` Command
npm run pretest
69. Use the `npm run posttest` Command
npm run posttest
70. Use the `npm run prebuild` Command
npm run prebuild
71. Use the `npm run postbuild` Command
npm run postbuild
72. Use the `npm run predeploy` Command
npm run predeploy
73. Use the `npm run postdeploy` Command
npm run postdeploy
74. Use the `npm run preformat` Command
npm run preformat
75. Use the `npm run postformat` Command
npm run postformat
76. Use the `npm run prelint` Command
npm run prelint
77. Use the `npm run postlint` Command
npm run postlint
78. Use the `npm run prewatch` Command
npm run prewatch
79. Use the `npm run postwatch` Command
npm run postwatch
80. Use the `npm run preclean` Command
npm run preclean
81. Use the `npm run postclean` Command
npm run postclean
82. Use the `npm run preinstall` Command
npm run preinstall
83. Use the `npm run postinstall` Command
npm run postinstall
84. Use the `npm run preuninstall` Command
npm run preuninstall
85. Use the `npm run postuninstall` Command
npm run postuninstall
86. Use the `npm run preupdate` Command
npm run preupdate
87. Use the `npm run postupdate` Command
npm run postupdate
88. Use the `npm run prewatch` Command
npm run prewatch
89. Use the `npm run postwatch` Command
npm run postwatch
90. Use the `npm run pretest` Command
npm run pretest
91. Use the `npm run posttest` Command
npm run posttest
92. Use the `npm run prebuild` Command
npm run prebuild
93. Use the `npm run postbuild` Command
npm run postbuild
94. Use the `npm run predeploy` Command
npm run predeploy
95. Use the `npm run postdeploy` Command
npm run postdeploy
96. Use the `npm run preformat` Command
npm run preformat
97. Use the `npm run postformat` Command
npm run postformat
98. Use the `npm run prelint` Command
npm run prelint
99. Use the `npm run postlint` Command
npm run postlint
100. Use the `npm run prewatch` Command
npm run prewatch