Categories
JavaScript Answers

How to fix Babel unexpected token import when running mocha tests with JavaScript?

To fix Babel unexpected token import when running mocha tests with JavaScript, we add the @babel/preset-env library.

To install it, we run

npm install @babel/preset-env --save-dev

Then in the .babelrc file, we add

{
  "presets": ["@babel/preset-env"]
}

to add the Babel preset to clear the syntax error.

Categories
JavaScript Answers

How to fix Node js Error: Protocol “https:” not supported. Expected “http:”?

To fix Node js Error: Protocol "https:" not supported. Expected "http:", we use https.get method.

For instance, we write

const http = require("http");
const https = require("https");
const url = new URL("https://www.example.com");
const client = url.protocol == "https:" ? https : client;
const req = client.get(url, (res) => {
  console.log(res);
});

to get the client according to the url‘s protocol.

We use the https client if it’s https and http if it’s http.

Then we call get with the client for the right protocol to make the get request.

Categories
JavaScript Answers

How to run npm http-server with SSL?

To run npm http-server with SSL, we create a certificate and use that to run the server.

To do this, we run

brew install mkcert
brew install nss

to install mkcert

Then we go to our project directory and run

mkcert 0.0.0.0 localhost 127.0.0.1 ::1

to create a certificate.

Then we run the server with our certificate with

http-server -S -C cert.pem -o
Categories
Vue Answers

How to fix Vue js error: Component template should contain exactly one root element?

To fix Vue js error: Component template should contain exactly one root element, we should wrap our elements with one root element in our template.

For instance, we write

<div>
  <div class="form-group">...</div>

  <div class="col-md-6">...</div>
</div>

to wrap a div around the other divs to clear the error.

Categories
JavaScript Answers

How to fix ‘TypeError: is not a function’ error in Node.js?

To fix ‘TypeError: is not a function’ error in Node.js, we should make sure the variable is a function.

For instance, we write

module.exports = functionName

to export the functionName function in function.js.

Then we require it with

const functionName = require('./function')

Now we can call functionName in our code.