Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doesn't work with yarn workspaces by default #39

Closed
bryzaguy opened this issue Sep 22, 2017 · 10 comments
Closed

Doesn't work with yarn workspaces by default #39

bryzaguy opened this issue Sep 22, 2017 · 10 comments

Comments

@bryzaguy
Copy link

I have a monorepo which is essentially a repository with many npm packages in it and I just started using yarn workspaces which hoists all dependencies to the root of the repository. I realized that all my node_modules were getting included because they were now located in a node_modules folder two parents up. So I had to switch to externals: /node_modules/

@barraq barraq mentioned this issue Nov 2, 2017
2 tasks
@alexrqs
Copy link

alexrqs commented Nov 16, 2017

I'm having the same issue so that PR witth moduleDirs will be awesome, I just don't know how @barraq resolved the issue with 2 node_modules folders because when you are using lerna, the folders inside packages/ will have their own node_modules with the symlinks created by lerna and other node packages will be on the root, that makes 2 node_modules folder to inspect.
This is valid with workspaces only, if you don't use workspaces all is good but for my particular case where I'm using a monorepo full of react components and on the root folder a storybook installation to preview the components, if I don't use workspaces react gets repeated and doesn't work so thats why the approach with webpack-node-externals is important.
right now it kinda works with

externals: [/^[a-z\-0-9]+$/],

but is not the most optimal way as @bryzaguy says

@barraq
Copy link

barraq commented Nov 17, 2017

@alexrqs actually I am using this patch for our monorepo at Freeletics. I simply have this configuration:

externals: [nodeExternals({
  modulesDirs: ['node_modules', path.resolve(__dirname, '../../node_modules')],
})],

and it works like a charm ;) actually that was the reason of the patch.

@chadly
Copy link

chadly commented Jan 10, 2018

@barraq you can accomplish the same thing without any code changes to this package by just including multiple instances:

externals: [
  nodeExternals(),
  nodeExternals({
    modulesDir: path.resolve(__dirname, '../../node_modules')
})],

@atdixon
Copy link

atdixon commented Feb 22, 2018

+1

Ideally the default modulesDir behavior would be to walk the workspace to its root (consulting all node_modules directories from package to root); thus, webpack-node-externals would work out of the box for monorepos.

There are a few issues with the current solutio of hard-coding module dirs:

  • It is brittle (ie, breaks if a package is moved/copied etc within the monorepo).
  • Options (eg whitelist) have to be repeated for each declaration.
  • Etc.

Also, node itself seems to resolve from node_modules up the directory tree. (I see it do so when I run node scripts that require hoisted libs.) Webpack and other libraries, similarly. So this would be idiomatic, no? If so, perhaps there's a direct/normalized library/facility for doing this walk that could be leveraged.

@sandangel
Copy link

hi, may I ask the current state of this issue? any update?

@alfonsolamadrid
Copy link

alfonsolamadrid commented Oct 18, 2018

In case somebody else is having trouble when using lerna or yarn_workspace, I've come across this answer in s.o..

It turns out, webpack-node-externals can "Read the modules from the package.json file instead of the node_modules folder".

By obtaining the dependencies via the package.json instead of depending on what's actually inside the node_modules dir, it wouldn't matter if the actual node modules are scoped out of the package when working as with a monorepo with "Yarn Workspaces" or "lerna".

Just my two cents (honest and perhaps naive question) wouldn't this be better if defaulted to true?

e.g. options.modulesFromFile (=true)

@sibelius
Copy link

how can I whitelist webpack/hot/poll?1000 in a monorepo setup?

@shaunlmason
Copy link

how can I whitelist webpack/hot/poll?1000 in a monorepo setup?

Try:

 externals: [
     nodeExternals({
         modulesDir: path.resolve(__dirname, './node_modules'),
         whitelist: ['webpack/hot/poll?1000']
     }),
     nodeExternals({
         modulesDir: path.resolve(__dirname, '../../node_modules'),
         whitelist: ['webpack/hot/poll?1000']
     })
 ],

@moshest
Copy link

moshest commented Jun 17, 2020

If you have many packages and you want to query them all, use this:

import glob from 'glob';

const externals = glob
    .sync('*/node_modules/', { absolute: true, cwd: __dirname })
    .map((modulesDir) => nodeExternals({ modulesDir }));

@liady
Copy link
Owner

liady commented Jul 8, 2020

In version 2, you can now specify additionalModuleDirs in the options.

@liady liady closed this as completed Jul 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests