How to use browserify with non-npm libraries?

混江龙づ霸主 提交于 2020-01-01 06:28:10

问题


According to http://www.slant.co/topics/1089/viewpoints/1/~what-are-the-best-client-side-javascript-module-loaders~browserify#9 one of the downside of using Browserify is that:

Not all javascript libraries have an npm version

While it's not too hard to create npm package for an existing library, it means maintaining it when the library updates. While most libraries are now on npm, many client side specific libraries are not.

I don't have any experience with npm aside from knowing how to install an existing module. In light of that, what is the easiest/best way to browserify with client-side non-npm libraries?

Is there a way for me to declare a local Javascript file as a dependency, instead of looking it up through npm?


回答1:


You can use local modules without problems by two ways:

1.Use a relative path to a module in require:

var myModule = require('../js/my-module');

2.Use a module name, but before, you should to add it to browser property in package.json:

package.json:

...
browser: {
  my-module: './js/my-module.js'
}

app.js:

var myModule = require('my-module');



回答2:


Some packages are packages with bower, these can be used with browserify by using the debowerify plugin.

For non-versioned things you can copy them to a lib directory in your project or add them as a git submodule and then configure browserify so that it can find things there too.



来源:https://stackoverflow.com/questions/28061040/how-to-use-browserify-with-non-npm-libraries

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!