Categories
JavaScript Answers

How to install Yarn in Ubuntu?

To install Yarn in Ubuntu, we add the package repository with Yarn before we install it.

We run

curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list

to add the Yarn package repo.

And then we run

sudo apt-get update && sudo apt-get install yarn

to run apt to install yarn.

Categories
JavaScript Answers

How to set Node engine to 8.x or 10.x in package.json?

To set Node engine to 8.x or 10.x in package.json, we set the engines property.

For instance, in package.json, we write

{
  "engines": {
    "node": "^8 || ^10"
  }
}

to add the engines property into the JSON.

We set the Node engine to 8.x or 10.x with ^8 || ^10.

Categories
JavaScript Answers

How to fix Webpack: Bundle.js – Uncaught ReferenceError: process is not defined error with JavaScript?

To fix Webpack: Bundle.js – Uncaught ReferenceError: process is not defined error with JavaScript, we add the process/browser plugin into the Webpack config.

To do this, we write

const webpack = require("webpack");

module.exports = {
  //...
  plugins: [
    new webpack.ProvidePlugin({
      process: "process/browser",
    }),
  ],
};

to add the process/browser plugin with the ProvidePlugin constructor in webpack.config.js.

We also need to require webpack with require.

Categories
JavaScript Answers

How to disable source maps for React application?

To disable source maps for React application, we set the GENERATE_SOURCEMAP environment variable to false.

For instance, in package.json, we write

{
  "scripts": {
    "start": "react-scripts start",
    "build": "GENERATE_SOURCEMAP=false react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"
  }
}

to set the GENERATE_SOURCEMAP environment variable to false before running react-scripts build to disable source map generation in the build script.

Categories
JavaScript Answers

How to get JavaScript Date in UTC with Moment.js?

To get JavaScript Date in UTC with Moment.js, we use the date getTime method.

For instance, we write

const t = new Date().getTime();

to create a new date with the current date and time with the Date constructor.

Then we call getTime to return its timestamp in milliseconds.