node-pg-migrate: yarn PnP support not working

Describe the bug

I upgraded to v8.0.0 from v7.5.0. When I try to run node-pg-migrate I get the following error:

Error: node-pg-migrate tried to access dotenv, but it isn't declared in its dependencies; this makes the require call ambiguous and unsound.

Required package: dotenv (via "dotenv/package.json")
Required by: node-pg-migrate@virtual:e298dc21ee497008eb9a0ebbe4a77b90443e0bab900aeeee3d636fad7ae3e60b61bd41079ed9d02f79f9898511e9fbf35388a339162608abfefebd9574b5c251#npm:8.0.0 (via REDACTED/.yarn/__virtual__/node-pg-migrate-virtual-fb0f6e4d60/0/cache/node-pg-migrate-npm-8.0.0-609b1b273d-2e9b854683.zip/node_modules/node-pg-migrate/bin/node-pg-migrate.js)

    at makeError (REDACTED/.pnp.cjs:38584:34)
    at resolveToUnqualified (REDACTED/.pnp.cjs:40224:21)
    at Object.resolveToUnqualified (REDACTED/.pnp.cjs:40404:26)
    at resolve$1 (file://REDACTED/.pnp.loader.mjs:2031:31)
    at nextResolve (node:internal/modules/esm/hooks:866:28)
    at Hooks.resolve (node:internal/modules/esm/hooks:304:30)
    at MessagePort.handleMessage (node:internal/modules/esm/worker:196:24)
    at [nodejs.internal.kHybridDispatch] (node:internal/event_target:825:20)
    at MessagePort.<anonymous> (node:internal/per_context/messageport:23:28)

To get around this (and other similar errors for other dependencies), I had to add to my .yarnrc.yml file as follows:

packageExtensions:
  node-pg-migrate@*:
    dependencies:
      dotenv: '*'
      config: '*'
      'dotenv-expand': '*'

Steps to reproduce

  1. Start a new project using yarn PnP.
  2. Install node-pg-migrate.
  3. Try to run it.

Logs

No response

System Info

System:
    OS: Linux 6.8 Ubuntu 22.04.5 LTS 22.04.5 LTS (Jammy Jellyfish)
    CPU: (14) x64 Intel(R) Core(TM) Ultra 7 165U
    Memory: 19.30 GB / 30.80 GB
    Container: Yes
    Shell: 5.1.16 - /bin/bash
  Binaries:
    Node: 20.13.1 - ~/.nvm/versions/node/v20.13.1/bin/node
    Yarn: 4.7.0 - ~/.nvm/versions/node/v20.13.1/bin/yarn
    npm: 10.5.2 - ~/.nvm/versions/node/v20.13.1/bin/npm

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 18 (13 by maintainers)

Most upvoted comments

I think the problem might be in how these are handled.

The code looks for ERR_MODULE_NOT_FOUND, but yarn is throwing an error with a code set to MODULE_NOT_FOUND.

I have no clue which way is canonical, but if the error was caught as defined by yarn, then it probably wouldn’t cause the issue anymore.

Ok, I have reproduced the issue at https://github.com/brenoepics/yarn-pgm-poc

Error: node-pg-migrate tried to access dotenv, but it isn't declared in its dependencies; this makes the require call ambiguous and unsound.
Error: node-pg-migrate tried to access dotenv-expand, but it isn't declared in its dependencies; this makes the require call ambiguous and unsound.
Error: node-pg-migrate tried to access config, but it isn't declared in its dependencies; this makes the require call ambiguous and unsound.

Yarn Berry (Yarn 2+) enforces strict dependency declarations. If node-pg-migrate uses packages like config, dotenv, or dotenv-expand but doesn’t declare them as dependencies, users must add them manually to their own package.json.

You can fix it by adding the required packages to your dependencies:

yarn add config dotenv dotenv-expand

https://yarnpkg.com/features/pnp https://yarnpkg.com/features/pnp#how-can-i-fix-ghost-dependencies

Maybe it would help Yarn 2+ users if we either list them as dependencies, document or add to the known ghost dependencies list?

EDIT:

It looks like bun also got issues with this, while looking at #1397 I tried

bunx --bun migrate up
$ node-pg-migrate --config-file ./test.json --tsconfig ./tsconfig.json up
error: Cannot find package 'dotenv' from '/project/node_modules/node-pg-migrate/bin/node-pg-migrate.js'
error: script "migrate" exited with code 1