https://github.com/narazaka/browserfs-module.js
require() with BrowserFS
https://github.com/narazaka/browserfs-module.js
browserfs nodejs require
Last synced: 6 months ago
JSON representation
require() with BrowserFS
- Host: GitHub
- URL: https://github.com/narazaka/browserfs-module.js
- Owner: Narazaka
- Created: 2016-07-17T18:47:10.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2020-07-07T02:23:38.000Z (over 5 years ago)
- Last Synced: 2025-04-13T15:08:18.129Z (6 months ago)
- Topics: browserfs, nodejs, require
- Language: JavaScript
- Size: 175 KB
- Stars: 7
- Watchers: 2
- Forks: 1
- Open Issues: 13
-
Metadata Files:
- Readme: Readme.md
Awesome Lists containing this project
README
# [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
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!"```
## License
This is released under [MIT License](https://narazaka.net/license/MIT?2016).