Using NPM Packages for ArangoDB: Enhance Functionality | 2013
ArangoDB follows the Common.JS specification for modules. However, it would be very convenient, if there was an easy way to install a package like “underscore.js”. These package are, for instance, available using NPM. There is a draft for packages on Common.JS which seems to be compatible with NPM.
NPM has a neat way of dealing with version conflicts. Basically, it allows multiple versions to exists simultaneously. For example, assume you have 4 packages A, B, C, D. A requires B and C and D, B requires C. Then directory layout might be as follows.
node_modules
|
+- A
| |
| +- node_modules
| |
| +- B
| | |
| | +- node_modules
| | |
| | +- C (1.0.0)
| |
| +- C (2.0.0)
|
+- D
Package B will see package C in version 1.0.0, while package A sees package C in version 2.0.0.
<!--more-->
This behaviour is easy to implement in ArangoDB. In addition to “Module” there is now a “Package”. Each package has it own module cache. When a package requires a module, the package hierarchy is traversed from the current package to the root (or global) package, until the module is found.
In order to use underscore, switch into the package directory and use NPM to install it
unix> cd /tmp/packages
unix> npm install underscore
npm http GET https://registry.npmjs.org/underscore
npm http 304 https://registry.npmjs.org/underscore
underscore@1.4.4 node_modules/underscore
Now start arangosh with the new “–javascript.package-path” option and enjoy underscore.
unix> arangosh --javascript.package-path /tmp/packages
arangosh> var _ = require("underscore");
arangosh> _.max([1,2,3])
3
4 Comments
Leave a Comment
Get the latest tutorials, blog posts and news:
hi frank,
first merry x-mas!
how to implement other packages within a foxx-app? i tried this:
new folder inside foxx-app:
in lib/node_modules/: npm install passport
then in app.js:
passport = require(‘./lib/node_modules/passport’)
but arango ‘ cannot locate module ‘./lib/node_modules/passport’ for package ‘application”…
how do i have to set the folder structure inside the foxx (package root path) to proper require another package?
thanx a lot
Currently you have to install the npm package inside the global “npm” directory.
However, it is a good point. If possible we should allow npm packages inside the foxx-app as well.
This would actually be a great feature. I think the most important use case for npm in arangodb is using it with Foxx, so separate Foxx apps should be able to use different versions of the same npm packages.
Absolutely. Created a ticket for it here: https://github.com/triAGENS/ArangoDB/issues/728