browserfs-module
Version:
require() with BrowserFS
65 lines (46 loc) • 2.77 kB
Markdown
# [browserfs-module](https://github.com/Narazaka/browserfs-module.js)
[](https://www.npmjs.com/package/browserfs-module)
[](https://www.npmjs.com/package/browserfs-module)
[](https://www.npmjs.com/package/browserfs-module)
[](https://www.npmjs.com/package/browserfs-module)
[](https://github.com/Narazaka/browserfs-module.js)
[](https://github.com/Narazaka/browserfs-module.js)
[](https://david-dm.org/Narazaka/browserfs-module.js)
[](https://david-dm.org/Narazaka/browserfs-module.js#info=devDependencies)
[](https://travis-ci.org/Narazaka/browserfs-module.js)
[](https://ci.appveyor.com/project/Narazaka/browserfs-module-js)
[](https://codeclimate.com/github/Narazaka/browserfs-module.js)
The [Node.js](https://github.com/nodejs/node)' `require()` on the browser!
## Motivation
Browsers do not support the `require()` API on Node.js, since they do not have File Systems. (And we are playing with magic `require()` processors, like webpack).
But we can make fake File System on browsers by using [BrowserFS](https://github.com/jvilk/BrowserFS).
Now, why not use the real `require()`?
## Install
npm:
```
npm install browserfs-module
```
bower:
```
bower install browserfs-module
```
## Usage
`node ./foo.js` corresponds to `Module._load ('./foo.js')`.
You can use `require()` in `./foo.js` just like on Node.js.
```html
<script src="browserfs.js"></script>
<script src="browserfs-module.js"></script>
<script>
var fsBase = new BrowserFS.FileSystem.InMemory();
BrowserFS.initialize(fsBase);
var fs = BrowserFS.BFSRequire('fs');
fs.writeFileSync('/main.js', 'module.exports = require("sub");');
fs.mkdirSync('/node_modules');
fs.writeFileSync('/node_modules/sub.js' ,'module.exports = "sub required!";');
Module = browserfsModule.Module;
var main = Module._load("/main");
console.log(main); // "sub required!"
</script>
```
## License
This is released under [MIT License](https://narazaka.net/license/MIT?2016).