Security News
JSR Working Group Kicks Off with Ambitious Roadmap and Plans for Open Governance
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
prebuildify
Advanced tools
The prebuildify npm package is used to prebuild native Node.js modules for multiple platforms and architectures. It simplifies the process of distributing prebuilt binaries, which can save users from having to compile the module from source.
Prebuild Native Modules
This feature allows you to prebuild native modules for specified Node.js versions, platforms, and architectures. The code sample demonstrates how to prebuild a module for Node.js versions 14.17.0 and 16.0.0 on a Windows 64-bit platform.
const prebuildify = require('prebuildify');
prebuildify({
targets: ['node-14.17.0', 'node-16.0.0'],
platform: 'win32',
arch: 'x64'
});
Generate Prebuilds for Multiple Platforms
This feature allows you to generate prebuilds for multiple platforms. The code sample shows how to prebuild a module for Node.js versions 14.17.0 and 16.0.0 on Windows, Linux, and macOS platforms for 64-bit architecture.
const prebuildify = require('prebuildify');
prebuildify({
targets: ['node-14.17.0', 'node-16.0.0'],
platforms: ['win32', 'linux', 'darwin'],
arch: 'x64'
});
Custom Prebuild Scripts
This feature allows you to define custom scripts to run before and after the prebuild process. The code sample demonstrates how to add preinstall and postinstall scripts to the prebuild process.
const prebuildify = require('prebuildify');
prebuildify({
targets: ['node-14.17.0', 'node-16.0.0'],
platform: 'win32',
arch: 'x64',
scripts: {
preinstall: 'echo Preinstall script',
postinstall: 'echo Postinstall script'
}
});
node-pre-gyp is a tool that makes it easy to publish and install Node.js C++ addons from binaries. It provides a way to package and distribute prebuilt binaries, similar to prebuildify, but with additional features like S3 hosting and versioning.
prebuild is another tool for prebuilding binaries for Node.js native modules. It offers similar functionality to prebuildify, allowing developers to prebuild and distribute binaries for multiple platforms and architectures.
cmake-js is a tool that simplifies the process of building native Node.js modules using CMake. While it does not directly provide prebuilding capabilities, it can be used in conjunction with other tools to achieve similar results.
Create and package prebuilds for native modules
npm install -g prebuildify
First go to your native module and make a bunch of prebuilds
# got to your native module
cd your-native-module
# build for all electron/node binary versions and strip out symbols
prebuildify --all --strip
# the prebuilds will be stored in ./prebuilds
ls prebuilds
Then only remaining thing you need to do now is make your module use a prebuild if one exists for the platform/runtime you are using.
Use [https://github.com/mafintosh/node-gyp-build] to do this.
# first install node-gyp-build
npm install --save node-gyp-build
Then add node-gyp-build
as an install script to your module's package.json
{
"name": "your-native-module",
"scripts": {
"install": "node-gyp-build"
}
}
The install script will check if a compatible prebuild is bundled. If so it does nothing. If not it will run node-gyp rebuild
to produce a build.
This means that if the user using your module has disabled install scripts your module will still work (!) as long as a compatible prebuild is bundled.
When loading your native binding from your index.js
you should use node-gyp-build
as will to make sure to get the right binding
// Will load a compiled build if present or a prebuild.
// If no build if found it will throw an exception
var binding = require('node-gyp-build')(__dirname)
module.exports = binding
An added benefit of this approach is that your native modules will work across multiple node and electron versions without having the user need to reinstall or recompile them - as long as you produce prebuilds for all versions.
When publishing your module to npm remember to include the ./prebuilds
folder.
That's it! Happy native hacking.
MIT
FAQs
Create and package prebuilds for native modules
The npm package prebuildify receives a total of 403,403 weekly downloads. As such, prebuildify popularity was classified as popular.
We found that prebuildify demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Security News
Research
An advanced npm supply chain attack is leveraging Ethereum smart contracts for decentralized, persistent malware control, evading traditional defenses.
Security News
Research
Attackers are impersonating Sindre Sorhus on npm with a fake 'chalk-node' package containing a malicious backdoor to compromise developers' projects.