In memory dependency installer
In-memory dependency installer.
Given a package folder (anything containing a package.json file), Librarian will create a virtual fs-like object representing the node_modules folder of the package:
``javascript${folderPath}/node_modules/lodash/index.js
const librarian = require('librarian')
const folderPath = '/path/to/my/folder'
const vfs = await librarian.createVfs('/path/to/my/folder')
vfs.readFileSync() // Buffer < .. >${folderPath}/node_modules/lodash/index.js
vfs.writeFileSync(
,${folderPath}/node_modules/lodash
'console.log("foo")' // change virtual contents
)
vfs.copySync(
'/home/me/my-code/some-lodash-replacement',
// replace virtual contents with a different implementation${folderPath}/node_modules
)
vfs.copySync(
,`
'/some/path/on/my/hd' // dump virtual contents to disk
)
Librarian caches packages on disk, recreating the node_modules folder in memory on runtime. This saves not only disk space, but also installation time compared to traditional package managers, because files do not need to be copied or linked to the hard-drive for each project.
Librarian has a built-in adapter that patches the fs module in order to provide the vfs to a node executable without writing anything to the hd. This is inspired by tink:
`javascript
const librarian = require('librarian')
const folderPath = '/path/to/my/package'
const testerCode =
const leftPad = require('left-pad')
console.log(leftPad('foo', 5, 0))${folderPath}/index.js
fs.writeFileSync(, testerCode)${folderPath}/index.js
await librarian.runModule() // "00foo"`
Librarian powers the component playground at bit.dev
It was developed to provide a fast and smooth installation experience so that developers can create, change and maintain their components.
##### librarian.createVfs(
Example: vfs = await librarian.createVfs('/path/to/my/module')Promise
Returns a that resolves into a virtual filesystem containing the node_modules of the package. path should be a folder containing at least a package.json file.vfs
The returned is a memfs instance.node_modules
The folder inside vfs will be contained inside the given path, eg.
`javascript // ENOENT
vfs.readdirSync('/path/to/my/module/node_modules') // [ / contents of node_modules / ]
vfs.readdirSync('/node_modules')
``
###### librarian.runModule(
Runs executable in a child process with its fs module with a virtual file system to provide it with its node_modules. Note that the executable should be in a folder containing at least a package.json with the appropriate dependencies.
Returns a node ChildProcess instance.
###### librarian.runMultipleInstalls([
Runs a librarian installation in all folders passed to it concurrently. This means populating the cache with all their dependencies and transitive dependencies, as well as creating a lockfile librarian-manifests.json for each one of them.
Returns a Promise that resolves once all installations are complete.
``
npm test
At the moment, Librarian is not a fully-fledged package manager. We believe it is stable enough to be an infrastructure for one, and are now working on adding some missing features. Notably:
* The ability to add packages to a running librarian instance (started with the runModule method). This would be similar to new packages added to the node_modules folder.FUSE
* Lazily place files in memory rather than preloading everything for less resource utilization.
* Interface with for real-time browsing of a virtual node_modules` folder.
* The ability to run in the browser
We enthusiastically welcome contributions. There is a lot to do, and we have big plans for Librarian. Check out our open issues or start a conversation by opening a new one, or a pull request.
Thanks to Josh Vanderwillik for contributing the package name on npm.