What is bin-wrapper?
The bin-wrapper npm package is a utility that helps you download and manage binaries for your Node.js projects. It allows you to specify a binary, download it if necessary, and make it available for use in your project. This is particularly useful for ensuring that the correct version of a binary is used, regardless of the environment in which your code is running.
What are bin-wrapper's main functionalities?
Download and manage binaries
This feature allows you to specify different sources for binaries based on the operating system and architecture. The code sample demonstrates how to set up a BinWrapper instance to download the gifsicle binary for different platforms and run it to check its version.
const BinWrapper = require('bin-wrapper');
const path = require('path');
const base = 'https://raw.githubusercontent.com/imagemin/gifsicle-bin/main/vendor';
const bin = new BinWrapper()
.src(`${base}/macos/gifsicle`, 'darwin')
.src(`${base}/linux/x64/gifsicle`, 'linux', 'x64')
.src(`${base}/win/x64/gifsicle.exe`, 'win32', 'x64')
.dest(path.join(__dirname, 'vendor'))
.use(process.platform === 'win32' ? 'gifsicle.exe' : 'gifsicle');
bin.run(['--version']).then(() => {
console.log('Binary is working');
}).catch(err => {
console.error('Binary failed to run', err);
});
Other packages similar to bin-wrapper
node-pre-gyp
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 precompiled binaries, which can be downloaded and used without requiring users to compile the code themselves. Compared to bin-wrapper, node-pre-gyp is more focused on C++ addons and their distribution, whereas bin-wrapper is more general-purpose for managing any kind of binary.
nexe
nexe is a command-line utility that compiles your Node.js application into a single executable file. It includes the Node.js runtime and your application code, making it easy to distribute and run your application on different systems. While bin-wrapper focuses on downloading and managing external binaries, nexe is about packaging your Node.js application into a standalone binary.
pkg
pkg is a tool that packages Node.js projects into executable files for different platforms. It allows you to create a single executable that includes your application code and the Node.js runtime. Similar to nexe, pkg is focused on creating standalone executables, whereas bin-wrapper is about managing external binaries that your project depends on.
bin-wrapper
Binary wrapper for Node.js that makes your programs seamlessly available as local dependencies
Install
$ npm install --save bin-wrapper
Usage
var BinWrapper = require('bin-wrapper');
var bin = new BinWrapper({ version: 1.80, global: true });
bin
.src('https://raw.github.com/yeoman/node-jpegtran-bin/0.2.4/vendor/win/x64/jpegtran.exe', 'win32', 'x64')
.src('https://raw.github.com/yeoman/node-jpegtran-bin/0.2.4/vendor/win/x64/libjpeg-62.dll', 'win32', 'x64')
.dest('vendor')
.use('jpegtran.exe')
.run(['--version'], function (err) {
if (err) {
throw err;
}
console.log('jpegtran is working');
});
Get the path to your binary with bin.use
:
console.log(bin.use);
API
new BinWrapper(opts)
Creates a new BinWrapper
instance. Use the version
option to define a specific
version and the global
option to enable or disable global checking.
.src(str)
Accepts a URL pointing to a file to download.
.dest(str)
Accepts a path which the files will be downloaded to.
.use(str)
Define which file to use as the binary.
.run(cmd, cb)
Runs the search for the binary. If no binary is found it will download the file using the URL
provided in .src()
. It will also check that the binary is working by running it using cmd
and checking it's exit code.
Options
version
Type: String
Default: undefined
Define a specific version.
global
Type: Boolean
Default: true
Whether to check for a binary globally or not.
License
MIT © Kevin Mårtensson