How to use @yarnpkg/core?
Костя Третяк

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

About: Author of Ditsmod (NodeJS framework written in TypeScript)

Location:
Kharkiv, Ukraine
Joined:
May 29, 2020

How to use @yarnpkg/core?

Publish Date: Feb 27 '21
6 5

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

Comments 5 total

  • Jen
    JenFeb 27, 2021

    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!

    • Костя Третяк
      Костя ТретякFeb 27, 2021

      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.

      • Jen
        JenFeb 27, 2021

        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.

        • Костя Третяк
          Костя ТретякFeb 27, 2021

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

          • Jen
            JenFeb 27, 2021

            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
Add comment