Categories
JavaScript Answers

How to install Node.js nvm in Docker?

To install Node.js nvm in Docker, we run curl to install it.

For instance, in our dockerfile, we add

SHELL ["/bin/bash", "--login", "-c"]

RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash
RUN nvm install 10.15.3

to run curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash

to download the nvm install script and run bash to install it with the downloaded script.

And then we run

nvm install 10.15.3

to install nvm 10.15.3.

Categories
JavaScript Answers

How to fix Socket.io and Node.js Cross-Origin request blocked?

To fix Socket.io and Node.js Cross-Origin request blocked, we add a cors option into the server.

For instance, we write

const io = require("socket.io")(server, {
  cors: {
    origin: "*",
  },
});

to set the cors.origin property to allow connection from all origins.

Categories
JavaScript Answers

How to print a list of all installed Node.js modules?

To print a list of all installed Node.js modules, we use the list option.

For instance, we run

npm -g list

to list all the globally installed packages.

We run

npm list

to list the packages installed in the project folder.

Categories
JavaScript Answers

How to force strict mode in Node.js?

To force strict mode in Node.js, we use the 'use strict' directive.

For instance, we write

"use strict";

to our JavaScript files to enable strict mode.

Categories
JavaScript Answers

How to Create and Use Enum in Mongoose and JavaScript?

To create and use enum in Mongoose and JavaScript, we can add an enum field into the schema.

For instance, we write

const userSchema = new mongoose.Schema({
  userType: {
    type: String,
    enum: ["user", "admin"],
    default: "user",
  },
});

to create a schema with Schema.

We call it with an object that has the enum property with the possible values for the enum to create the userType enum field that is a string that can either be 'user' or 'admin'.