Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a .NET (Core) equivalent of npm link?

This essentially amounts to, "how do I clone a .NET project, make modifications, and use it rather than the published one?"

In NodeJS, we have npm link, which lets you link a local package (module) in your node_modules/ directory, to your current project. So for example, instead of using Express in your package.json, you can

  1. Clone Express
  2. Make modifications to express
  3. Compile (transpile, if necessary) and/or build
  4. Run npm link in Express repo to create a globally available local package
  5. Run npm link express in your current project to use your local express, rather that the one you would get if you npm install it.

With .NET, the closest solution I have seen so far includes creating a local feed, but in my experimentation this doesn't seem to work. Other questions on stack overflow like how to use local packages in .net seem to offer solutions using RestoreSources, which is virtually undocumented across the entire web. When attempting to change RestoreSources to use a LocalPackages directory, it is not clear to me the local packages are being used or not (source in obj/ directory still seems to come from nuget package rather than local).

like image 531
netpoetica Avatar asked May 30 '19 19:05

netpoetica


People also ask

What is .NET npm?

npm allows you to install and manage packages for use in both Node. js and ASP.NET Core applications.

What is npm vs NuGet?

Npm installs and tracks JavaScript dependencies. NuGet manages dependencies for . NET projects. Gradle manages dependencies and the build process for Java projects.

Why npm link is not working?

This can be all the more frustrating because npm link doesn't throw an error when this happens. Notice how the bottom line resolves to a folder where the dependency resides locally. To fix this, use node -v in each project and check the running version of node. Use nvm install x.x.x & nvm use x.x.x to match them up.


1 Answers

For anyone in the future wondering this, the correct answer is to use the local feed. My problem was that I had cached nuget packages which were being resolved during dotnet restore.

The following command before restore solved my issues:

dotnet nuget locals all --clear
dotnet restore

Essentially, you need to have a nuget config (NuGet.config) in your solution that sets up your local packages directory:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
    <clear />
    <add key="LocalDev" value="./my-project/artifacts" />
    <add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
</packageSources>
</configuration>

Where artifacts/ directory contains all of the .nupkg packages you want to use during restore. It is obviously also essential that you must make sure those packages are built/compiled before you dotnet restore in your primary solution:

dotnet pack /p:Version=5.22.12 --configuration Release --force --output "%~dp0artifacts";
like image 185
netpoetica Avatar answered Sep 20 '22 16:09

netpoetica