NodeJS: 'MODULE_NOT_FOUND' after installing package

337 Views Asked by At

Here is a simple code that I want to run as a NodeJS script:

const fs = require('fs');
const { Test } = require('artillery');

const options = {
  'key': fs.readFileSync('./key.pem'),
  cert: fs.readFileSync('./certificates.pem'),
  ca: fs.readFileSync('./ca.pem')
};

const test = new Test(config, options);

test.run((err, results) => {
  if (err) {
      console.error(err);
  } else {
      console.log(results);
  }
});

When I run it with:

node myscript.js

I receive an error Error: Cannot find module 'CL\path\to\node_modules\artillery\lib'. Please verify that the package.json has a valid "main" entry.

I tried to delete node_modules and package_locks.json, and then reinstall everything in package.json with npm install. I also deleted npm cache, removed and then reinstalled the Artillery package. nmp, nodejs and Artillery are all the latest versions.

My package.json is:

{
  "name": "helloworld",
  "version": "0.0.0",
  "private": true,
  "main": "./bin/www"
  "scripts": {
    "start": "node ./bin/www"
  },
  "dependencies": {
    "artillery": "^2.0.0-27",
    "artillery-plugin-http-ssl-auth": "^0.0.1",
    "axios": "^1.2.2",
    "cookie-parser": "~1.4.4",
    "date-fns": "^2.29.3",
    "debug": "^4.3.4",
    "express": "^4.18.2",
    "http-errors": "~1.6.3",
    "https": "^1.0.0",
    "jshint": "^2.13.6",
    "morgan": "^1.3.2",
    "pug": "^2.0.4"
  },
  "devDependencies": {
    "@types/artillery": "^1.7.0"
  }
}

Thank you in advance!!

1

There are 1 best solutions below

1
On

As the error says, you're missing a main entry in your package.json. You can use ./bin/www for it:

"main": "./bin/www"