Correct way to access modules that aren't part of the dependencies in PnP strict mode #4940
Replies: 2 comments
-
So what I've arrived at so far is using the pnpapi directly to resolve packages in case it is available. That allows me to distinguish whether I am running in pure npm or within yarn PnP. I can then use PnP's resolveRequest to resolve to the actual fileSystem location. That seems to work so far. If anyone else has a better idea that isn't so roundabout please let me know. The solution here is to check if You can then get the file locations equivalent by running:
|
Beta Was this translation helpful? Give feedback.
-
Use the require.resolve('packageName', {paths: ['/path/to/resolve/from']}) https://nodejs.org/dist/latest-v16.x/docs/api/modules.html#requireresolverequest-options |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
We have a node package that is a helper script that parses values in package.json and extracts files from the modules in question to assist in the build process. We use require.resolve("module") to get access to the on disk path of the module.
With PnP in strict mode the access to other modules is denied because the helper script doesn't directly declare it as a dependency.
Is there any way without switching everything to loose mode to get rid of the requirement to list modules in dependencies for scripts like this?
The require call in this case isn't unsound or ambiguous because the required modules in question are declared in the main projects dependencies.
One possible solution I've thought of is writing a yarn plugin. I'm just wondering whether that's the only officially supported solution.
Beta Was this translation helpful? Give feedback.
All reactions