Search code examples
node.jsnode-modulesyarnpkgyarnpkg-v3

yarn pnp fallback to default require


I'm writing some unit tests to check if my-module loads correctly a user-land module external-module:

const myMod = require('my-module')

myMod( { load: 'external-module' } )

Right now I'm having the error:

Your application tried to access transport, but it isn't declared in your dependencies; this makes the require call ambiguous and unsound.  Required package: transport Required by: /Users/mspigolon/workspace/pino/test/  Require stack: - /Users/mspigolon/workspace/pino/test/transport.test.js

This is loud and clear because of the test that creates at runtime the dependency in the node_modules folder and it is not processed by yarn during the installation phase.

  await symlink(
    join(__dirname, 'fixtures', 'transport'),
    join(__dirname, '..', 'node_modules', 'transport')
  )

I cannot find a way to say to [email protected] to do the fallback to the filesystem.

I read about the PnPApi but I did not figure out how to use them to load this dependency created at runtime. Could you help me? Is it the right choice or do you see a better way?

I have tried the pnpIgnorePatterns option too, but it ignore the whole file, not just one single require

Another solution would be to publish the test-transport-module and install it in my opinion, but I would avoid it.

My yarnrc is quite straightforward:

yarnPath: ".yarn/releases/yarn-berry.cjs"
nodeLinker: pnp
pnpMode: loose

Do you have any tips?


Solution

  • The best solution is to install the dependency within the link: protocol

    yarn add --dev transport@link:./test/fixtures/transport