Pautas de Empaquetado Node.js
La postura de Node.js en los paquetes de biblioteca global ascendente es que son “.. mejor evitados si no se necesitan”. En Fedora, adoptamos la misma postura con nuestros paquetes nodejs. Puedes proporcionar un paquete que utilice nodejs, pero incluiría todas las bibliotecas nodejs que sean necesarias.
Qué Empaquetar
-
El intérprete, cabeceras/bibliotecas de desarrollo, y las herramientas surtidas para gestionar instalaciones en nivel de proyecto.
-
Ejemplos: nodejs, npm, yarn
-
-
Paquetes que proporcionan aplicaciones que los usuarios desearían utilizar en su intérprete.
-
Ejemplos: uglify
-
Pautas de Nomenclatura
-
Paquetes de aplicación que mayormente proporcionen herramientas (como opuesto a bibliotecas) que sucedan para ser escritos en Node.js siguen en su lugar la Guía de Nombrado general.
-
El nombre de un paquete de extensión/biblioteca de Node.js debe empezar por nodejs- y luego el nombre original o el nombre usado en el registro de npm. Por ejemplo: nodejs-foomodule. Aunque es poco común que el nombre de un paquete contenga node, si lo contiene, debe agregar el prefijo nodejs. Por ejemplo, el registro de npm contiene un módulo uuid y uno node-uuid, que deberían nombrarse nodejs-uuid y nodejs-node-uuid, respectivamente.
BuildRequires
Para crear un paquete que sea un módulo de nodejs, agrupe o use módulos de nodejs, o necesite nodejs para generar o probar, necesita tener:
BuildRequires: flexiblas-devel
Macros
Las siguientes macros están definidas para usted:
| Macro | Definición normal | Notas |
|---|---|---|
__nodejs |
%{_bindir}/node |
El intérprete de Node.js |
nodejs_version |
e.g. 0.9.5 |
La versión de Node.js instalada actualmente. |
nodejs_sitelib |
%{_prefix}/lib/node_modules |
Donde se instalan módulos de Node.js escritos exclusivamente en JavaScript |
nodejs_sitearch |
%{_prefix}/lib/node_modules |
Dónde se instalan los módulos nativos de C++ para Node.js |
nodejs_symlink_deps |
%{_prefix}/lib/rpm/nodejs-symlink-deps |
Consulte Symlinking Dependencies a continuación. |
nodejs_fixdep |
%{_prefix}/lib/rpm/nodejs-fixdep |
Consulte a continuación Correcting Dependencies. |
nodejs_arches |
%{ix86} x86_64 %{arm} |
Consulte #ExclusiveArch. Esta macro está proporcionada por |
nodejs_default_filter |
%global __provides_exclude_from ^%{nodejs_sitearch}/.*\\.node$ |
Filtra los elementos no deseados de los módulos nativos. Consulte Filtering Unwanted Provides a continuación. |
These macros are provided by the nodejs-packaging package.
During %install or when listing %files you can use the %nodejs_sitelib or %nodejs_sitearch macro to specify where the installed modules are to be found. For instance:
%files
# A pure JavaScript node module
%{nodejs_sitelib}/foomodule/
# A native node module
%{nodejs_sitearch}/barmodule/
Using this macro instead of hardcoding the directory in the specfile ensures your spec remains compatible with the installed Node.js version even if the directory structure changes radically (for instance, if %nodejs_sitelib moves into %{_datadir}).
ExclusiveArch
The V8 JavaScript runtime used by Node.js uses a Just-in-Time (JIT) compiler that is specially tuned to individual architectures, and must be manually ported to any new architectures that wish to support it. Node.js packages must include an ExclusiveArch line that restricts them to only these architectures.
The %{nodejs_arches} macro is provided to make this easy, so pure JavaScript packages must use:
ExclusiveArch: %{nodejs_arches} noarch
Native (binary) packages must omit noarch and list only %{nodejs_arches} or the list of architectures as appropriate.
Bundled Licenses
The licenses of the bundled Node.js modules need to be in the spec file. If you are using our bundling script, they will be listed in <package>-<version>-bundled-licenses.txt. It is recommended that you include <package>-<version>-bundled-licenses.txt in the rpm.
Cada vez que actualice su paquete, deberá verificar las licencias incluidas en el Listado de Licencia del Software en Fedoras. Tenga en cuenta que el JavaScript pre‐compilado/minimizado puede estar empaquetado, pero el requisito de verificar las licencias también se aplica a él, consulte las directrices de :JavaScript.
List all unique licenses on the License: line of your spec file. Separate each license with the word "and".
License: <license1> AND <license2> AND <license3>
...
Source3: %{npm_name}-%{version}-bundled-licenses.txt
...
%prep
...
cp %{SOURCE3} .
...
%files
%license LICENSE %{npm_name}-%{version}-bundled-licenses.txt
If you have further questions refer to the Fedora Licensing Guidelines.
Using tarballs from the npm registry
The canonical method for shipping most node modules is tarballs from the npm registry. The Source0 for such modules should be of the form https://registry.npmjs.org/<modulename>/-/<modulename>-<version>.tgz. For instance:
Source0: https://registry.npmjs.org/npm/-/npm-1.1.70.tgz
This method should be preferred to using checkouts from git or automatically generated tarballs from GitHub.
These tarballs store the contents of the module inside a package directory, so every package using these tarballs should use the following in %prep:
%prep %setup -q -n package
Using tarballs for bundling
It is prefered to use tarballs for bundling node module dependencies. These tarballs should be independent from the main package source. There should be two tarballs. One for the binary, runtime package. One for testing while the package builds. This creates a smaller installed package.
These tarballs store the bundled modules in a directory called node_modules_prod, and node_modules_dev.
If your packages does not need one of the tarballs, then change these instructions accordingly. If it does not need the prod tarball, remove Source1 plus the %build and %install sections. If it does not need the dev tarball, remove Source2 and the %check section.
Note1: The setup of the prod and dev tarballs will soon become a macro. At the time of this writting, they are not.
Note2: The tarball with the dev dependencies needs to be unpacked in %check and not in %prep to avoid accidentally bundling the unpackaged dependencies that are only needed for testing.
...
Source1: %{npm_name}-%{version}-nm-prod.tgz
Source2: %{npm_name}-%{version}-nm-dev.tgz
...
%prep
...
# Configurar los módulos de Node.js de tiempo de ejecución (producción) agrupados
tar xfz %{SOURCE1}
mkdir -p node_modules
pushd node_modules
ln -s ../node_modules_prod/* .
ln -s ../node_modules_prod/.bin .
popd
...
%install
...
# Copiar los módulos de Node.js empaquetados
cp -pr node_modules node_modules_prod %{buildroot}%{nodejs_sitelib}/%{npm_name}
...
%check
%{__nodejs} -e 'require("./")'
# Configurar los módulos de desarrollo de Node.js incluidos para realizar pruebas.
# Nota: esto no puede estar en %%prep ni en dev node_modules
# puede entrar en las rpm normales
tar xfz %{SOURCE2}
pushd node_modules
ln -s ../node_modules_dev/* .
popd
pushd node_modules/.bin
ln -s ../../node_modules_dev/.bin/* .
popd
# Ejemplo de ejecución de prueba utilizando el binario en ./node_modules/.bin/
./node_modules/.bin/vows --spec --isolate
...
Instalar Módulos
Most node modules do not contain their own install mechanism. Instead they rely on npm to do it for them. npm must not be used to install modules in Fedora packages, since it usually requires network access.
Instead, install files in their proper place manually using install or cp. Most files should be installed in %{nodejs_sitelib}/ but documentation should be installed via %doc. In the event that the module ships arch independent content other than JavaScript code, that content should be installed in %{_datadir} and the module should be patched to cope with that.
Client-side JavaScript
Many node modules contain JavaScript that can be used both client-side and server-side and it is sometimes hard to identify code intended only for use in the browser. Since there are no current packaging guidelines for client-side JavaScript and bundling of such code is currently permitted in Fedora, it is currently permissible for client-side JavaScript to be bundled with nodejs modules in %{nodejs_sitelib}.
Automatic Requires and Provides
The nodejs package includes an automatic Requires and Provides generator that automatically adds versioned dependencies based on the information provided in a module’s and bundled dependencies package.json file. Additional Requires are added to native (binary) modules to protect against ABI breaks in Node or the V8 JavaScript runtime. Additional Provides: bundled() line is added for e
Provides npm
It also adds virtual provides in the form npm(<module name>) to identify modules listed in the npm registry (the module is listed at npmjs.org) . If a module is not listed in the npm registry, it must not provide this. Modules that aren’t listed in the npm registry should set private to true in their package.json file. If not, you must patch package.json to include that.
Provides bundled
It also automatically adds bundled provides in the form bundled(<bundled_module_name>) = <bundled_module_version> to identify bundled modules. Bundled modules must be in either the node_modules or node_modules_prod directories to be automatically added.
Correcting Dependencies
For non-bundled modules only.
Occasionally the dependencies listed in package.json are inaccurate. For instance, the module may work with a newer version of a dependency than the one explictly listed in the package.json file. To correct this, use the %nodejs_fixdep RPM macro. This macro should be used in %prep and will patch package.json to contain the correct dependency information.
To convert any dependency to not list a specific version, just call %nodejs_fixdep with the npm module name of the dependency. This changes the version in package.json to *. (Or adds one if it wasn’t already listed.) For example:
%prep %setup -q -n package %nodejs_fixdep foomodule
You can also specify a version:
%prep %setup -q -n package %nodejs_fixdep foomodule '>2.0'
The second argument to %nodejs_fixdep must be a valid package.json version specifier as explained in \`man npm json\++`.
You can also remove a dependency:
%prep %setup -q -n package %nodejs_fixdep -r foomodule
Symlinking Dependencies
For non-bundled modules only.
Node.js and npm require that dependencies explicitly be included or linked into a node_modules directory inside the module directory. To make this easier, a %nodejs_symlink_deps macro is provided and will automatically create a node_modules tree with symlinks for each dependency listed in package.json. This macro should be called in the %install section of every Node.js module package.
Building native modules with node-gyp
Most native modules use the node-gyp tool to build themselves, which configures and uses the gyp build framework to build addon modules that can interact with Node.js and the V8 JavaScript interpreter used by it.
The WAF build framework has been abandoned by upstream and is not supported in Fedora.
BuildRequires
To build a native module designed to be built with node-gyp, add BuildRequires: node-gyp, along with BuildRequires: nodejs-devel and -devel packages for any shared libraries needed by the module.
%build
Some native modules have Makefiles or other build processes that handle any special needs that module has, such as linking to system versions of dependencies. If present, these should be used. Check the module’s package.json file for information about what command npm will run to build these modules.
Most modules use vanilla node-gyp, and may not have build instructions in package.json. To build these, simply use the following:
%build
export CXXFLAGS="%{optflags}"
node-gyp rebuild
Note that some modules may specify something like node-gyp configure && node-gyp build. This is equivalent to node-gyp rebuild.
%install
node-gyp creates a shared object file with the extension .node as part of its build process, which should be located in build/Release. This file may be used as the main entry point for the library, or is utilized by JavaScript wrapper code included with the module.
If the shared object is used as the main entry point, it should be installed at %{nodejs_sitelib}/<module name>/index.node. The require() function will automatically load this if there is no corresponding index.js or entry point defined in package.json to override it. For example:
%install
mkdir -p %{buildroot}%{nodejs_sitelib}/foomodule
cp -p build/Release/index.node package.json %{buildroot}%{nodejs_sitelib}/foomodule/
If the shared object is called by JavaScript wrapper code, the situation is slightly more complicated.
If the module uses the npm bindings module, the shared object file should be installed in %{nodejs_sitelib}/<module name>/build/<module name>.node, which is at the top of bindings search path and where node-gyp usually creates a symlink to wherever the real shared object file exists. For example:
%install
mkdir -p %{buildroot}%{nodejs_sitelib}/foomodule/build
cp -p package.json wrapper.js %{buildroot}%{nodejs_sitelib}/foomodule/
cp -p build/Release/foomodule.node %{buildroot}%{nodejs_sitelib}/foomodule/build/
If the module hardcodes build/Release/<module name>.node, the module should be patched to use build/<module name>.node instead, and upstream should be advised that they should use the bindings module, because their module could break when users use debug builds of node.
If the module uses it’s own Makefiles to locate the shared object file(s) to a specific location, then those files should installed in that location.
Filtering Unwanted Provides
RPM automáticamente añade algunos proporcionan virtual no deseada para los archivos de objeto compartido con módulos nativos. Para retirarlos, añada %{?nodejs_default_filter} hasta la comienzo del archivo spec del paquete. Para más información, consulte Packaging:AutoProvidesAndRequiresFiltering.
Build testing in %check
All Node.js module spec files must include a %check section that contains (at minimum) the line:
%{__nodejs} -e 'require("./")'
This test ensures that the module is actually loadable, which will help avoid situations where a new upstream release has added a new dependency without the packager noticing.
Any other tests made available by upstream should also be run wherever possible.
For convienence, %nodejs_symlink_deps also accepts a --check argument, which will make it operate in the current working directory instead of the buildroot. You can use this in the %check section to make dependencies available for running tests. When this argument is used, development dependencies as listed in the "devDependencies" key in package.json are also linked.
Bundling Script
It is recommended to use the nodejs-packaging-bundler script found in the Fedora nodejs-packaging-bundler package. More documentation for it can be found at the Fedora nodejs-packaging repo .
Example Spec
%global npm_name tape
Name: nodejs-%{npm_name}
Version: 5.1.1
Release: 1%{?dist}
Summary: Tap-producing test harness for Node.js and browsers
License: MIT AND ISC
URL: https://github.com/substack/tape
Source0: https://registry.npmjs.org/%{npm_name}/-/%{npm_name}-%{version}.tgz
Source1: %{npm_name}-%{version}-nm-prod.tgz
Source2: %{npm_name}-%{version}-nm-dev.tgz
Source3: %{npm_name}-%{version}-bundled-licenses.txt
BuildArch: noarch
ExclusiveArch: %{nodejs_arches} noarch
Requires: nodejs
BuildRequires: nodejs-devel
%description
%{summary}.
%prep
%setup -q -n package
cp %{SOURCE3} .
# Setup bundled runtime(prod) node modules
tar xfz %{SOURCE1}
mkdir -p node_modules
pushd node_modules
ln -s ../node_modules_prod/* .
ln -s ../node_modules_prod/.bin .
popd
%build
#nothing to do
%install
mkdir -p %{buildroot}%{nodejs_sitelib}/%{npm_name}
cp -pr package.json index.js lib/ \
%{buildroot}%{nodejs_sitelib}/%{npm_name}
# Copy over bundled nodejs modules
cp -pr node_modules node_modules_prod \
%{buildroot}%{nodejs_sitelib}/%{npm_name}
mkdir -p %{buildroot}%{nodejs_sitelib}/tape/bin
install -p -D -m0755 bin/tape %{buildroot}%{nodejs_sitelib}/tape/bin/tape
mkdir -p %{buildroot}%{_bindir}
ln -sr %{nodejs_sitelib}/tape/bin/tape %{buildroot}%{_bindir}/tape
%check
%{__nodejs} -e 'require("./")'
# Setup bundled dev node_modules for testing
# Note: this cannot be in %%prep or the dev node_modules
# can get pulled into the regular rpm
tar xfz %{SOURCE2}
pushd node_modules
ln -s ../node_modules_dev/* .
popd
pushd node_modules/.bin
ln -s ../../node_modules_dev/.bin/* .
popd
# Run tests
./node_modules/.bin/tap test/*.js
%files
%doc readme.markdown
%license LICENSE %{npm_name}-%{version}-bundled-licenses.txt
%{nodejs_sitelib}/tape
%{_bindir}/tape
%changelog
Want to help? Learn how to contribute to Fedora Docs ›