Error: Cannot find module 'pg', after running npm install pg
Asked Answered
C

3

8

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.

Canned answered 25/5, 2018 at 19:23 Comment(3)
Is it only when debugging is in place, and only that module? Just trying to get a sense of the situation.Mongeau
Happens in regular run mode as well. And it seems to be all modules I'm trying to use.Canned
do you have node_modules folder in your project root?Wainscoting
A
7

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

Aura answered 25/5, 2018 at 19:46 Comment(0)
R
3

If you are using TypeScript it is necessary to install pg types. Use the command

npm install --save-dev @types/pg
Retarder answered 8/9, 2022 at 11:54 Comment(1)
its not about pg type, this commands help me "npm install pg --save"Brigand
C
0

We had the same issue with Strapi 4, we fixed it by installing pg:

If you get a “cannot find module ‘pg’” error, install it by running yarn add pg or npm install --save pg

As written here: https://strapi.io/blog/postgre-sql-and-strapi-setup

Churchwell answered 28/3 at 14:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.