“Error: Please install pg package manually” when trying to run “npm run serve:dev”?
Asked Answered
M

7

29

I'm trying to run an app with script npm run serve:dev but it gives an error Error: Please install pg package manually when trying to run npm run serve:dev

I already tried npm install -g pg','npm install -g pg-hstore

ERROR:

[email protected] serve:dev /home/qroach/kshitij-mag nodemon --ignore './src/' --exec babel-node --presets babel-preset-env ./server/bin/www

[nodemon] 1.18.10 [nodemon] to restart at any time, enter rs [nodemon] watching: . [nodemon] starting babel-node --presets babel-preset-env ./server/bin/www /home/qroach/node_modules/sequelize/lib/dialects/abstract/connection-manager.js:81 throw new Error(Please install ${moduleName} package manually); ^

Error: Please install pg package manually at ConnectionManager._loadDialectModule (/home/qroach/node_modules/sequelize/lib/dialects/abstract/connection-manager.js:81:15) at new ConnectionManager (/home/qroach/node_modules/sequelize/lib/dialects/postgres/connection-manager.js:18:24) at new PostgresDialect (/home/qroach/node_modules/sequelize/lib/dialects/postgres/index.js:14:30) at new Sequelize (/home/qroach/node_modules/sequelize/lib/sequelize.js:241:20) at Object. (/home/qroach/kshitij-mag/server/db/models/index.js:16:15) at Module._compile (internal/modules/cjs/loader.js:799:30) at loader (/usr/lib/node_modules/babel-cli/node_modules/babel-register/lib/node.js:144:5) at Object.require.extensions.(anonymous function) [as .js] (/usr/lib/node_modules/babel-cli/node_modules/babel-register/lib/node.js:154:7) at Module.load (internal/modules/cjs/loader.js:666:32) at tryModuleLoad (internal/modules/cjs/loader.js:606:12) at Function.Module._load (internal/modules/cjs/loader.js:598:3) at Module.require (internal/modules/cjs/loader.js:705:19) at require (internal/modules/cjs/helpers.js:14:16) at Object. (/home/qroach/kshitij-mag/server/controllers/AuthController.js:2:1) at Module._compile (internal/modules/cjs/loader.js:799:30) at loader (/usr/lib/node_modules/babel-cli/node_modules/babel-register/lib/node.js:144:5) at Object.require.extensions.(anonymous function) [as .js] (/usr/lib/node_modules/babel-cli/node_modules/babel-register/lib/node.js:154:7) at Module.load (internal/modules/cjs/loader.js:666:32) at tryModuleLoad (internal/modules/cjs/loader.js:606:12) at Function.Module._load (internal/modules/cjs/loader.js:598:3) at Module.require (internal/modules/cjs/loader.js:705:19) at require (internal/modules/cjs/helpers.js:14:16) [nodemon] app crashed - waiting for file changes before starting...

I expect it to run on using the script, but it just gives this error.

Marxmarxian answered 25/3, 2019 at 15:7 Comment(0)
V
32

Just install locally

npm install pg --save

Voiceful answered 9/5, 2019 at 14:4 Comment(2)
npm install -g pg --save Installed the pg package globally and it worked!Hydromedusa
why isn't sequelize require pg as a dependency?Speer
P
20

I was also facing same issue. Try connecting your db like this:

import pg from 'pg';
import { Sequelize } from 'sequelize';

const sequelize = new Sequelize('postgres://admin:admin@localhost:5432/mydb', {
  dialect: 'postgres',
  dialectModule: pg
});

This worked for me. More info here

Pusillanimity answered 4/2, 2022 at 7:12 Comment(2)
Is it really necessary to import * as pg from 'pg' ? I believe it's better to just import pg from 'pg'Zebapda
@AndreGoulart is correct. import * as pg from 'pg' will actually cause downstream failures in Sequelize with the error this.lib.Client is not a constructor. Use import pg from 'pg'.Barbarous
D
4
//in your sequelize object initialization do this

import pg from 'pg';

{
   // your sequelize config
   dialectModule: pg
   ...
}
Dinkins answered 25/9, 2022 at 11:1 Comment(2)
Your answer could be improved by adding more information on what the code does and how it helps the OP.Geomorphology
If you use the above code, sequelize will use js bindings and not load the native bindings.Supranational
E
3

Install it globally using the below command:

npm install -g pg --save

Installing locally did not worked for me, make sure to have -g flag

Entrust answered 13/7, 2023 at 9:51 Comment(0)
B
1

It worked for me when I run it from local node modules

./node_modules/.bin/sequelize db:migrate

Betsey answered 10/12, 2021 at 11:52 Comment(0)
L
0

Also make sure that sequelize-cli is installed when running npx sequelize-cli

I was getting the problem only in Heroku. After staring at it for long enough, I finally understood the cause: I had sequelize-cli on devDependencies and Heroku must have been pruning them. Then when I ran npx sequelize-cli it was installing sequelize-cli every time to some global location. But from there, pg was not present. Moving sequelize-cli to dependencies solved it.

And BTW: always do npx --no-install. On the fly install is insane, especially with peer dependencies like this as I've learnt.

Langelo answered 18/3, 2022 at 14:41 Comment(0)
T
-4

Try to delete your node_modules and install them again:

npm install

or

yarn
Tompion answered 15/6, 2020 at 6:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.