Topic 000: NodeJS intro, File-Folder structure, package.json & package-lock.json, Dependency
Sure, let’s start with an introduction to Node.js and then dive into the common file-folder structure,
package.json
, package-lock.json
, and handling dependencies.
Node.js is a powerful, open-source, server-side runtime environment built on Chrome’s V8 JavaScript engine. It allows developers to use JavaScript to write server-side code, enabling the development of scalable and high-performance network applications.
When setting up a Node.js project, organizing your files and folders in a logical manner is crucial for maintainability and scalability. Here’s a typical structure:
my-node-app/
│
├── node_modules/ # Installed dependencies
├── public/ # Static files (e.g., HTML, CSS, JavaScript)
├── src/ # Source code
│ ├── controllers/ # Controller files (handling business logic)
│ ├── models/ # Database models (e.g., Mongoose schemas)
│ ├── routes/ # Route definitions
│ ├── services/ # Service files (e.g., third-party integrations)
│ └── app.js # Main application setup
│
├── tests/ # Test files
├── .env # Environment variables
├── .gitignore # Git ignore file
├── package.json # Project metadata and dependencies
├── package-lock.json # Exact versions of installed dependencies
├── README.md # Project documentation
└── server.js # Entry point to the application
package.json
& package-lock.json
package.json
The package.json
file is a critical part of any Node.js project. It holds various metadata relevant to the project and is used to manage the project’s dependencies, scripts, versioning, and other configurations.
server.js
).package.json
{
"name": "my-node-app",
"version": "1.0.0",
"description": "A sample Node.js application",
"main": "server.js",
"scripts": {
"start": "node server.js",
"test": "mocha"
},
"dependencies": {
"express": "^4.17.1",
"mongoose": "^5.12.3"
},
"devDependencies": {
"mocha": "^8.3.2",
"chai": "^4.3.4"
},
"author": "Your Name",
"license": "MIT"
}
package-lock.json
The package-lock.json
file is automatically generated when dependencies are installed or updated. It records the exact versions of every installed package, ensuring that the same versions are installed in the future, thus providing consistency across different environments.
package-lock.json
{
"name": "my-node-app",
"version": "1.0.0",
"lockfileVersion": 2,
"requires": true,
"dependencies": {
"express": {
"version": "4.17.1",
"resolved": "https://registry.npmjs.org/express/-/express-4.17.1.tgz",
"integrity": "sha512-...",
"requires": {
"accepts": "~1.3.7"
// other dependencies
}
}
// other dependencies
}
}
Dependencies can be installed using npm or yarn. Here’s how you can install a package:
npm install <package-name> --save # Adds to dependencies
npm install <package-name> --save-dev # Adds to devDependencies
npm install express mongoose --save
npm install mocha chai --save-dev
After installing the dependencies, you can use them in your application by requiring them.
const express = require("express");
const mongoose = require("mongoose");
const app = express();
mongoose.connect("mongodb://localhost:27017/mydatabase", {
useNewUrlParser: true,
useUnifiedTopology: true,
});
app.get("/", (req, res) => {
res.send("Hello World!");
});
app.listen(3000, () => {
console.log("Server is running on port 3000");
});
Node.js is a versatile and powerful platform for server-side development. By understanding the file-folder structure, package.json
, package-lock.json
, and dependency management, you can effectively organize and manage your Node.js projects. This foundational knowledge is essential for building scalable and maintainable applications.