What is Node.js?
Node.js is an open-source
JS runtime
that allows you to execute JavaScript code on the server side. It’s built on Chrome's V8 JavaScript engine.Runtime?
The environment where JavaScript code is executed. It could be
- On the server(backend)
- In the browser(frontend)
V8 engine?
The V8 engine is an open-source JavaScript engine developed by Google. It is used to execute JavaScript code in various environments, most notably in the Google Chrome web browser.
Installing Node.js
- Build from source
- Using a package manager (brew, chocolat)
- Using nvm
What is bun?
Like Node.js, Bun is a JavaScript runtime that allows you to execute JavaScript code on the server side.
Installing bun
Linux
curl -fsSL https://bun.sh/install | bash
Starting a Node.js project
To
initialize
a Node.js project locally,- Run the following command -
npm init -y
- Exploring package.json
- Writeing some code
let name="tushar"; console.log(name);
- Run the code
node index.js
- Add a
script
inpackage.json
"scripts":{ "start":"node index.js" },
- Run npm run start
💡The full form of NPM is Node Package Manager.
It is a package manager for JavaScript, primarily used for managing libraries and dependencies in Node.js projects. NPM allows developers to easily install, update, and manage packages of reusable code
💡
package managers
are an important concept in programming languages/runtimes.
For eg the package manager
of rust is cargo
Node.js provide you some internal packages also.
Internal Packages vs External Packages
Internal Packages
Some common ones include
- fs - Filesystem
- path - Path related functions
- http - Create HTTP Servers (we’ll discuss this tomorrow)
fs package
The fs (Filesystem) package is used to read, write, update contents on the filesystem.
const fs=require("fs"); const path=require("path"); const filePath=path.join(__dirname,'a.txt'); fs.readFile(filePath, 'utf8',(err,data)=>{ if(err){ console.log(err); } else { console.log(data); } })
External Packages
Some smart developers have written libraries like Chalk, Express, and many more, and published them on the NPM Registry. The NPM Registry is a vast collection of open-source JavaScript libraries and packages available for public use. It is managed by npm, Inc. This registry hosts millions of packages, ranging from simple utility functions to complex frameworks, allowing developers to easily integrate pre-built solutions into their projects.
Semantic Versioning Format
Every external package is updated incrementally. A specific version looks something like follows -
"chalk": "^5.3.0"
The format is as follows -
MAJOR.MINOR.PATCH
- MAJOR - Major version changes indicate significant updates or breaking changes.
- MINOR - Minor version changes signify the addition of new features or improvements in a backward-compatible manner.
- PATCH - Patch version changes include backward-compatible bug fixes or minor improvements that address issues without adding new features or causing breaking changes.
^
(Caret): Updates to the latest minor and patch versions (e.g.,^1.2.3
installs1.2.x
but not2.x.x
).
~
(Tilde): Updates only the patch version (e.g.,~1.2.3
installs1.2.4
but not1.3.0
).
When you run
npm install <package-name>
npm fetches the specified package from the NPM registry, downloads it, and installs it into your project.
You can explore the registry and its packages at ‣
Try Fetching a Package from npm
- Initializing a project
npm init -y
- Make a file name index.js and Install external dependencies
npm install express
- Write some code
const express = require('express') const app = express() app.get('/',(req, res)=> { res.send('Hello World') }) app.listen(3000)
- Run your code
Save this script in a file named
index.js
and execute it using:node index.js
In your
package.json
, you define a start
script like this:"scripts": { "start": "node index.js" }
Now you can run
npm run start
.- Use
node index.js
for simple, direct execution.
- Use
npm run start
for standardized workflows in larger or team-based projects.
package-lock.json
When you run
npm install
, a file named package-lock.json
is generated.The
package-lock.json
records the exact versions of all dependencies and their dependencies (sub-dependencies) that are installed at the time when npm install
was run.You should push the package-lock.json
file to GitHub.You lay a solid foundation for building efficient Node.js applications. With continuous learning and exploration, you can harness Node.js to create everything from simple scripts to complex backend systems.
Happy coding! 🚀