Node.js Compatibility with Bare
Last updated
Last updated
Bare offers great compatibility with Node.js counterparts. Most of the modules and APIs used by developers are covered and supported.
assert
: (through npm:bare-node-assert)
buffer
: (through npm:bare-node-buffer)
child_process
: (through npm:bare-node-child-process)
console
: (through npm:bare-node-console)
crypto
: (through npm:bare-node-crypto)
events
: (through npm:bare-node-events)
fs
: (through npm:bare-node-fs)
http
: (through npm:bare-node-http)
https
: (through npm:bare-node-https)
inspector
: (through npm:bare-node-inspector)
module
: (through npm:bare-node-module)
os
: (through npm:bare-node-os)
path
: (through npm:bare-node-path)
process
: (through npm:bare-node-process)
readline
: (through npm:bare-node-readline)
repl
: (through npm:bare-node-repl)
stream
: (through npm:bare-node-stream)
timers
: (through npm:bare-node-timers)
tls
: (through npm:bare-node-tls)
tty
: (through npm:bare-node-tty)
url
: (through npm:bare-node-url)
util
: (through npm:bare-node-util)
worker_threads
: (through npm:bare-node-worker-threads)
zlib
: (through npm:bare-node-zlib)
To get the full Node.js compatible layer that Bare currently supports add the following lines to the package.json file.
NPM aliasing is a feature that allows developers to define custom names, or aliases, for their dependencies. This enables them to use a more intuitive or project-specific naming convention instead of relying on the package’s official name.
In your project’s package.json
file, use the alias
field to specify package aliases. For example:
After defining aliases, install the dependencies as usual:
Then, in the code import the aliased package:
fs
in a project using aliasesInit a new terminal project.
Replace the contents of index.js
with the following:
This will create a file called test.txt
and write a string to it. It will then read the file and log the content to the console.
Now run the file using Node.js.
This should output the following.
If we run the same project using Bare, it will fail with the MODULE_NOT_FOUND
error.
Add the following lines to the package.json
file.
This installs bare-fs
newer than ^2.1.5
. Then, adds alias fs
to the wrapper npm:bare-node-fs
.
The only thing the wrapper does is module.exports = require('bare-fs')
and at version *
, meaning the version that is specified is used.
Using the wrapper saves space as npm will only include bare-fs
once if something else installs it.
Now install the dependencies.
Now run the project using Bare.
This should output the following.
When writing a module that uses fs
the mapping can be specified directly in the module instead of relying on the compatible. This can be achieved using an 'import map'.
Let's take the fs
example and use import maps instead of aliases.
fs
in a project using import mapsStart a new terminal project
Change the import statement at the top:
Add the following fields to the package.json
:
Install the dependencies:
Now run the file using Node.js.
This should output the following.
Run the same file using Bare
It should log the following output:
This way the module is in full control of exactly which version of fs
is bound to Bare.
This is the best option, as it provides the best of both worlds. Node.js compatibility, but with full control of the dependencies.
If the project dependencies use core modules, use or to consume the Bare version of those modules.
Make sure is installed on your system. This can be checked by running node -v
in the terminal.
Now to make it compatible with Bare, we will create an alias for fs
in our project and install the respective Bare package to make it work again. In this case it's .
For example uses fs
and to work in both Bare and Node.js it adds the following import map to the package.json
file.
Replace contents of index.js
with the code from the NPM aliases .