Categories
JavaScript Answers

How to use the Underscore module with Node.js

To use the Underscore module with Node.js, we call require to include the underscore module.

For instance, we write

const _ = require("underscore");

to import the underscore module in our JavaScript code to use its functions.

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.