Categories
JavaScript Answers

How to set max viewport in Node Puppeteer?

To set max viewport in Node Puppeteer, we set the --window-size option.

For instance, we write

const browser = await puppeteer.launch({
  headless: false,
  args: ["--window-size=1920,1080"],
  defaultViewport: null,
});

to call launch to launch the browser.

We set args to ["--window-size=1920,1080"] to make the window size 1920×1080.

Categories
JavaScript Answers

How to use ES6 modules in Node 12?

To use ES6 modules in Node 12, we set the --experimental-modules option.

For instance, we write

{
  "scripts": {
    "start": "node --experimental-modules src/index.mjs "
  }
}

in package.json to add the start script.

We use it to run node with the --experimental-modules on to enable ES6 modules support.

Categories
JavaScript Answers

How to fix Uncaught Error: Cannot find module ‘jquery’ with Node Electron app?

To fix Uncaught Error: Cannot find module ‘jquery’ with Node Electron app, we need to add the jQuery script into our HTML file.

For instance, we write

<!DOCTYPE html>
<html>
  <head></head>

  <body>
    <h1>Hello World!</h1>
  </body>

  <script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>

  <script>
    window.jQuery = window.$ = jQuery;

    $(document).ready(function () {
      console.log("jQuery is loaded");
    });
  </script>
</html>

to add the jQuery script with

<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
Categories
JavaScript Answers

How to use a self signed certificate for a HTTPS Node.js server?

To use a self signed certificate for a HTTPS Node.js server, we set a few options.

For instance, we write

const options = {
  host: "localhost",
  port: 8000,
  path: "/api/v1/test",
  rejectUnauthorized: false,
  requestCert: true,
  agent: false,
};

https
  .createServer(options, (req, res) => {
    res.writeHead(200);
    res.end("OK\n");
  })
  .listen(8000);

to set rejectUnauthorized to false, requestCert to true, and agent to false to allow self signed certificates to be used on the https server.

Categories
JavaScript Answers

How to create line breaks in console.log() in Node.js?

To create line breaks in console.log() in Node.js, we log '\n'.

For instance, we write

console.log({ a: 1 }, "\n", { b: 3 }, "\n", { c: 3 });

to log line breaks with '\n'.