Categories
JavaScript Answers

How to fix the “Cannot find module ‘@babel/core'” with JavaScript?

To fix the "Cannot find module ‘@babel/core’" with JavaScript, we install the @babel/core package.

For instance, we run

npm install @babel/core --save

to install the @babel/core package with npm and save it into package.json.

Categories
JavaScript Answers

How to fix “cannot find module ‘request'” error with Node.js?

To fix "cannot find module ‘request’" error with Node.js, we install the request package.

To install it, we run

npm install request --save

Then we can use it with

const request = require("request");

request("http://www.example.com", (error, response, body) => {
  if (!error && response.statusCode === 200) {
    console.log(body);
  }
});

We call request to make a get request to http://www.example.com

And get the response with response from the callback.

Categories
JavaScript Answers

How to fix Babel 7 – ReferenceError: regeneratorRuntime is not defined with JavaScript?

To fix Babel 7 – ReferenceError: regeneratorRuntime is not defined with JavaScript, we install a few packages and then import them into our project.

We install the missing packages with

npm install --save core-js
npm install --save regenerator-runtime    

And then import them with

import "core-js/stable";
import "regenerator-runtime/runtime";

to let us use Babel in our project.

Categories
JavaScript Answers

How to specify the path of package.jso to npm with JavaScript?

To specify the path of package.jso to npm with JavaScript, we set the --prefix option.

For instance, we run

npm --prefix /path/to/project run build

to set the --prefix option to the path of package.json we want npm to read from when running the build command.

Categories
JavaScript Answers

How to use specific middleware in Express for all paths except a specific one with Node.js?

To use specific middleware in Express for all paths except a specific one with Node.js, we can check the path of the request in the middleware function.

For instance, we write

const checkUser = (req, res, next) => {
  if (req.path == "/") {
    return next();
  }
  next();
};

app.all("*", checkUser);

to call app.all to add the checkUser middleware for all routes.

In checkUser, we check the path of the request with req.path.

If it’s '/', we call the next middleware with next and return to end the execution of checkUser.