Categories
JavaScript Answers

How to use module.exports as a constructor with Node.js?

To use module.exports as a constructor with Node.js, we export it as a default export.

For instance, we write

class Square {
  constructor(width) {
    this.width = width;
  }
  area() {
    return Math.pow(this.width, 2);
  }
}

module.exports = Square;

to export the Square class in square.js.

Then we write

const Square = require("./square");

const s = new Square(5);
const area = s.area();

to import the Square constructor from square.js with require.

And then we call Square and use its methods.

Categories
JavaScript Answers

How to install a Node.js module without using npm?

To install a Node.js module without using npm, we download the module into the node_modules directory.

After we downloaded the module into the folder, we write

const moduleName = require("foo");

in our code to import the foo module assuming that’s what we downloaded.

Categories
JavaScript Answers

How to fix Error: ENOENT, stat ‘./path/to/file’ error in Node.js?

To fix Error: ENOENT, stat ‘./path/to/file’ error in Node.js, we need to use the __dirname variable to access an absolute path in the project.

For instance, we write

const path = require("path");

const fullPath = path.join(__dirname, "path/to/file");

to call path.join to combine the project directory __dirname with the relative path in the project folder we want to access.

join returns the full path as a string.

Categories
JavaScript Answers

How to do MongoDB atomic “findOrCreate”: findOne, insert if nonexistent, but do not update with JavaScript?

To do MongoDB atomic "findOrCreate": findOne, insert if nonexistent, but do not update with JavaScript, we use the findAndModify method.

For instance, we write

db.collection.findAndModify({
  query: { _id: "id" },
  update: {
    $setOnInsert: { foo: "bar" },
  },
  new: true,
  upsert: true,
});

to call findAndModify with an object with the query to look for the item with the ID 'id'.

We set update to the field on update.

upsert is set to true to insert a new entry if the item doesn’t exist.

Categories
JavaScript Answers

How to set up environment specific configs to be used with everyauth with Node.js?

To set up environment specific configs to be used with everyauth with Node.js, we return the config according to the NODE_ENV environment variable.

For instance, we write

module.exports = () => {
  switch (process.env.NODE_ENV) {
    case "development":
      return {
        //...
      };
    case "production":
      return {
        //...
      };
    default:
      return {
        //...
      };
  }
};

in the config.js file to return the config according to the NODE_ENV environment variable value.

Then we write

const Config = require("./config");
const conf = Config();

to require the config.js file and then call the imported Config function to return the config for the environment in app.js.

And then we run

NODE_ENV=production node app.js

to set the NODE_ENV environment variable value and run app.js.