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.

Categories
JavaScript Answers

How to ungzip (decompress) a Node.js request’s module gzip response body?

To ungzip (decompress) a Node.js request’s module gzip response body, we use the zlib library.

For instance, we write

const request = require("request");
const zlib = require("zlib");
const concat = require("concat-stream");

request(url)
  .pipe(zlib.createGunzip())
  .pipe(
    concat((stringBuffer) => {
      console.log(stringBuffer.toString());
    })
  );

to call request to make a get request to the file url.

Then we call pipe with the stream returned by zlib.createGunzip() to unzip the file.

And we call pipe again with the stream returned by the concat function to get the unzipped data as a buffer from stringBuffer.

Categories
JavaScript Answers

How to use docker run command to pass arguments to CMD in Dockerfile?

To use docker run command to pass arguments to CMD in Dockerfile, we run CMD with an array.

For instance, we write

CMD ["sh", "-c", "node server.js ${cluster} ${environment}"]

in our Dockerfile to run the sh command with the arguments after it.

cluster and environment are the environment variables we defined in the docket-compose.yml file like

node:
  environment:
    - environment=dev
    - cluster=0