VISHAL DAGA
VISHAL DAGA

Reputation: 4289

webpack - loading from different node_modules for files in different folder

If I have a folder structure like this

root
|
|-src
| |-components
|
|-otherSrc
| |-components
|   |-sub_components
| |-node_modules
|
|-node_modules

Is it possible to load libraries referenced in root/src/components/.js from root/node_modules and for root/othersrc/components/.js from root/othersrc/node_modules (or root/othersrc/components/sub_components/*.js from root/othersrc/node_modules)

Upvotes: 0

Views: 883

Answers (1)

Filip Dupanović
Filip Dupanović

Reputation: 33660

No, node_modules will be scanned only relative to the current directory going up to the root, so your deeply nested directories won't be searched for as candidates for resolving packages.

You can either install all the dependencies somewhere in the ancestry (e.g. /node_modules) or configure resolve.modules and use absolute imports or you'll have to specify relative paths in the packages in node_modules that will only work on your computer.

These approaches leave you at a disadvantage because it's hard to move your sources between machines, ensure that the correct dependencies are resolved and that they can be kept up-to-date. If you really want modularize your application into "components" that have their own dependencies and can be independently redistributed and maintained from the rest of your sources, then you might want to look into the monorepository pattern.

Upvotes: 2

Related Questions