DEV Community

SOVANNARO
SOVANNARO

Posted on

Importing JSON and Watch Mode in Node.js πŸš€

Hey there, amazing devs! πŸ‘‹ Today, we’re diving into two super useful features in Node.js: Importing JSON and Watch Mode. These features make development easier and more efficient, so let’s break them down in a fun and simple way! 😎


πŸ“₯ Importing JSON in Node.js

In many projects, we need to work with JSON data, like configuration files or API responses. Node.js makes it super easy to import JSON files directly! πŸš€

πŸ”Ή Traditional Way (Using require)

Before ES Modules, the most common way to import JSON was using require:

const config = require('./config.json');
console.log(config);
Enter fullscreen mode Exit fullscreen mode

βœ… This works only in CommonJS (require is not available in ES Modules).

πŸ”Ή Modern Way (Using import)

If you're using ES Modules, you need to explicitly enable JSON imports by adding the assert option:

import config from "./config.json" assert { type: "json" };
console.log(config);
Enter fullscreen mode Exit fullscreen mode

βœ… This method works only in ES Modules (type: "module" in package.json).

πŸ”Ή Alternative: fs.readFileSync()

If you need to load JSON dynamically, use the File System (fs) module:

import { readFileSync } from 'fs';
const data = JSON.parse(readFileSync('./config.json', 'utf8'));
console.log(data);
Enter fullscreen mode Exit fullscreen mode

βœ… Works in both CommonJS and ES Modules!


πŸ‘€ Watch Mode (--watch)

Tired of restarting your Node.js app manually after every change? Watch Mode is here to save the day! πŸš€

πŸ”Ή What is Watch Mode?

Watch Mode automatically restarts your Node.js application whenever you make changes to the source code. No more stopping and restarting manually! πŸŽ‰

πŸ”Ή How to Use Watch Mode?

Simply add the --watch flag when running your Node.js file:

node --watch app.js
Enter fullscreen mode Exit fullscreen mode

Boom! Now, any time you edit and save app.js, Node.js will restart automatically. πŸ”„

πŸ”Ή Watch Mode with ES Modules

If you're using ES Modules, don’t forget to enable the type: "module" setting in package.json:

{
  "type": "module"
}
Enter fullscreen mode Exit fullscreen mode

Then run your script with:

node --watch app.js
Enter fullscreen mode Exit fullscreen mode

βœ… Works perfectly with ES Modules!

πŸ”Ή Watch Mode with CommonJS

For CommonJS, just run:

node --watch server.js
Enter fullscreen mode Exit fullscreen mode

No extra setup needed!


πŸš€ Final Thoughts

Both Importing JSON and Watch Mode make Node.js development smoother and faster. JSON imports keep your data handling clean, while Watch Mode saves you time by automatically restarting your app.

This is just the beginning! In the next article, we’ll explore more Path Moduleβ€”stay tuned! 🎯

If you found this blog helpful, make sure to follow me on GitHub πŸ‘‰ github.com/sovannaro and drop a ⭐. Your support keeps me motivated to create more awesome content! πŸš€

Happy coding! πŸ’»πŸ”₯

Top comments (0)