I built a monorepo using Lerna and Yarn workspaces.
Everything works fine but everytime I install a new dependency on a package (let's call him A) using:
yarn add <package_name>
Yarn adds it and then triggers the install script of all the packages in the monorepo, even the ones that A doesn't rely on.
It there anyway to avoid this? It takes a few moment to install them for no reason at all.
Try adding to the specific workspace with:-
yarn workspace <workspace_name> add <package_name>
For some docs check here
Using scope add the package to the particular module.
lerna add some_package_1 --scope=some_module_x
More: https://github.com/lerna/lerna/tree/master/commands/add#readme
You can try Yarn 2 with nodeLinker: node-modules in .yarnrc.yml. Yarn 2 guarantees to trigger rebuild only on packages that have their dependencies changed, this is something that was not guaranteed by Yarn 1. However there will still be a very rare case when seemingly unrelated packages be rebuilt if they are hoisted differently after adding new package, but this will happen very rarely.
Related
I am currently migrating our project from Lerna to PNPM and we have a script we run I have posted it below
"postinstall": "npm run bootstrap"
"bootstrap": "lerna bootstrap --hoist",
"clean": "lerna clean -y",
is there a PNPM equivalent of Lerna bootstrap command and Lerna clean command?
You don't need any Lerna bootstrap equivalent, just get started with pnpm workspace is easy enough, you just need to add a pnpm-workspace.yaml file in your project root and add the location of your packages (which is typically packages/). As mentioned on pnpm documentation:
A workspace must have a pnpm-workspace.yaml file in its root. A workspace also may have an .npmrc in its root.
Hoisting is not recommended with pnpm and is disabled by default, but if you really want to hoist then you can add shamefully-hoist into .npmrc, see pnpm doc - shamefully-hoist
By default, pnpm creates a semistrict node_modules, meaning dependencies have access to undeclared dependencies but modules outside of node_modules do not. With this layout, most of the packages in the ecosystem work with no issues. However, if some tooling only works when the hoisted dependencies are in the root of node_modules, you can set this to true to hoist them for you.
I'm not sure about lerna clean equivalent but to remove a dependency from node module, you can use pnpm remove --recursive as per pnpm doc - remove
When used inside a workspace (with --recursive), removes a dependency (or dependencies) from every workspace package.
When used not inside a workspace, removes a dependency (or dependencies) from every package found in subdirectories.
You can see an example of all of that in Lerna-Lite pnpm-workspace.yaml and I would suggest you to take a look at Lerna-Lite and maybe not give up Lerna entirely, I created Lerna-Lite fork when Lerna was no longer maintained (it is now) and the big difference is that Lerna-Lite only has a subset of the original Lerna commands (all the Lerna commands you mentioned aren't in Lerna-Lite because all package managers now handle these better than Lerna/Lerna-Lite does), the other distinction is that it's much smaller since I also made a few commands as optional packages and only kept version and publish in the core CLI, so it's much smaller when version and publish is all you need. I also don't like the new Lerna changes and the new direction that Lerna is now heading to (it's becoming more & more another Nrwl/Nx product, because Lerna v6 is now requiring Nx behind the scene, and that is not the case with Lerna-Lite, Nx can be installed with Lerna-Lite but it's totally optional and that's the way it should and will always be in Lerna-Lite).
I'm working on private library of react components. I've already setup the whole workflow etc. but I'm not sure about one thing.
In my library I have some packages listed as peerDependencies which have to be peer ones (for example react, cuz only one instance of react can be installed at once, otherwise everything breaks). But if I'd like to setup some tests in my library I need react installed there as devDependency.
So I have to install react as peer and dev in library. And what happens when I publish this package to npm register? devDependencies are excluded here?
Thanks!
If you need it in production, add it to peerDependencies. Otherwise, leave it in devDependencies. When someone installs your library, dev dependencies won't be installed, and peer dependencies should already be installed.
If you create a node package, and you have some dev dependencies, npm wont count them for the publish. That's why it's called devDependencies. It's not under the production code.
I know you have to specify when installing an npm package --save-dev so it saves dependencies onto your json file.
I am using vis-timelime in one of my projects. I have done some changes in vis-timeline, then locally build it and using it as dependency in my project. While doing so, vis-timeline is getting installed properly but i believe the peer dependencies of vis-timeline are not coming. Do note that I'm using npm version - 7.6.3.
cd vis-timeline;
//added some console logs in few files
npm install;
npm run build;
Then in my project-
cd my-app
npm install local-path-to-my-vis-timeline
Running above commands install the vis-timeline in node_modules of my-app. However, other peer dependencies of vis-timeline like vis-data, etc. do not come automatically. Since I am using npm version 7.6.3, wasn't it supposed to happen automatically?? If not, any graceful solution to this?
Or let me know of any other better way to locally do changes in vis-timeline library and use it in my local project for debugging.
Sounds like an issue with npm. This post has a list of solutions that might work.
Otherwise, maybe try using yarn instead of npm?
Is it possible to install a yarn dependency as an editable dependency?
I'm looking for something like python pip's:
pip install -e
For local development of a library. My goal is to see changes in a package I'm developing while simultaneously using it in another package.
You can directly edit the files in node_modules and changes there are reflected immediately, but also overwritten when yarn decides to update the module. Note, that some files need to be built first or might be a bit tricky to edit (minified / transpiled build artefacts). You can also install the module directly from GitHub, if this is easier for your development process.
Apart from that, there is no such thing as "editable" dependencies in npm / yarn.
My node app has a dependency that depends on node-webcrypto-ossl.
I'd like to run something like:
yarn what-depends node-webcrypto-ossl
To see the dependency chain, eg:
A
B
node-webcrypto-ossl
However I can't find anything to do this in the yarn CLI docs.
How do I see what requires a package using yarn?
yarn added the yarn why command specifically to get information on why a package is imported.
For example:
yarn why node-webcrypto-ossl