How to fix 'Error: Cannot find module 'supertest'' - node.js

I am new to nodejs. I got one nodejs application and i was just trying to run the tests this app contains. So i tried to run npm test command after installing all packages using npm install. But npm test is throwing below error always
internal/modules/cjs/loader.js:657
throw err;
^
Error: Cannot find module 'supertest'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:655:15)
at Function.Module._load (internal/modules/cjs/loader.js:580:25)
at Module.require (internal/modules/cjs/loader.js:711:19)
at require (internal/modules/cjs/helpers.js:14:16)
I tried to uninstall and install supertest but it did not help. I see there is no node_modules folder inside supertest. Would that be an issue? How do we fix it?
Here is the file which is using supertest
var fs = require('fs')
var request = require('supertest')
var config = require('./config').getConfig()
var url = config.url
var caCert
if (config.caCertFile) {
caCert = fs.readFileSync(config.caCertFile)
}
var preparedRequest = function () {
return caCert ? request.agent(url, { ca: caCert }) : request(url)
}
module.exports = preparedRequest

It seems that supertest is not in your package.json depencendies. So If you run npm install, it will not install supertest.
Just run npm install supertest to install supertest, or npm install --save supertest to install it and add the dependency in your package.json so the next time you run npm install it will install supertest too :)

Related

Cannot find module 'dotenv/types'

Im using next.js and trying to set an environment variable - I have installed dotenv ofcourse by im being told that the module cannot be found and have no earthly idea as to why. Why might something like this be happening?
The dotenv is in my dependencies, But when i run dotenv --version i get told 'bash command not found'
I should say that the below code is probably not relevant to the problem.
in my .env file at the root of the doc i have
API_URL = http://localhost:1337
Then in my config file i have
const path = require('path');
require('dotenv').config();
module.exports = {
env: {
API_URL: process.env.API_URL,
},
webpack: (config) => {
config.resolve.alias['components'] = path.join(_dirname, 'components');
config.resolve.alias['public'] = path.join(_dirname, 'public');
return config;
},
};
Im getting this on the the console:
Loaded env from C:\Users\sidtu\Documents\Websites\next-example\.env
Cannot find module 'dotenv/types'
code: 'MODULE_NOT_FOUND',
"The dotenv is in my dependencies, But when i run dotenv --version i get told 'bash command not found'" - That is typical behaviour for a npm package, only packages that are installed globally should be accessible from the command line.
If you are just using dotenv in weback, use dotenv-webpack instead.
If you are having trouble getting .env files to work with React see this blog
Use npm to install dotenv-types
$ npm i -g dotenv-types
Usage:
$ dotenv-types -h
Source: https://www.npmjs.com/package/dotenv-types

OKTA express middleware - TypeError [ERR_INVALID_ARG_TYPE]: The "original" argument must be of type Function. Received type undefined

Just including the following in an app causes the following error.
const { ExpressOIDC } = require('#okta/oidc-middleware');
Full error:
$ node app.js
internal/util.js:257
throw new ERR_INVALID_ARG_TYPE('original', 'Function', original);
^
TypeError [ERR_INVALID_ARG_TYPE]: The "original" argument must be of type Function. Received type undefined
at promisify (internal/util.js:257:11)
at Object.<anonymous> (/dev/tmpOidc/node_modules/jose/lib/jwk/key/rsa.js:13:25)
at Module._compile (internal/modules/cjs/loader.js:689:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:20:18)
at Object.<anonymous> (/dev/tmpOidc/node_modules/jose/lib/jwk/import.js:9:16)
Steps to reproduce
1. Create a new node app and add express and Okta's oidc middleware
npm init
npm install express --save
npm install --save #okta/oidc-middleware
Create app.js
const express = require('express')
const app = express()
const port = 3000
// const { ExpressOIDC } = require('#okta/oidc-middleware');
app.get('/', (req, res) => res.send('Hello World!'))
app.listen(port, () => console.log(`Example app listening at http://localhost:${port}`))
Run it
node app.js
See how it runs. Finally, uncomment and include the line that sets up the OIDC middleware.
const { ExpressOIDC } = require('#okta/oidc-middleware');
Run the app again and I get the error above shows up.
Solved: Need to run with node v12 not v10
I just published a YouTube video that shows you how to use Okta's OIDC Middleware.
You can find the steps I used here. To summarize:
Create a new project with express-generator and pug: npx express-generator --view=pug
Add a new web app on Okta with http://localhost:3000/callback as the login redirect URI
Install Schematics CLI: npm install -g #angular-devkit/schematics-cli
Install and run OktaDev Schematics with your Okta values:
npm i #oktadev/schematics
schematics #oktadev/schematics:add-auth --issuer=$issuer --clientId=$clientId --
clientSecret=$clientSecret
Start your app and authenticate with Okta: npm start
You can see the result of these steps in this GitHub repo.
Solved. When I posted my question I should've included system information such as the version of Node. I had v10.8.0. Once I updated to v12 the typeerror went away.

NPM and NodeJS Compatibility: NodeJS works from PM prompt, but not script

I am attempting to get a lighthouse script running in Node.JS (which I am new to). I followed the intial instructions here https://github.com/GoogleChrome/lighthouse/blob/master/docs/readme.md#using-programmatically. I was able to complete the prior steps in the package manager console (Visual Studio 2017):
npm install -g lighthouse
lighthouse https://airhorner.com/
//and
lighthouse https://airhorner.com/ --output=json --output-path=./report/test1.json
However, I do get an initial warning that NPM only supports Node.JS in versions 4 through 8 and recommends a newer version. The problem is I am running Node v12 and NPM v5 - both the latest.
When I create a script version like below (app.js)
const lighthouse = require('lighthouse');
const chromeLauncher = require('chrome-launcher');
const config = {
extends: 'lighthouse:default',
settings: {
emulatedFormFactor: 'desktop',
onlyCategories: 'performance',
output: 'json',
outputPath: './report.json'
}
};
function launchChromeAndRunLighthouse(url, opts = null, config) {
return chromeLauncher.launch().then(chrome => {
opts.port = chrome.port;
return lighthouse(url, opts, config).then(results => {
return chrome.kill().then(() => results.lhr);
});
});
}
// Usage:
launchChromeAndRunLighthouse('https://airhorner.com/', config).then(results => {
// Use results!
});
And run the command
C:\src\project> node app.js
I get the error - Cannot find module 'lighthouse'
don't install lighthouse locally use it inside your working dir .
first start by running npm init that will create the package.json file inside the current working dir
then npm install --save lighthouse will download it and save it to node_modules now you can use it locally inside your working dir
it should look something like this
app.js
package.json
node_modules/
then run node app.js

nodejs connect cannot find static

NOTE: I have tried other solution given here but it didn't work
A newbie with NodeJs. I am trying to follow AngularJS pro and got stuck with setting up NodeJs server. According to book, I installed nodejs and then installed connect package using
npm install connect
then downloaded angularjs in folder next to nodejs folder. Then wrote server.js file to connect to server. Here is the content of the file:
var connect = require('connect');
connect.createServer(connect.static("../angularjs")).listen( 5000);
When I run this server.js file using:
node server.js
I get following error:
function app(req, res, next){ app.handle(req, res, next); }
merge(app, proto);
merge(app, EventEmitter.prototype);
app.route = '/';
app.stack = [];
return app;
has no method 'static'
at Object.<anonymous> (C:\web\nodejs\server.js:2:36)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:906:3
Any ideas guys?
Thanks.
The connect package has made some changes in the latest 3.x version of their code base, moving the static middleware to it's own package. You can view the list of packages that have been moved here.
So you have two options:
Option 1
You can install an older, 2.x version of connect and use that as is:
$ npm install connect#2.X.X
Installing the latest 2.X.X version will allow your current implementation to function properly.
Option 2
You can continue to use the 3.x version of connect, and also add serve-static:
$ npm install serve-static
You would also have to update your server.js file to include the new serve-static module:
var connect = require('connect'),
serveStatic = require('serve-static');
var app = connect();
app.use(serveStatic("../angularjs"));
app.listen(5000);
The answer by dylants is helpful. However, here are the exact steps that I followed in order to resolve the same error.
1. From the command window , change directory to the directory in which you installed nodeJS.
2. After having already ran npm install connect, run:
npm install serve-static
3. Create a file named server.js with the following code:
var connect = require('connect'),
serveStatic = require('serve-static');
var app = connect();
app.use(serveStatic("./angularjs"));
app.listen(5000);
While still in command window, and still in the directory in which you have installed nodeJS, run:
node server.js
Navigate to the URL
http://localhost:5000/test.html
This should work. Here is my directory configuration:
C:\NodeJSInstallLocation\angularjs
var connect = require('connect'),
serveStatic = require('serve-static');
connect().use(
serveStatic("angularjs")
).listen(5000);
You might want to try something like this
var express = require('express');
var app = express();
app.use(express.static('angularjs'));
Try this...
const express = require('express');
const app = express();
const path = require('path');
app.use('',express.static(path.join(__dirname, '/static/')));

Cannot find module 'connect' on Mac OS X Lion

I installed connect module using NPM running the following command:
npm install connect
it created the module in /Download/usr/node_modules/connect folder. I created a file which
uses connect module using
var connect = require('connect');
var util = require('util');
function sendjson(res,obj)
{
res.writeHead(200,{'Content-Type':'application/json',});
var objstr = JSON.stringify(obj);
util.debug('SENDJSON' + objstr);
res.end(objstr);
}
var server = connect.createServer(
connect.router(function(app){
app.get('/foo', function(req,res){
sendjson(res,{path:'foo'});
})
app.get('/bar', function(req,res){
sendjson(res,{path:'bar'});
})
})
);
server.listen(3000);
I run node createServer.js and it throws in the terminal and it gives me the following error.
Cannot find module 'connect'
NPM modules by default need to be installed locally in the folder that contains the source file that uses them. So if your source file is in /Desktop/nodescripts, you should run "npm install connect" in that same folder. That will create node_modules folder in that path, and your script will be able to find it.

Resources