How to use NPM and Yarn in ASP.Net Core?

Asked

Viewed 274 times

1

I’m having trouble using Yarn/NPM in ASP.Net Core.

I am installing packages as follows: (Example installing Jquery)

Yarn: yarn add jquery
NPM: npm install jquery

Everything normally occurs, the problem is that the files go to the folder node_modules, while in ASP.Net Core it is "necessary" that the files are in wwwroot.

I would love to use the Bower, since it has how to define the directories of the installations, however it is not working and is recommending the use of Yarn.

So how could I install the packages to use in ASP.Net Core?

If possible, I would like the files to be installed on wwwroot/lib.

I researched, but only found some things in English, but I’m horrible in English and the translators of the browsers don’t seem to be so much better, it gets all confused.

  • Vinicius, set up npm or Yarn is easy but to copy pro wwwroot you have to use something like webpack

  • @Fabridamazio I used the Gulp to return this, but I’ll see about the Webpack, maybe it has a better way of doing it. Thanks a lot :D

  • @Viníciusfile something new?

1 answer

2

For this, it is possible to use the flag --modules-folder:

yarn install --modules-folder <caminho>

Specifies an alternative location for the directory node_modules, instead of the standard ./ node_modules.

So in your case, you could install the jquery using the following command:

$ yarn add jquery --modules-folder wwwroot/lib

There, your folder structure should look something like this:

pasta_raiz
├── package.json
├── wwwroot
│   └── lib
│       └── jquery
│           └── ...arquivos do jquery
└── yarn.lock

However, as you install more and more npm packages, it can get a little tiresome typing this flag with the path every time.

To "automate" the typing of this flag (and any other flag you need to globally use in your project), you can add a file .yarnrc in your project’s root folder with the following content:

--modules-folder wwwroot/lib

After that, just use the commands normally yarn, yarn install or yarn add <pacote> and modules will already be automatically saved in folder ./wwwroot/lib of your project.


By the way, to yarn documentation is already 100% translated into Brazilian Portuguese, now it’s easier to understand and learn about Yarn or/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.