Error: Cannot find module 'pg', after running npm install pg - node.js

Using VSCode. Installed latest Node.js (first time user) for Windows, and the desired modules, one of which was 'pg' (npm install pg)
At the top of my script I have this:
const pg = require('pg');
When i attempt to debug, I get this error when it hits that line:
Exception has occurred: Error
Error: Cannot find module 'pg'
at Function.Module._resolveFilename (module.js:547:15)
at Function.Module._load (module.js:474:25)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (c:\EBI\Work\Node-pg-log\get_pg_log.js:3:12)
at Module._compile (module.js:649:14)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
Is there something else I need to install or set up? Debugging is in place.

This usually happens when node_modules/ folder is located in some sub-directory. Like
/app
----index.js
----/libs
--------/node_modules/
You want a directory structure like
/app
----index.js
----node_modules/
Where node_modules folder is either in the current directory or in some upper level directory

If you are using TypeScript it is necessary to install pg types. Use the command
npm install --save-dev #types/pg

Related

Node : Cannot find module common

I was running a node server w/o any issue and for some purpose had to remove the node_modules dir and re-install the dependencies. Upon doing so and running the server again I get this error :
Error: Cannot find module './common'
at Function.Module._resolveFilename (module.js:547:15)
at Function.Module._load (module.js:474:25)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (C:\Work\Cinde-Node\node_modules\debug\src\node.js:236:18)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
I installed common separately using npm install common but that didnt solve the problem.
How do I fix this ?
It looks like you have a line like
require('./common');
it should be
require('common')
I was getting the same error when I tried running my express project. The following command helped me to fix the issue.
rm package-lock.json && rm -r node_modules && npm i --save --legacy-peer-deps

Gulp watch not working - cannot find module connect

Trying to run my project using gulp (havent used gulp before and have inherited an exisiting project).
I install the dependencies into the wordpress theme directory.
Running gulp watch throws this error:
Error: Cannot find module 'connect'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (C:\dev\projects\romac-website\web\app\themes\romac\node_modules\browser-sync\lib\utils.js:307:31)
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)
I have tried to update npm but not sure if it is fine or the problem (still).
I also tried to ins install this module with npm -g install connect
What should I try next?
Try deleting the node-modules folder then reinstall npm install connect --save-dev Then retry running gulp

nodejs - can't find module Backbone

var Backbone = require('backbone');
causes this error:
module.js:340
throw err;
^
Error: Cannot find module 'backbone'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (C:\Users\denman\workspace-nodejs\AFirstServer_NodeUpAndRunning\hello-world-server.js:6:16)
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)
how can this be?
I installed Backbone via npm install -g backbone
I even restarted Eclipse and my machine.
By default, node will not load modules that are installed globally. You should be performing any npm install-s in your project's directory root, not installing them globally.
Try this instead. Create your new project directory, change to it, and then:
npm init #this will create a package.json for your project
npm install --save backbone # this will install backbone to the directory, and save it into your package.json as a dependency
Then create a file in that directory called index.js. Put your code in there requiring backbone. Then from within that directory run node index.js, and you'll find that everything works, and backbone is available.
Here is a good blog post on the subject.

coreMIDI and NodeJS

I've installed coreMIDI following this:
sudo npm install -g coremidi
The installation was successful, but I don't understand how to use it using node.js
This is the first line of my code:
var coremidi = require('coremidi')();
But the output is:
Error: Cannot find module 'coremidi'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (/Users/[NAME]/Desktop/nodeMidi/coremidi/test1.js:1:78)
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)
The require statement in node looks for a folder named node_modules in the current folder and if that exists looks in there for a folder named after your package. If it isn't found, it goes one directory higher and tries that again. It'll keep doing this until it reached the root folder of your file system.
The reason it can't find coremidi is because coremidi has been installed in the global node_modules folder since you used the -g flag. This is OS/node install dependent, but usually something like /usr/lib/local/share/npm/ or whatever. Since this path
You shouldn't use -g to install npm modules unless they're specifically meant to be installed globally (usually that means that include a script for you to run, like NPM itself, or jshint, or mocha, or tap, or etc...)

node.js returns "Cannot find module 'lockit-mongodb-adapter'"

I am going through my first node.js project. I've installed lockit , have a app.js file, and when I try to run it I get this error
module.js:340
throw err;
^ Error: Cannot find module 'lockit-mongodb-adapter'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at new module.exports (C:\Users\xx\Documents\mailer\index.js:38:44)
at Object.<anonymous> (C:\Users\xx\Documents\mailer\examples\mongodb\app. js:32:14)
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)
I am quite certain I have lockit-mongodb-adapter installed.
You need to install the module. Cd to the root of your app directory and run this command and make sure lockit-mongodb-adapter is in your node modules directory.
npm install lockit-mongodb-adapter
Another thing you can do if you are certain it is installed is double check to see what is installed by running this command:
npm list
And this one to see what is installed globally:
npm list -g
Update:
I was able to reproduce the issue you where having. And to resolve it I deleted the module from the node moules directory. And re-installed with the following command:
npm install lockit-mongodb-adapter -save
It now shows up in npm list with no issues:

Resources