Referencing resources in node_modules folder

2020-06-30 01:08发布

问题:

I am currently in the process of writing my first Node.js app. I recently installed bootstrap via the npm (following instructions on bootstrap's web site) and was wondering the "standard" way of referencing the bootstap.min.css (and other files of interest). It is best to use grunt/gulp to copy (bundle and minify) the resources I need into my project structure?

Any help would be appreciated.

回答1:

I was able to copy with the node_modules bootstrap resources with a simple Grunt task:

copy: {
    bootstrapSrcCss: {
      src: "./node_modules/bootstrap/dist/css/bootstrap.css",
      dest: "./public/src/css/bootstrap.css"
    },
    bootstrapSrcJs: {
      src: "./node_modules/bootstrap/dist/js/bootstrap.js",
      dest: "./public/src/js/bootstrap.js"
    }
}

Using grunt plug-in: grunt-contrib-copy



回答2:

As explained in This Blog Post, you can create a "Static Route" instead of exposing the node_modules structure totally.

app.use('/scripts', express.static(__dirname + '/node_modules/bootstrap/dist/'));

But that may not be the best method. It seems that packages that come with resources also come with Grunt files as well.