Categories
JavaScript Answers

How to remove a file with Node.js?

Sometimes, we want to remove a file with Node.js.

In this article, we’ll look at how to remove a file with Node.js.

How to remove a file with Node.js?

To remove a file with Node.js, we can use the fs.unlink` method.

For instance, we write

fs.unlink('./server/my.csv', (err) => {
  if (err) {
    return console.log(err);
  }
  console.log('file deleted successfully');
});

to call fs.unlink with the path of the file to delete and a callback that runs when unlink is finished.

err is set when there’s an error.

We can also call unlinkSync to delete a file synchronously.

For instance, we write

const fs = require('fs');
const filePath = './server/my.csv';
fs.unlinkSync(filePath);

to call unlinkSync with the filePath of the file to delete.

Conclusion

To remove a file with Node.js, we can use the fs.unlink` method.

Categories
JavaScript Answers

How to store Node.js deployment settings/configuration files?

Sometimes, we want to store Node.js deployment settings/configuration files.

In this article, we’ll look at how to store Node.js deployment settings/configuration files.

How to store Node.js deployment settings/configuration files?

To store Node.js deployment settings/configuration files, we can use the dotenv package.

To install it, we run

npm i dotenv

Then we write

const dotenv = require('dotenv');
dotenv.load();

to require the dotenv package.

And then we call dotenv.load to load our config file.

Then in our .env file, we can put some config entries like

S3_BUCKET=bucket
SECRET_KEY=secret
OTHER_SECRET_STUFF=secret

And the data from the .env file will be put in the process.env object with the keys in .env as the property names.

Conclusion

To store Node.js deployment settings/configuration files, we can use the dotenv package.

Categories
JavaScript Answers

How to call async/await functions in parallel with Node.js?

Sometimes, we want to call async/await functions in parallel with Node.js.

In this article, we’ll look at how to call async/await functions in parallel with Node.js.

How to call async/await functions in parallel with Node.js?

To call async/await functions in parallel with Node.js, we can call Promise.all.

For instance, we write

await Promise.all([someCall(), anotherCall()]);

to call Promise.all with an array of promises.

We use await to wait for all promises to resolve until we move onto the next line.

And we can store the resolved values with

const [someResult, anotherResult] = await Promise.all([someCall(), anotherCall()]);

Conclusion

To call async/await functions in parallel with Node.js, we can call Promise.all.

Categories
JavaScript Answers

How to get version from package.json in Node.js code?

Sometimes, we want to get version from package.json in Node.js code.

In this article, we’ll look at how to get version from package.json in Node.js code.

How to get version from package.json in Node.js code?

To get version from package.json in Node.js code, we can import the content of package.json.

For instance, we write

import {
  version
} from './package.json';

to import the version property from package.json as a string.

Conclusion

To get version from package.json in Node.js code, we can import the content of package.json.

Categories
JavaScript Answers

How to run a command line binary with Node.js?

Sometimes, we want to run a command line binary with Node.js.

In this article, we’ll look at how to run a command line binary with Node.js.

How to run a command line binary with Node.js?

To run a command line binary with Node.js, we can use the child_process module.

For instance, we write

const {
  exec
} = require('child_process');

exec('cat *.js bad_file | wc -l', (err, stdout, stderr) => {
  if (err) {
    return;
  }
  console.log(`stdout: ${stdout}`);
  console.log(`stderr: ${stderr}`);
});

to call exec with the command we want to run and the callback that runs when the command is done.

In the callback we return the standout output content with stdout.

And we return the standard error output with stderr.

Conclusion

To run a command line binary with Node.js, we can use the child_process module.