DEV Community

Костя Третяк
Костя Третяк

Posted on

How to use @yarnpkg/core?

I see that on yarn repository mentions about @yarnpkg/core:

allows any application to manipulate a project programmatically.

Okay, that's what I need! But how to use it? I also found the API documentation, but I don't understand how to programmatically install a package from npmjs.com, for example.

Pseudocode of what I want:

import { someUtil } from '@yarnpkg/core';

async function myTest(packageName: string) {
  await someUtil.add(packageName); // Installing from npmjs.com
  const package = await import(packageName); // Load the newly installed package from node_modules.
  package.doSomething(); // Work with the package
}

myTest('somePackage'); // Imagine that I run this function from an HTTP request
Enter fullscreen mode Exit fullscreen mode

Oldest comments (5)

Collapse
 
jenbutondevto profile image
Jen • Edited

Hi there. What's the reason for wanting to add a package to another? using @yarnpkg/core might be a little too low level for what you need, it's essentially reimplementing how yarn installs/resolves modules in your own app.

If it sounds like what you need, then take a look at this test in the yarnpkg/core directory. It includes making a temporary directory, downloading the package to it, and modifying the target package. github.com/yarnpkg/berry/blob/mast....

It sounds like you want to use the patch: protocol. After installing the package (defined in your package.json), then your patch will modify the package. yarnpkg.com/features/protocols#patch. This is a lot simpler than the above.

good luck!

Collapse
 
kostyatretyak profile image
Костя Третяк • Edited

What's the reason for wanting to add a package to another?

I want users to be able to add npm packages to an already running web application. That is, that they add npm package on the fly without having to restart the application.

It sounds like you want to use the patch: protocol. After installing the package (defined in your package.json), then your patch will modify the package.

No, I don't need to change/patch the package, I just need to use it. I know that I can do this using npm, but this method is not suitable for me, because it is a hack, and the official documentation does not contain recommendations in this regard. While the official yarn documentation says it provides a special API to do similar things. The problem is that it is not clear how to use this API.

Now I'm trying to understand the tests, looking for whether they do something similar in them.

Collapse
 
jenbutondevto profile image
Jen

Ok - I think I understand now. I definitely think you don't need yarnpkg/core. That would involve your app downloading into memory into the users browser, and making sure that is ES5 compatible for older browsers.

ou can use skypack skypack.dev. Skypack is a CDN which serves packages for browser environments and node.js too. You can do something like

 import react from 'https://cdn.skypack.dev/react';
// react code works eg react.createElement(...)
Enter fullscreen mode Exit fullscreen mode

These are optimised for browser - they might be for node too but I'm not entirely sure.

Thread Thread
 
kostyatretyak profile image
Костя Третяк

Thank you, Jen. I need dynamic install packages on NodeJS web server.

Thread Thread
 
jenbutondevto profile image
Jen • Edited

Do you need to install it, or just serve it? skypack will work for node also. I haven't tried, but I imagine you can do something like

const pkgUrl = 'https://cdn.skypack.dev/date-fns'
const servePkg = async () => { 
   const { default: pkg } = await import(pkgUrl);
   // pkg.method(...)
 })
Enter fullscreen mode Exit fullscreen mode