DEV Community

SavvyShivam
SavvyShivam

Posted on • Originally published at savvyshivam.hashnode.dev on

13. Import JSON and Watch Mode in Node.js: A Developer's Guide

JSON (JavaScript Object Notation) is a widely used data interchange format, particularly in web development. In Node.js, you can seamlessly import JSON data as if it were a JavaScript object, making it easy to work with structured data. Additionally, Node.js introduced a watch mode feature in version 18, which greatly enhances the development process by automatically restarting the application when code changes occur. In this article, we'll explore how to import JSON and use the watch mode in Node.js.

Importing JSON Data

Creating a JSON File

To import JSON data as a module in Node.js, you need to create a JSON file with key-value pairs. For example, you can create a file named data.json:

// data.json
{
  "name": "John Doe",
  "age": 30,
  "city": "New York"
}

Enter fullscreen mode Exit fullscreen mode

Importing JSON Data

In Node.js, you can import JSON data using the require function. When you import a JSON file in this way, it's treated as a JavaScript object:

// Import JSON data
const data = require('./data.json');

// Accessing properties from the JSON object
console.log(data.name); // Outputs: John Doe
console.log(data.age); // Outputs: 30
console.log(data.city); // Outputs: New York

Enter fullscreen mode Exit fullscreen mode

Be Mindful of File Extensions

It's essential to be aware that Node.js prioritizes finding a .js file with the same name as the JSON file before the JSON file itself. To avoid confusion and ensure that you're importing the JSON file, it's recommended to use the .json file extension when importing.

Using Watch Mode in Node.js

Introduction to Watch Mode

Node.js introduced a watch mode feature starting from version 18, which is incredibly helpful for developers. Watch mode automatically restarts the Node.js process when code changes occur, eliminating the need for manual restarts during development.

Running a File in Watch Mode

To run a Node.js file in watch mode, you can use the --watch flag with the node command followed by the filename:

node --watch myApp.js

Enter fullscreen mode Exit fullscreen mode

For example, if you have a file named myApp.js, running the above command will start the application in watch mode. Now, any changes made to the myApp.js file will be detected, and the application will automatically restart, reflecting the code changes instantly.

Benefits of Watch Mode

Watch mode offers several benefits to developers:

  • Real-time Updates : Developers can make changes to their code and immediately see the results without manually stopping and restarting the application.

  • Enhanced Productivity : Watch mode eliminates the need to constantly switch between the terminal and the code editor to restart the application manually.

  • Faster Debugging : It streamlines the debugging process by reducing downtime and ensuring that the application is always running.

  • Improved Development Experience : Developers can focus more on coding and less on managing the development environment.

In conclusion, importing JSON data in Node.js is straightforward, allowing developers to work with structured data efficiently. Additionally, the watch mode feature introduced in Node.js version 18 significantly enhances the development experience by automating the process of detecting and reflecting code changes. Whether you're dealing with JSON data or working on a Node.js application, these features contribute to a smoother and more productive development process.

Top comments (0)