# Yarn workspaces and Ember

**URL:** <https://discuss.emberjs.com/t/yarn-workspaces-and-ember/15672>\
**Category:** Uncategorized\
**Created:** [October 17, 2018, 3:54pm UTC](https://discuss.emberjs.com/t/yarn-workspaces-and-ember/15672 "2018-10-17T15:54:10Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![ryanto](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/ryanto/32/13850_2.png) [@ryanto](https://discuss.emberjs.com/u/ryanto)\
**Post date:** [October 17, 2018, 3:54pm UTC](https://discuss.emberjs.com/t/yarn-workspaces-and-ember/15672/1 "2018-10-17T15:54:10Z")

</div>

Recently I added yarn workspaces to my Ember project and I’ve been super happy with the workflow! I’m able to work on an npm package alongside my Ember application without having to worry about `npm link` or `npm publish`.

However, it seems in doing this I broke `ember install`, since it causes yarn to error. Here’s an example.

```nohighlight
~/my-ember-app$> ember install ember-cli-clipboard                                                                                                                                                       

Command failed: yarn add --dev ember-cli-clipboard --non-interactive
error Running this command will add the dependency to the workspace
root rather than the workspace itself, which might not be what you
want - if you really meant it, make it explicit by running this
command again with the -W flag (or --ignore-workspace-root-check).

```

My workspace root is the Ember app and all of the workspaces are subdirectories in `lib` under the Ember application. I think that’s what is causing the issue.

I found this [article from Square](https://medium.com/square-corner-blog/ember-and-yarn-workspaces-fca69dc5d44a) and it looks like they keep all their workspaces in a packages folder, without any project at the root. Is that what I should be doing if I want to use workspaces in an Ember app?

---

<div class="post-metadata">

**Author:** ![Spencer\_Price](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/spencer_price/32/14878_2.png) [@Spencer\_Price](https://discuss.emberjs.com/u/Spencer_Price)\
**Post date:** [October 17, 2018, 4:41pm UTC](https://discuss.emberjs.com/t/yarn-workspaces-and-ember/15672/2 "2018-10-17T16:41:38Z")

</div>

Hey @ryanto! — we’ve been using Yarn workspaces for some time.

We keep the root-level directory basically empty and keep all of our modules in a `packages` folder (same, it seems, as what Square does).

For what it’s worth, we also moved our `.eslintrc.js` and `.prettierrc` files to the top level — so those are only specified in the nested packages on a case-by-case basis. Not sure if this ended up being the _best_ idea because we have our yarn workspaces configured not to hoist any `eslint` package:

This is roughly our root-level `package.json`:

```json
{
  "name": "our-monorepo",
  "version": "0.0.0",
  "private": true,
  "workspaces": {
    "packages": [
      "packages/*"
    ],
    "nohoist": [
      "eslint"
    ]
  },
  "license": "SEE LICENSE IN README.md",
  "dependencies": {
    "eslint-config-prettier": "2.7.0",
    "eslint-plugin-prettier": "2.3.1"
  }
}

```

---

<div class="post-metadata">

**Author:** ![lennyburdette](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/lennyburdette/32/13794_2.png) [@lennyburdette](https://discuss.emberjs.com/u/lennyburdette)\
**Post date:** [October 22, 2018, 5:22pm UTC](https://discuss.emberjs.com/t/yarn-workspaces-and-ember/15672/3 "2018-10-22T17:22:02Z")

</div>

Hi, I wrote that blog post. 😁

If I’m reading this correctly:

> My workspace root is the Ember app and all of the workspaces are subdirectories in `lib` under the Ember application.

… your project looks like this?

```sh
package.json # with "workspaces" config
ember-cli-build.js
app/
lib/
├── some-package/
├── ├── package.json
├── ├── index.js
├── another-package/
├── ├── package.json
├── ├── index.js

```

Yeah, I don’t think that’ll work. The top-level “package” shouldn’t be a real package and generally doesn’t have its own dependencies. You don’t have to nest projects under a “packages” directory, but it’s very common in monorepos like babel so I used that as the example layout in the blog post.

My current project layout looks like this:

```sh
package.json
.eslintrc.js # and other dotfiles
team-one/
├── engine-a/
├── ├── package.json
├── ├── index.js
├── ├── addon/
├── addon-b/
├── ├── package.json
├── ├── index.json
├── ├── addon/
├── ├── app/
team-two/
├── ├── engine-c/
... # more packages
host-application/
├── package.json
├── ember-cli-build.js

```

The top-level package.json is just:

```json
{
  "private": true,
  "workspaces": [
    "host-application",
    "team-one/*",
    "team-two/*"
  ]
}

```

Hope this helps!

---

<div class="post-metadata">

**Author:** ![2hu12](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/2hu12/32/14135_2.png) [@2hu12](https://discuss.emberjs.com/u/2hu12)\
**Post date:** [April 22, 2019, 3:23am UTC](https://discuss.emberjs.com/t/yarn-workspaces-and-ember/15672/4 "2019-04-22T03:23:13Z")

</div>

It’s a very excellent blog post!

I have a question, you use `yarn add` command directly or `ember install` on ember addon installation if all are workspace packages including the host app?

Also, I have another question on how to install an ember addon with a default addon blueprint to a workspace package, like the [ember-highcharts](https://github.com/ahmadsoe/ember-highcharts/blob/master/blueprints/ember-highcharts/index.js)? Cause it’ll add another dependency “highcharts” to it’s package.json and `ember-cli` just doesn’t support to install a addon to a workspace package.

Thanks!

---

<div class="post-metadata">

**Author:** ![lennyburdette](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/lennyburdette/32/13794_2.png) [@lennyburdette](https://discuss.emberjs.com/u/lennyburdette)\
**Post date:** [April 23, 2019, 12:02am UTC](https://discuss.emberjs.com/t/yarn-workspaces-and-ember/15672/5 "2019-04-23T00:02:01Z")

</div>

@2hu12 I run `ember install` within each package in the workspace if I need the addon’s blueprint to run during installation. I basically pretend that I’m working on each addon/app independently and never install packages in the top-level package.json.

I’m not sure I understand your second question, especially the “`ember-cli` just doesn’t support to install a addon to a workspace package” part. Can you elaborate?

---

<div class="post-metadata">

**Author:** ![2hu12](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/2hu12/32/14135_2.png) [@2hu12](https://discuss.emberjs.com/u/2hu12)\
**Post date:** [April 23, 2019, 3:11am UTC](https://discuss.emberjs.com/t/yarn-workspaces-and-ember/15672/6 "2019-04-23T03:11:33Z")

</div>

@lennyburdette

> `ember-cli` just doesn’t support to install a addon to a workspace package

When all dependencies are installed on the workspace root node\_modules and if I run `ember install` within each paceage, it will get an error of “node\_modules appears empty, you may need to run `yarn install`”.

Do you use the “nohoist” option of yarn workspace to avoid this problem?

---

<div class="post-metadata">

**Author:** ![lennyburdette](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/lennyburdette/32/13794_2.png) [@lennyburdette](https://discuss.emberjs.com/u/lennyburdette)\
**Post date:** [April 23, 2019, 3:38am UTC](https://discuss.emberjs.com/t/yarn-workspaces-and-ember/15672/7 "2019-04-23T03:38:23Z")

</div>

When you say dependencies are installed in the workspace root, do you mean that that they’re listed in the root package.json’s `dependencies` or `devDependencies`? Or just that the packages exist in the top-level node\_modules folder?

If it’s the former, that’s not what you want. Make sure you’re always installing dependencies in each ember app or addon.

If it’s the latter, are you on a recent version of ember-cli? Yarn workspaces support wasn’t complete until 3.1.

---

<div class="post-metadata">

**Author:** ![2hu12](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/2hu12/32/14135_2.png) [@2hu12](https://discuss.emberjs.com/u/2hu12)\
**Post date:** [April 23, 2019, 3:47am UTC](https://discuss.emberjs.com/t/yarn-workspaces-and-ember/15672/8 "2019-04-23T03:47:29Z")

</div>

I mean the latter one and I use the ember-cli@3.9.0.

Does it have node\_modules folder within each package or there is only one node\_modules folder of the workspace root?

---

<div class="post-metadata">

**Author:** ![lennyburdette](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/lennyburdette/32/13794_2.png) [@lennyburdette](https://discuss.emberjs.com/u/lennyburdette)\
**Post date:** [April 23, 2019, 8:15pm UTC](https://discuss.emberjs.com/t/yarn-workspaces-and-ember/15672/9 "2019-04-23T20:15:33Z")

</div>

@2hu12 You will have n+1 node\_modules folders (where n is the number of packages in your workspace root.)

Most dependencies will end up in the top-level node\_modules folder. This is also where the symlinks to your workspaces live so node’s require algorithm can find them.

You’ll also have a node\_modules folder in each workspace. If there aren’t conflicting dependencies, it will be mostly empty. It will _always_ contain a .bin directory with executables though.

Sounds like you have everything set up correctly. I’m not sure why you’re still seeing an error.

---

<div class="post-metadata">

**Author:** ![2hu12](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/2hu12/32/14135_2.png) [@2hu12](https://discuss.emberjs.com/u/2hu12)\
**Post date:** [April 24, 2019, 3:30am UTC](https://discuss.emberjs.com/t/yarn-workspaces-and-ember/15672/10 "2019-04-24T03:30:07Z")

</div>

I got the “node\_modules appears empty, you may need to run `yarn install`” error cause it only has one workspace root “node\_modules” when I run “ember install” within one package.

If you have “n+1 node\_modules folders”, the “nohoist” option is necessary, right?

---

<div class="post-metadata">

**Author:** ![lennyburdette](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/lennyburdette/32/13794_2.png) [@lennyburdette](https://discuss.emberjs.com/u/lennyburdette)\
**Post date:** [April 26, 2019, 3:42pm UTC](https://discuss.emberjs.com/t/yarn-workspaces-and-ember/15672/11 "2019-04-26T15:42:11Z")

</div>

I only get that error message when I’m running the ember command outside of an app or addon directory. Is your globally installed version of ember-cli up to date?

I haven’t found a need for nohoist yet—what you’re trying to do should work without it.

---

<div class="post-metadata">

**Author:** ![2hu12](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/2hu12/32/14135_2.png) [@2hu12](https://discuss.emberjs.com/u/2hu12)\
**Post date:** [May 16, 2019, 4:12am UTC](https://discuss.emberjs.com/t/yarn-workspaces-and-ember/15672/12 "2019-05-16T04:12:31Z")

</div>

Thank you @lennyburdette and sorry for this late response.

According to the first item in the [Limitations & Caveats](https://yarnpkg.com/en/docs/workspaces#toc-limitations-caveats) section,

> the workspace dependencies will be hoisted higher into the filesystem hierarchy

If you don’t use the “nohoist” option, how do you make sure that there will be n+1 “node\_modules”? Image that if one workspace package “addon-a” only has common dependencies with another workspace package “addon-b”, the dependencies of “addon-a” and “addon-b” will be hoisted to the root-level “node\_modules”, right?

I have a [GitHub repo branch](https://github.com/2hu12/ember-engine-verification/tree/yarn-workspace) for the problem i had which is I don’t know how to install an ember addon like “ember-fetch” or “ember-highcharts” to one of it’s packages like “engine-a-lazy”, it also has a [doc](https://github.com/2hu12/ember-engine-verification/blob/yarn-workspace/ISSUE.md) for the issues.

---

<div class="post-metadata">

**Author:** ![lennyburdette](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/lennyburdette/32/13794_2.png) [@lennyburdette](https://discuss.emberjs.com/u/lennyburdette)\
**Post date:** [May 16, 2019, 4:10pm UTC](https://discuss.emberjs.com/t/yarn-workspaces-and-ember/15672/13 "2019-05-16T16:10:19Z")

</div>

Node’s module resolution algorithm will traverse the file system hierarchy to find packages when they’re hoisted. This is what makes workspaces work!

I think the issue in your example repo is that the root is both a package and a workspace root. You have things set up the way ryanto did at the top of this thread. ([My response to that](https://discuss.emberjs.com/t/yarn-workspaces-and-ember/15672/3))

Compare my demo repo: [GitHub - lennyburdette/ember-monorepo-demo](https://github.com/lennyburdette/ember-monorepo-demo) … the host application is just another package in the workspace root, and the `"workspaces"` key in the root package.json can find it.
