We have a shared assets library shared between multiple frontends under a single monorepo and would like to be able automatically detect if node_modules is used in the path to allow the copy to search recursively upwards. Due to the hoisting nature of packages to the top level of the monorepo, this breaks node_modules relative copy paths.
@admosity can you describe more verbosely you use case? Example repo will be great!
We have multiple frontends that rely on a shared assets repository that is distributed via npm packaging. These frontends have to be developed together. Using lerna with hoist, the packages get moved to the monorepo level and the copy configuration that originally relied on the relative path would break.
https://github.com/admosity/lerna-copy-webpack-plugin-reproduce
There's two angular-cli frontends in there that leverage the webpack copy plugin under the hood. Both repos have a git dependency (https://github.com/admosity/shared-assets-lib):
"shared-assets-lib": "admosity/shared-assets-lib#master"
Which holds the following cat image:

The frontends have a single page that just shows the cat packages/client1/src/app/app.component.html which has:
<img src="/cat-assets/cat.jpg" alt="">
The copy configuration has this entry in both the frontends:
{
"glob": "**/*",
"input": "../node_modules/shared-assets-lib/assets",
"output": "./cat-assets/"
}
When going to each individual project, you are able to
@admosity Thank you, this is very rare use case. Therefore, hardly anyone is implementing it except you, so PR welcome. Seems it is more bug then feature.
+1, would also be useful for projects using yarn workspaces
PR welcome
@smirnowld can't reproduce with latest lerna, both examples work fine
What is the recommended way to copy files from node_modules? When using yarn workspaces, there's no guarantee whether it will be in the project's node_modules or hoisted up to the root level.
new CopyWebpackPlugin([{
from: `${path.dirname(require.resolve(`${moduleName}/package.json`))}/xxx`,
to: 'xxx',
}
]),
When work with yarn workspace, I use path.dirname and require.resolve to resolve module's real path.
Most helpful comment
+1, would also be useful for projects using yarn workspaces