Categories
JavaScript Answers

How to fix AWS missing credentials when trying send something to S3 Bucket in Node.js?

To fix AWS missing credentials when trying send something to S3 Bucket in Node.js, we call the config.update method.

For instance, we write

AWS.config.update({
  accessKeyId: "YOURKEY",
  secretAccessKey: "YOURSECRET",
  region: "sa-east-1",
});

const s3 = new AWS.S3();
const params = {
  Bucket: "makersquest",
  Key: "mykey.txt",
  Body: "HelloWorld",
};
s3.putObject(params, (err, res) => {
  if (err) {
    console.log("Error uploading data: ", err);
  } else {
    console.log("Successfully uploaded data to myBucket/myKey");
  }
});

to call update to update access key and secret key to connect to AWS.

Categories
JavaScript Answers

How to loop through JSON with Node.js?

To loop through JSON with Node.js, we use the forEach method.

For instance, we write

const objectKeysArray = Object.keys(yourJsonObj);

objectKeysArray.forEach((objKey) => {
  const objValue = yourJsonObj[objKey];
});

to call Object.keys to get the object’s keys in an array.

Then we call forEach with a callback to get the property value with yourJsonObj[objKey].

Categories
JavaScript Answers

How to fix “nodemon” not recognized with Node?

To fix "nodemon" not recognized with Node, we’ve to install the nodemon package.

To do this, we run

npm install -g nodemon

to install nodemon globally with npm install -g.

Categories
JavaScript Answers

How to fix ‘node’ is not recognized as an internal or external command error?

To fix ‘node’ is not recognized as an internal or external command error, we add the Node executable folder to the PATH environment variable.

For instance, we run

SET PATH=C:\Program Files\Nodejs;%PATH%

to add the Node executable folder to the PATH with SET on Windows.

Categories
JavaScript Answers

How to require from URL in Node.js?

To require from URL in Node.js, we use the require-from-url package.

To install it, we run

npm install require-from-url

Then we use it by writing

const requireFromUrl = require("require-from-url/sync");
const module = requireFromUrl("http://example.com/nodejsmodules/myModule.js");

to call requireFromUrl with the URL of the module to return the module.