Node.js package guidelines

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

Arch package guidelines

32-bitCLRCMakeCrossDKMSEclipseElectronFontFree PascalGNOMEGoHaskellJavaKDEKernelLispMesonMinGWNode.jsNonfreeOCamlPerlPHPPythonRRubyRustShellVCSWebWine

Package naming

Tip: A custom _pkgname variable can be used instead of pkgname. This variable can generically be defined as follows: _pkgname=${pkgname#nodejs-}

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


npm provides a stable naming scheme for download URLs. PKGBUILD#source source=() array can use the following URL template:$_pkgname/-/$_pkgname-$pkgver.tgz

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 --prefix "${pkgdir}/usr" "${srcdir}/${_pkgname}-${pkgver}.tgz"

    # 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 for similar results as follows:

Another place where you may find references to $pkgdir is the attributes of packages. If you do not care about man pages (they will not be installed for dependencies anyway), you may delete them like this:

An example of all three of these techniques can be seen in readability-cliAUR.

Using nvm

When a node.js-based application requires different version for building or packaging, then nvmAUR can be leveraged.

Add it as a build dependency:

makedepends=('npm' 'nvm')

nvmAUR uses environment variable to look for its prefix, which is set to if not specified before nvmAUR initialization.

You can use the following function to create and isolate your custom prefix from user's location:

This function should be called before interacting with nvmAUR, or any other Node.js based programs that should use the specified version.

Example PKGBUILD usage

Alternatively, bare will look for a version string in file in the current directory.

An example of this usage can be seen in insomniaAUR. See PKGBUILD for more information.

This article is issued from Archlinux. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.