Categories
JavaScript Answers

How to fix Heroku Node.js app deploy failed `error code=H10`?

To fix Heroku Node.js app deploy failed error code=H10, we should not hard code the port for our app.

For instance, we write

app.listen(process.env.PORT || 3000, () => {});

to call listen with the PORT environment variable or 3000 as a fallback to run with the port from the environment variable or on port 3000 as the default.

Categories
JavaScript Answers

How to run Sequelize.js delete query with Node.js?

To run Sequelize.js delete query with Node.js, we call the destroy method.

For instance, we write

Model.destroy({
  where: {
    // criteria
  },
});

to call Model.destroy with an object with the where property set to the criteria for the entries we want to delete.

Categories
JavaScript Answers

How to fix sudo node command not found, but node without sudo is working on EC2?

To fix sudo node command not found, but node without sudo is working on EC2, we add some symlinks as an admin.

We run

sudo ln -s /usr/local/bin/node /usr/bin/node
sudo ln -s /usr/local/lib/node /usr/lib/node
sudo ln -s /usr/local/bin/npm /usr/bin/npm
sudo ln -s /usr/local/bin/node-waf /usr/bin/node-waf

to add the symlinks for the directories for the Node.js paths in our EC2 virtual machine to run Node as root.

Categories
JavaScript Answers

How to include Node.js ES6 classes with require?

To include Node.js ES6 classes with require, we set module.exports to the class.

Then we can use require to import the class.

For instance, we write

class Animal {
  //...
}
module.exports = Animal;

to set module.exports to the Animal class to export the class in Animal.js.

Then we write

const Animal = require("./Animal");

class Dog extends Animal {
  //...
}

to require the Animal.js file to import the class.

And then we create the Dog class that inherits from the Animal class.

Categories
JavaScript Answers

How to suppress output when running npm scripts with JavaScript?

To suppress output when running npm scripts with JavaScript, we run npm run with the silent option.

For instance, we run

npm run --silent your-script

to run your-script without showing any output with the --silent option.