TipsAndTricks/NPM
Background
JavaScript is becoming a leading programming language for IoT due to the popularity of Node.js [1] [2] [3]. However Node.js application packages (or modules as they are typically known) tend to have many dependencies and often are not very descriptive of what versions of these dependencies they require. Node.js modules are managed by a tool called Node Package Manager (NPM) which accesses a module registry to install dependencies. In previous versions of Yocto Node.js module recipes created the package by running npm in the do_compile task that would look something like this
SRC_URI = "https://github.com/gruntjs/grunt-cli.git" do_compile() { # changing the home directory to the working directory, the .npmrc will be created in this directory export HOME=${WORKDIR} # configure cache to be in working directory npm set cache ${WORKDIR}/npm_cache # clear local cache prior to each compile npm cache clear # compile and install node modules in source directory npm --arch=${TARGET_ARCH} --verbose install }
The problem with this approach is that the npm install command triggers download of dependent modules. As web operations are not expected in the do_compile task, proxy variables are not propagated so recipes must be extended to add configuration for handling corporate firewalls.
In Yocto 2.1 an NPM fetcher was added to greatly simplify the packaging of Node.js modules as well as helping you check your licensing requirements.
The fetcher is not yet documented in the bitbake manual [4], so this article will help you get the best out of it.
Creating NPM Recipes
Fetcher Syntax
The new npm fetcher uses the npm scheme, must have the registry as the path (usually registry.npmjs.org, but any registry can be used) and requires a name parameter to specify the module. Assuming recipe name and version match the module, the above recipe snippet could be replaced with the following
SRC_URI = "npm://registry.npmjs.org;name=${PN};version=${PV}"
Using devtool
Although npm recipes can be created manually, using devtool make the job much easier. Just run the follow for module grunt-cli, version 1.10.
devtool add "npm://registry.npmjs.org;name=grunt-cli;version=1.1.0"
Under the hood devtool runs recipetool create with the same fetch uri. Recipetool downloads each dependency and write a recipe file. The recipe file is fairly simple but will contain every license that recipetool has found and include it in the LIC_FILES_CHKSUM. Many node modules have unclear licensing so you'll see "unknown" in the LICENSE field. Have a look at the modules not listed.
Recipetool will also create a shrinkwrap and lockdown file for your recipe. Shrinkwrap files in npm are used to make sure that the full dependency chain of a node module is the same as the user expected. Alot of packages don't provide this so we create one on the fly, you can replace it with your own. Lockdown checks that the files recipetool downloaded are the same as the ones your users will download when using your recipe. This simply checks dependencies have not been changed and that your NPM registry is still handing out the same file.
Building & dependencies
Some stuff here