Yarn 2 workspaces not installing dependencies
Asked Answered
B

2

15

I'm testing out setting up yarn 2 workspaces. I think I've done it the way I'm supposed to, but when I run yarn install from the root it doesn't install any modules nor does it create the symplink to dependencies as expected. I have the following folder structure

root/
  package-a/
  package-b/

Each contains a package.json and each of the package folders contains an index.js. Here are the package.json files

root:

{
  "name": "yarn-workspaces-poc",
  "version": "1.0.0",
  "license": "MIT",
  "private": true,
  "workspaces": [
    "package-a/",
    "package-b/"
  ]
}

package-a:

{
  "name": "package-a",
  "version": "1.0.0",
  "type": "module",
  "dependencies": {
    "cross-env": "5.0.5",
    "package-b": "workspace:*"
  }
}

package-b:

{
  "name": "package-b",
  "version": "1.0.0",
  "type": "module",
  "main": "index.js",
  "dependencies": {
    "cross-env": "5.0.5"
  }
}

Here are the js files

package-a/index.js

import test from "package-b";
console.log('testing');
console.log(test());

package-b/index.js

export default function b() {
  console.log("From b. You made it!");
}

The expected behavior is that when I run yarn install from the root a node_modules folder will be created there. It should contain the cross-env package as well as a folder symlinked to package-b. However nothing gets created. Here's the output from the command:

➤ YN0000: ┌ Resolution step
➤ YN0000: └ Completed
➤ YN0000: ┌ Fetch step
➤ YN0000: └ Completed
➤ YN0000: ┌ Link step
➤ YN0000: └ Completed
➤ YN0000: Done in 0s 96ms

edit:

Additionally if I just run package-a to test it this is the result:

internal/process/esm_loader.js:74
    internalBinding('errors').triggerUncaughtException(
                              ^

Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'package-b' imported from /root/package-a/index.js
Did you mean to import package-b/index.js?
    at packageResolve (internal/modules/esm/resolve.js:655:9)
    at moduleResolve (internal/modules/esm/resolve.js:696:18)
    at Loader.defaultResolve [as _resolve] (internal/modules/esm/resolve.js:810:11)
    at Loader.resolve (internal/modules/esm/loader.js:86:40)
    at Loader.getModuleJob (internal/modules/esm/loader.js:230:28)
    at ModuleWrap.<anonymous> (internal/modules/esm/module_job.js:56:40)
    at link (internal/modules/esm/module_job.js:55:36) {
  code: 'ERR_MODULE_NOT_FOUND'
}
Binford answered 26/3, 2021 at 14:55 Comment(1)
yarn v2 is still a pain after a year. I'd revert back to yarn v1Archiepiscopal
J
6

Create a .yarnrc.yml at the root of your monorepo,

Add the following property to it:

nodeLinker: node-modules

Perhaps the most notable change with Yarn 2 - is the PnP system. Say goodbye to node_modules

This is the default behaviour unless you specify the "legacy" node-modules linker

Documented here

Bonus info

For deploying packages separately its sometimes useful to prevent hoisting of node_modules to the root.

You can add

nmHoistingLimits: workspaces

To the .yarnc.yml to ensure every package has their dependencies installed directly at the package level.

This is much more robust than the old noHoist: [*/**] from yarn 1.

Jurado answered 9/8, 2021 at 15:47 Comment(1)
Adding nodeLinker: node-modules still does not create a node_modules folder when running yarn install. It still creates the pnp files and the .yarn directory. I'm using yarn v3.3.1Sible
F
2

I had a similar problem. It turns out the new version of Yarn does not use node_modules:

https://yarnpkg.com/getting-started/migration#switching-to-plugnplay

https://yarnpkg.com/getting-started/migration#final-notes

This is really confusing as it is at odds with the documentation for workspaces.. which describes the outcome you (and I) were expecting: https://yarnpkg.com/features/workspaces

Once you have run 'yarn install', you can start the servers however you did before but prepending 'yarn workspace WORKSPACENAME '..

so if you would normally start like this:

rootfolder$ cd package-b
package-b$ node index.js

you would now run this from the root folder:

rootfolder$ yarn workspace package-b node index.js

There are a few other things that you may need to setup for your IDE etc.. there is plenty of info here: https://yarnpkg.com/getting-started/migration#switching-to-plugnplay

Fireside answered 18/6, 2021 at 21:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.