Categories
JavaScript Answers

How to use Node.js filesystem with async / await?

Sometimes, we want to use Node.js filesystem with async / await.

In this article, we’ll look at how to use Node.js filesystem with async / await.

How to use Node.js filesystem with async / await?

To use Node.js filesystem with async / await, we can use the fs.promises module.

For instance, we write

import fs from 'fs';
const fsPromises = fs.promises;

const listDir = async () => {
  try {
    return fsPromises.readdir('path/to/dir');
  } catch (err) {
    console.error('Error occured while reading directory!', err);
  }
}

listDir();

to define the listDir function that calls fsPromises.readdir with the path we want to list.

We use await to get the result resolved by the promise returned.

And we use try and catch to catch any errors if the promise is rejected.

Conclusion

To use Node.js filesystem with async / await, we can use the fs.promises module.

Categories
JavaScript Answers

How to add error handling to Node.js streams?

Sometimes, we want to add error handling to Node.js streams.

In this article, we’ll look at how to add error handling to Node.js streams.

How to add error handling to Node.js streams?

To add error handling to Node.js streams, we call pipeline with all the operators before the error handler.

For instance, we write

const {
  pipeline,
  finished
} = require('stream');

pipeline(
  input,
  transformA,
  transformB,
  transformC,
  (err) => {
    if (err) {
      console.error('Pipeline failed', err);
    } else {
      console.log('Pipeline succeeded');
    }
  });


finished(input, (err) => {
  if (err) {
    console.error('Stream failed', err);
  } else {
    console.log('Stream is done reading');
  }
});

to call pipeline with the operators input, transformA, and transformB, and transformC.

Then we pass in the error handler callback so that errors with any of the operators will be picked up by it.

Then we call finished with input to handle any errors with input.

Conclusion

To add error handling to Node.js streams, we call pipeline with all the operators before the error handler.

Categories
JavaScript Answers

How to remove menu bar from Electron app?

Sometimes, we want to remove menu bar from Electron app.

In this article, we’ll look at how to remove menu bar from Electron app.

How to remove menu bar from Electron app?

To remove menu bar from Electron app, we can set the autoHideMenuBar to true.

For instance, we write

const mainWindow = new BrowserWindow({
  autoHideMenuBar: true,
})

to create a BrowserWindow instance by passing in an object with autoHideMenuBar set to true.

As a result, the menu bar would be hidden in the app.

Conclusion

To remove menu bar from Electron app, we can set the autoHideMenuBar to true.

Categories
JavaScript Answers

How to restart Node upon changing a file?

Sometimes, we want to restart Node upon changing a file.

In this article, we’ll look at how to restart Node upon changing a file.

How to restart Node upon changing a file?

To restart Node upon changing a file, we can use the supervisor package.

We install it by running

npm install supervisor -g

Then we can watch for file changes in the Node project with entry point app.js by running

supervisor app.js

Conclusion

To restart Node upon changing a file, we can use the supervisor package.

Categories
JavaScript Answers

How to move files in Node.js?

Sometimes, we want to move files in Node.js.

In this article, we’ll look at how to move files in Node.js.

How to move files in Node.js?

To move files in Node.js, we can use the fs.rename method.

For instance, we write

const fs = require('fs')

const oldPath = 'old/path/file.txt'
const newPath = 'new/path/file.txt'

fs.rename(oldPath, newPath, (err) => {
  if (err) {
    throw err
  }
  console.log('successfully moved')
})

to call rename with the oldPath of the file, newPath of the file, and a callback that runs when the moving is done.

If there’s an error, err will be set.

Conclusion

To move files in Node.js, we can use the fs.rename method.