Categories
JavaScript Answers

How to share variables between files in Node.js?

Sometimes, we want to share variables between files in Node.js.

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

How to share variables between files in Node.js?

To share variables between files in Node.js, we can put our shared variables in a module.

For instance, we write

module.js

const name = "foobar";

exports.name = name;

to export the name variable in module.js by assigning it to exports.name.

And then in main.js, we write

main.js

const myModule = require('./module');

const name = myModule.name;

to call require to include the module.js module and assign the exported contents to myModule.

And then we get the exports.name value with myModule.name,

Conclusion

To share variables between files in Node.js, we can put our shared variables in a module.

Categories
JavaScript Answers

How to read a text file using Node.js?

Sometimes, we want to read a text file using Node.js.

In this article, we’ll look at how to read a text file using Node.js.

How to read a text file using Node.js?

To read a text file using Node.js, we call fs.readFile.

For instance, we write

const fs = require('fs')

const filename = '/foo/bar.txt'

fs.readFile(filename, 'utf8', (err, data) => {
  if (err) {
    throw err;
  }
  console.log(data)
})

to call fs.readFile with the filename, the encoding of the text file, and a callback that has the read file data stored in the `data parameter.

If there’s an error when reading the file, err is set.

Conclusion

To read a text file using Node.js, we call fs.readFile.

Categories
JavaScript Answers

How to limit concurrency when using ES6’s Promise.all()?

Sometimes, we want to limit concurrency when using ES6’s Promise.all().

In this article, we’ll look at how to limit concurrency when using ES6’s Promise.all().

How to limit concurrency when using ES6’s Promise.all()?

To limit concurrency when using ES6’s Promise.all(), we can use the p-limit package.

To install it, we run

npm i p-limit

For instance, we write

const pLimit = require('p-limit');

const limit = pLimit(3);

const urls = [
  "http://www.exampleone.com/",
  "http://www.exampletwo.com/",
  "http://www.examplethree.com/",
  "http://www.examplefour.com/",
]

const promises = urls.map(url => {
  return limit(() => fetchData(url));
});

(async () => {
  const result = await Promise.all(promises);
  console.log(result);
})();

to call pLimit with 3 to return the limit function that limits the number of concurrent promises to 3.

Then we call urls.map with a callback that returns the promise that’s returned by limit.

And then call Promise.all with promises to return the results from the promises.

Conclusion

To limit concurrency when using ES6’s Promise.all(), we can use the p-limit package.

Categories
JavaScript Answers

How to fix the “Error: Cannot find module ‘express'” error with Node.js?

Sometimes, we want to fix the "Error: Cannot find module ‘express’" error with Node.js.

In this article, we’ll look at how to fix the "Error: Cannot find module ‘express’" error with Node.js.

How to fix the "Error: Cannot find module ‘express’" error with Node.js?

To fix the "Error: Cannot find module ‘express’" error with Node.js, we should install the express package.

To install it, we run

npm install express

Conclusion

To fix the "Error: Cannot find module ‘express’" error with Node.js, we should install the express package.

Categories
JavaScript Answers

How to configure the region in Node.js AWS SDK?

Sometimes, we want to configure the region in Node.js AWS SDK.

In this article, we’ll look at how to configure the region in Node.js AWS SDK.

How to configure the region in Node.js AWS SDK?

To configure the region in Node.js AWS SDK, we can call the aws.config.update method.

For instance, we write

const AWS = require('aws-sdk');
AWS.config.update({
  region: 'us-east-1'
});

const dd = new AWS.DynamoDB();
const s3 = new AWS.S3();

to set the region to 'us-east-1' with AWS.config.update.

Conclusion

To configure the region in Node.js AWS SDK, we can call the aws.config.update method.