Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yarn 2 workspaces not installing dependencies

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'
}
like image 248
LoneWolfPR Avatar asked Mar 26 '21 14:03

LoneWolfPR


People also ask

How do I install missing dependencies in yarn?

Installing Options There are many options for installing dependencies, including: Installing all dependencies: yarn or yarn install. Installing one and only one version of a package: yarn install --flat. Forcing a re-download of all packages: yarn install --force.

How do you add dependency to yarn?

You can also add other types of dependencies using flags: yarn add --dev to add to devDependencies. yarn add --peer to add to peerDependencies. yarn add --optional to add to optionalDependencies.

Where does yarn 2 install packages?

Yarn 2 uses a new Plug'n'Play (PnP) architecture that is a huge departure from how npm projects have always worked. PnP does away with the node_modules/ folder where packages get installed.

Does yarn install add dev dependencies?

Yarn will not install any package listed in devDependencies if the NODE_ENV environment variable is set to production .

How do I install dependencies in yarn?

There are many options for installing dependencies, including: 1 Installing all dependencies: yarn or yarn install 2 Installing one and only one version of a package: yarn install --flat 3 Forcing a re-download of all packages: yarn install --force 4 Installing only production dependencies: yarn install --production More ...

What are yarn workspaces?

Yarn is fast. Yarn stores packages in a cache. Yarn is parallel. But what about Yarn workspaces? That’s how Yarn explains them: Yarn Workspaces is a feature that allows users to install dependencies from multiple package.json files in subfolders of a single root package.json file, all in one go.

What is yarn install used for in Python?

yarn install is used to install all dependencies for a project. The dependencies are retrieved from your project’s package.json file, and stored in the yarn.lock file. When developing a package, installing dependencies is most commonly done after: You have just checked out code for a project that needs these dependencies to function.

How to set yarn 2 as a package manager in Eclipse?

To set yarn 2 as a package manager for the current project you need run two commands in your project root: Now you ready to define your sub-packages. Lets create package-a folder and package-b folder. \hello-yarn-workspaces \package-a \package-b package.json Let’s run yarn init -y for both folders, let’s look again to our structure


2 Answers

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.

like image 132
Daniel Cooke Avatar answered Oct 13 '22 03:10

Daniel Cooke


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

like image 25
techyhippy Avatar answered Oct 13 '22 03:10

techyhippy