Node.js package guidelines - ArchWiki

# Node.js package guidelines

From ArchWiki

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

## Package naming

Package names should start with a `nodejs-` prefix.

## Using npm

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

    makedepends=('npm')

This is a minimal `package` function:

    package() {
        npm install -g --user root --prefix "$pkgdir"/usr "$srcdir"/source-tarball.tar.gz

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

### 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 in NPM, and actually considered a feature. However, you may remove those reference manually, since they are not used in any way.

All dependendcies 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 -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.

Category:

- Arch package guidelines