Search code examples
node.jsnode-modulesyarnpkgyarn-workspacesyarn-v2

What path do I use in yarn 2 with plug 'n play to access a specific file in a package?


This path was working node_modules/webpack/bin/webpack.js in the following command in Yarn 1...

yarn --cwd packages/mypackage/ node --trace-deprecation node_modules/webpack/bin/webpack.js --env env=dev

I moved to Yarn 2 (berry) with plug 'n play (PNP) and Workspaces so there's not a node_modules folder (the module I need is stored a zip file at .yarn/cache/webpack-npm-5.64.0-a89fa9e176-9d32a61d98.zip. The file within the zip upzips to /node_modules/webpack/bin/webpack.js.

Here's what I tried, but I'm getting a path error, so have the path wrong still...

yarn workspace @myworkspace/mypackage node --trace-deprecation .yarn/plugins/@webpack-npm/bin/webpack.js --env env=dev

If there's a better way to do this that doesn't require writing out this whole path, I'm all for that too.


Solution

  • As stated by the Yarn Docs PnP is now the standard mode for package installation and should automatically create a .pnp.cjs file. You may have to add

    require('./.pnp.cjs').setup();
    

    to your script and/or run

    node -r ./.pnp.cjs ./server.js
    NODE_OPTIONS="--require $(pwd)/.pnp.cjs" node ./server.js
    

    as stated in the docs.

    Look here on how to access your zip file in a function. It should be as easy as require.resolve(webpack/bin/webpack.js)