andywer/pg-listen

pg Dependency missing

LukasLoeffler opened this issue · 4 comments

While setting up an empty project with pg-listen to do some testing I ran into an issue.

After installing pg listen via npm i pg-listen and starting a script with import createSubscriber from "pg-listen", node throws the folloing error:

internal/modules/cjs/loader.js:889
  const err = new Error(message); 
              ^

Error: Cannot find module 'pg'
Require stack:
- E:\Coding\pg-listener\node_modules\pg-listen\dist\index.js
- E:\Coding\pg-listener\node_modules\pg-listen\index.js
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:889:15)
    at Function.Module._load (internal/modules/cjs/loader.js:745:27)
    at Module.require (internal/modules/cjs/loader.js:961:19)
    at require (internal/modules/cjs/helpers.js:92:18)
    at Object.<anonymous> (E:\Coding\pg-listener\node_modules\pg-listen\dist\index.js:56:10)
    at Module._compile (internal/modules/cjs/loader.js:1072:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10)
    at Module.load (internal/modules/cjs/loader.js:937:32)
    at Function.Module._load (internal/modules/cjs/loader.js:778:12)
    at Module.require (internal/modules/cjs/loader.js:961:19) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    'E:\\Coding\\pg-listener\\node_modules\\pg-listen\\dist\\index.js',
    'E:\\Coding\\pg-listener\\node_modules\\pg-listen\\index.js'
  ]
}

After installing the package "pg" manually, the script started successfully.

Having a look into the package.json of pg-listen, "pg" only appears as peerDependency and devDependency.

Hey Lukas. As you already pointed out, the dependency is not missing, it's a peer dependency.

The reason behind it is that you shall be able to use the pg version that you need and pg-listen will use what's there. The alternative would be that pg becomes a normal dependency, but then either your entire app will have to stick to the pg version that pg-listen requires (likely a deal-breaker for many projects) or you will end up with multiple pg instances in your dependency tree, each one in a different version, which can also lead to all kinds of subtle issues.

It might make sense to change the readme, though:

- npm install pg-listen
+ npm install pg pg-listen

What do you think?

Hey Andy,
thanks for clarification.
I guess adding this to the readme will do the trick.
However, I would add an additional note that states the exact circumstances to prevent unnecessary pg installs.

Is it really that any version of pg-listen works with any version of pg?
If not, then a compatibility matrix could be useful in the readme...