How do you use an npm library on the front-end wit

2019-09-05 01:07发布

A couple projects I've seen seem to be usable on the front-end, but the only "Install" instruction in the doc is a npm install xxx line.

The projects use webpack, and it's either obvious, or requiring a plugin that I'm missing, but I could not find anything related to npm modules in the webpack docs

My google / reading skills being obviously failing, could someone please RTFM me about the steps on how to go from :

npm install foo

to running this in a browser :

var foo = require("foo");

1条回答
男人必须洒脱
2楼-- · 2019-09-05 02:02

Well, turns out that's because you... hardly have anything to do.

In my case :

npm install -g webpack
npm install babel-loader
npm install redux

In webpack.config.js

module.exports = {
    entry: "./public/javascripts/entry.js",
    output: {
        path: __dirname,
        filename: "./public/javascripts/bundle.js"
    },
    module: {
        loaders: [
            { test: /\.jsx?$/,
              exclude: /(node_modules|vendor)/,
              loader: "babel-loader"
            }
        ]
    }
};

In entry.js :

var redux = require("redux");
console.log(redux)

And things work... will flag for closing.

查看更多
登录 后发表回答