5 Must-Know Node.js Methods (With Examples)

Published on 16th March, 2025

If you're working with Node.js, there are some built-in methods that you’ll use again and again. These methods help you read files, handle paths, manage environment variables, and more.

In this post, I'll walk you through 5 important Node.js methods with simple examples so you can use them in your projects.

Let’s dive in! πŸš€

1. Read a File (fs.readFile)

When working with files, you often need to read data from them. The fs.readFile() method allows you to do this asynchronously.

Example:

const fs = require('fs');

fs.readFile('example.txt', 'utf8', (err, data) => {
  if (err) {
    console.error('Error reading file:', err);
    return;
  }
  console.log('File content:', data);
});

πŸ’‘ Tip: Always handle errors properly to avoid crashes when a file is missing.

2. Write to a File (fs.writeFile)

Need to save some data to a file? fs.writeFile() lets you write data to a file asynchronously.

Example:

const fs = require('fs');

const content = 'Hello, this is a test file!';

fs.writeFile('output.txt', content, (err) => {
  if (err) {
    console.error('Error writing file:', err);
    return;
  }
  console.log('File written successfully!');
});

πŸ’‘ Tip: If the file already exists, this method will overwrite it. Use fs.appendFile() if you want to add new content instead.

3. Build File Paths (path.join)

Node.js runs on different operating systems, and file paths can be tricky. path.join() ensures your file paths work across all platforms.

Example:

const path = require('path');

const filePath = path.join(__dirname, 'files', 'data.txt');
console.log('File path:', filePath);

πŸ’‘ Tip: This is useful when working with directories and avoiding hardcoded paths.

4. Access Environment Variables (process.env)

Sensitive data like API keys, database credentials, and configurations should not be hardcoded. Use process.env to access environment variables securely.

Example:

console.log('Database URL:', process.env.DB_URL);

πŸ’‘ Tip: Store your environment variables in a .env file and use the dotenv package to load them into process.env.

5. Delay Execution (setTimeout)

Sometimes, you need to delay a function for a few seconds. setTimeout() makes this easy.

Example:

console.log('Start');

setTimeout(() => {
  console.log('This runs after 2 seconds');
}, 2000);

πŸ’‘ Tip: Great for retries, animations, and async operations where you need a slight delay.

Conclusion

Here’s a quick recap of the 5 important Node.js methods we covered:

βœ… fs.readFile() – Reads files asynchronously.

βœ… fs.writeFile() – Saves data to a file.

βœ… path.join() – Builds correct file paths.

βœ… process.env – Manages environment variables.

βœ… setTimeout() – Delays execution of a function.

These methods are simple but powerful, and you’ll use them often as a Node.js developer.

Which one do you use the most? Let me know in the comments! 😊

#Nodejs #JavaScript #WebDevelopment #CodingTips

Comments

Please login to publish your comment!

By logging in, you agree to our Terms of Service and Privacy Policy.


No comments here!