home shape

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
Frank Celler

Frank Celler

Frank is both entrepreneur and backend developer, developing mostly memory databases for two decades. He is the CTO and co-founder of ArangoDB. Try to challenge Frank asking him questions on C, C++ and MRuby. Besides Frank organizes Cologne’s NoSQL group & is an active member of NoSQL community.

4 Comments

  1. heaven78 on December 28, 2013 at 7:15 pm

    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

    • fceller on December 29, 2013 at 1:50 pm

      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.

      • Alan Plum on January 9, 2014 at 5:17 pm

        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.

Leave a Comment





Get the latest tutorials, blog posts and news: