Categories
JavaScript Answers

How to write files in Node.js?

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

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

How to write files in Node.js?

To write files in Node.js, we can use the fs.writeFile method.

For instance, we write

const fs = require('fs');

fs.writeFile("/tmp/test", "Hey there!", (err) => {
  if (err) {
    return console.log(err);
  }
  console.log("The file was saved!");
});

to call fs.writeFile with the path to write the file to, the file content string, and a callback that’s run when writeFile is finished running.

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

Conclusion

To write files in Node.js, we can use the fs.writeFile method.

Categories
JavaScript Answers

How to exit a Node.js program?

Sometimes, we want to exit a Node.js program.

In this article, we’ll look at how to exit a Node.js program.

How to exit a Node.js program?

To exit a Node.js program, we can call process.exit.

For instance, we run

process.exit()

to exit the program without any exit code.

Or we can write

process.exit(1)

to exit the Node.js program with exit code 1.

Conclusion

To exit a Node.js program, we can call process.exit.

Categories
JavaScript Answers

How to use async/await with a forEach loop with Node.js?

Sometimes, we want to use async/await with a forEach loop with Node.js.

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

How to use async/await with a forEach loop with Node.js?

To use async/await with a forEach loop with Node.js, we can use the for-of loop.

For instance, we write

async function printFiles() {
  const files = await getFilePaths();

  for (const file of files) {
    const contents = await fs.readFile(file, 'utf8');
    console.log(contents);
  }
}

to loop through the files array and call fs.readFile on the file.

The await keyword will wait for the promise returned by readFile to resolve before moving to the next iteration.

Conclusion

To use async/await with a forEach loop with Node.js, we can use the for-of loop.

Categories
JavaScript Answers

How to find the version of an installed NPM package?

Sometimes, we want to find the version of an installed NPM package.

In this article, we’ll look at how to find the version of an installed NPM package.

How to find the version of an installed NPM package?

To find the version of an installed NPM package, we can use the npm list command.

We run

npm list

to find the version numbers for local packages.

And we run

npm list -g

to find the version numbers for global packages.

We can also run npm list with the package name to find the version of the package installed.

For instance, we run

npm list left-pad

to find the version of the left-pad package installed.

Conclusion

To find the version of an installed NPM package, we can use the npm list command.

Categories
JavaScript Answers

How to pass command line arguments to a Node.js program?

Sometimes, we want to pass command line arguments to a Node.js program.

In this article, we’ll look at how to pass command line arguments to a Node.js program.

How to pass command line arguments to a Node.js program?

To pass command line arguments to a Node.js program, we can get them from the process.argv array property.

For instance, we write

process.argv.forEach((val, index, array) => {
  console.log(index, val);
});

to call process.argv.forEach with a callback to get the value for each command line argument in our Node app.

val has the command line argument value at the given array index.

Conclusion

To pass command line arguments to a Node.js program, we can get them from the process.argv array property.