Node.js package guidelines

From ArchWiki
Jump to navigation Jump to search
Arch package guidelines

32-bitCLRCMakeCrossDKMSEclipseElectronFontFree PascalGNOMEGoHaskellJavaKDEKernelLispMesonMinGWNode.jsNonfreeOCamlPerlPHPPythonRRubyRustVCSWebWine

This document covers standards and guidelines on writing PKGBUILDs for Node.js packages.

Package naming

Package names for Node.js libraries should start with a nodejs- prefix. For standalone applications, just use the program name.

Using npm

When installing with npm, add it as a build dependency:


There is also usually no need to extract the tarball:


This is a minimal package function:

package() {
    npm install -g --user root --prefix "${pkgdir}/usr" "${srcdir}/${pkgname}-${pkgver}.tgz"

    # Non-deterministic race in npm gives 777 permissions to random directories.
    # See for details.
    find "${pkgdir}/usr" -type d -exec chmod 755 {} +

    # npm gives ownership of ALL FILES to build user
    chown -R root:root "${pkgdir}"

Setting temporary cache

When npm processes package.json in order to build a package it downloads dependencies to its default cache folder at $HOME/.npm. To avoid littering user's home folder we can temporarily set a different cache folder with --cache flag:

Download dependencies to ${srcdir}/npm-cache and install them in package directory

npm install --cache "${srcdir}/npm-cache" 

Continue with packaging as usual

npm run packager

Package contains reference to $srcdir/$pkgdir

npm unfortunately creates references to the source dir and the pkg dir, this is a known issue. However, you may remove those references manually since they are not used in any way.

All dependencies will contain a reference to $pkgdir, in the _where attribute. You can usually remove those attributes with some sed magic as follows:

 find "$pkgdir" -name package.json -print0 | xargs -r -0 sed -i '/_where/d'

Your main package will have some other references too. The easiest way to remove those is to remove all underscored properties, but that is not as easy with sed. Instead, you can use jq for similar results as follows:

local tmppackage="$(mktemp)"
local pkgjson="$pkgdir/usr/lib/node_modules/$pkgname/package.json"
jq '.|=with_entries(select(.key|test("_.+")|not))' "$pkgjson" > "$tmppackage"
mv "$tmppackage" "$pkgjson"
chmod 644 "$pkgjson"

An example of both techniques can be seen in bower-awayAUR.