githubnpmazure-devopsnpm-publishgithub-packages

Moving npm packages from Azure Devops to Github


We used to have our npm packages in Azure DevOps Artifacts Feed packages, but now we moved everything to GitHub and we're trying to move the npm and NuGet packages as well.

Let's say my company name is "XYZ".

After so many failed trials, I was finally able to add some packages to GitHub but the problem is that I had to change their names from "ABC" to "@XYZ/ABC". This will require me to update all the apps that were using this package to include the organization.

Is there a way to publish packages to GitHub without adding the organization name? Or at least a way to inform npm that whenever I do npm install for a package, check in @XYZ first?


Solution

  • You may consider the workaround to explicitly set the GitHub registry from which your packages are installed. As far as I have tested, we don't have to change the package names in the package.json.

    npm config set registry https://npm.pkg.github.com/@<MyGitHubOrgName>
    

    or

    npm install --registry=https://npm.pkg.github.com/@<MyGitHubOrgName>
    

    enter image description here

    enter image description here

    If your actual expectation is to publish unscoped npm packages to GitHub Packages, I am afraid that according to this document on Working with the npm registry - GitHub Docs

    GitHub Packages only supports scoped npm packages.

    ...

    Verify the name of your package in your project's package.json. The name field must contain the scope and the name of the package. For example, if your package is called "test", and you are publishing it to the "My-org" GitHub organization, the name field in your package.json should be @my-org/test.

    It seems that all npm packages not only those published to Azure Artifacts feeds are required to be added with a scope before publishing to GitHub Packages. It is more likely by the design of GitHub Packages feature rather than an issue of Azure DevOps. In addition, as you may have already noticed, a scope is not required for an npm package published to Azure Artifacts.

    Please also note as that is stated in the npm document.

    Scopes are a way of grouping related packages together, and also affect a few things about the way npm treats the package.

    Each npm user/organization has their own scope, and only you can add packages in your scope. This means you don't have to worry about someone taking your package name ahead of you. Thus it is also a good way to signal official packages for organizations.