Don Chambers
Don Chambers

Reputation: 4251

NPM location in ASP.Net 5 application

I am creating an ASP.Net 5 application and have a few npm packages that are needed by the client. I have package.json configured and I see the NPM folder under dependencies. I also see the hidden node_modules folder with the actual packages.

These files can't be served because the are not in the wwwroot folder.

I tried adding package.json to the wwwroot folder and that adds the packages to wwwroot. However, I get an error that I think is related to paths that are too long. It get: "the item metadata (fullpath) cannot be applied node_modules". In addition, most examples show package.json at the root level.

I have seen examples of the gulp file that minifies the packages. It seems to expect the packages to be in wwwroot but that is not where they are by default.

I have found a way to move bower packages to wwwroot but I can't use bower for some of these packages.

What's the best practice for this?

Upvotes: 2

Views: 467

Answers (2)

David Anderson
David Anderson

Reputation: 13670

You can use gulp to move the dependencies you need. Here is an example of copying jquery from node_modules to wwwroot.

gulp.task("jquery",
    function() {
        return gulp.src("node_modules/jquery/dist/*")
                   .pipe(gulp.dest("wwwroot/lib/jquery"));
});

Upvotes: 0

Dealdiane
Dealdiane

Reputation: 4064

I'm not aware of any best practice for this but the common practice is what you've described. This is a little bit different for aspnet5 projects however.

The aspnet5 team assumes the you pull down npm packages to be used with gulp. bower on the other hand should be used to pull down frontend dependencies.

I have seen examples of the gulp file that minifies the packages. It seems to expect the packages to be in wwwroot but that is not where they are by default.

The default scaffolding should have a minify task. Take a look at your gulpfile.js. In that file, there's a paths.webroot that is by default ./wwwroot. The min:js task will find all your javascript files in the /wwwroot/js folder, concatenate and minify it. If you need to add more gulp tasks, then you just chain them up in that task. If your scripts are in the /wwwroot/js folder, then there is no need to fiddle with the paths.

Upvotes: 1

Related Questions