Categories
JavaScript Answers

How to install and run Mocha, the Node.js testing module?

To install and run Mocha, the Node.js testing module, we install the mocha package globally.

To install it, we run

npm install --global mocha

to install mocha globally.

We can also install it in the project folder as a dev dependency with

npm install --save-dev mocha

Then in package.json, we add

{
  //...
  "scripts": {
    "test": "mocha"
  }
  //...
}

to add the test script to run mocha installed in the node_modules folder.

Categories
JavaScript Answers

How to fix ‘ng: command not found’ error with Angular?

To fix ‘ng: command not found’ error with Angular, we install the @angular/cli package.

To install it, we run

npm i -g @angular/cli
Categories
JavaScript Answers

How to fix Npm Please try using this command again as root/administrator error with JavaScript?

To fix Npm Please try using this command again as root/administrator error with JavaScript, we run npm as an administrator.

To do this, we:

  1. Click Start, click All Programs, and then click Accessories.
  2. Right-click Command prompt, and then click Run as administrator.

Then we run the npm command in the administrator command prompt.

Categories
JavaScript Answers

How to install a specific version of Node on Ubuntu?

To install a specific version of Node on Ubuntu, we use the n module.

For instance, we run

sudo npm cache clean -f
sudo npm install -g n
sudo n stable

to clear the npm cache with

sudo npm cache clean -f

Then we install the n module globally with

sudo npm install -g n

We run n with

sudo n stable

Then we install Node 16.15.1 with

sudo n 16.15.1
Categories
JavaScript Answers

How to fix Webpack 4 “size exceeds the recommended limit (244 KiB)” error with JavaScript?

To fix Webpack 4 "size exceeds the recommended limit (244 KiB)" error with JavaScript, we set devtool to false.

For instance, in webpack.config.js, we write

const mode = process.env.NODE_ENV || "development";
module.exports = {
  //...
  devtool: mode === "development" ? "inline-source-map" : false,
  mode,
  //...
};

to set devtool to false is mode isn’t 'development'.