Node.js Compatibility with Bare
Bare offers great compatibility with Node.js counterparts. Most of the modules and APIs used by developers are covered and supported.
Currently supported modules
assert
:bare-assert
(throughnpm:bare-node-assert)
buffer
:bare-buffer
(throughnpm:bare-node-buffer)
child_process
:bare-subprocess
(throughnpm:bare-node-child-process)
console
:bare-console
(throughnpm:bare-node-console)
crypto
:bare-crypto
(throughnpm:bare-node-crypto)
events
:bare-events
(throughnpm:bare-node-events)
fs
:bare-fs
(throughnpm:bare-node-fs)
http
:bare-http1
(throughnpm:bare-node-http)
https
:bare-https
(throughnpm:bare-node-https)
inspector
:bare-inspector
(throughnpm:bare-node-inspector)
module
:bare-module
(throughnpm:bare-node-module)
os
:bare-os
(throughnpm:bare-node-os)
path
:bare-path
(throughnpm:bare-node-path)
process
:bare-process
(throughnpm:bare-node-process)
readline
:bare-readline
(throughnpm:bare-node-readline)
repl
:bare-repl
(throughnpm:bare-node-repl)
stream
:bare-stream
(throughnpm:bare-node-stream)
timers
:bare-timers
(throughnpm:bare-node-timers)
tls
:bare-tls
(throughnpm:bare-node-tls)
tty
:bare-tty
(throughnpm:bare-node-tty)
url
:bare-url
(throughnpm:bare-node-url)
util
:bare-utils
(throughnpm:bare-node-util)
worker_threads
:bare-worker
(throughnpm:bare-node-worker-threads)
zlib
:bare-zlib
(throughnpm:bare-node-zlib)
Config for the Node.js stdlib
To get the full Node.js compatible layer that Bare currently supports add the following lines to the package.json file.
Consuming dependencies that use core Node.js modules
If the project dependencies use core Node.js modules, use NPM aliases or import maps to consume the Bare version of those modules.
Consuming dependencies using NPM Aliases
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:
Example of using fs
in a project using aliases
fs
in a project using aliasesMake sure Node.js is installed on your system. This can be checked by running
node -v
in the terminal.
Start a new project
Init 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.
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 bare-fs.
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.
Consuming dependencies using Import Maps
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'.
For example Localdrive uses fs
and to work in both Bare and Node.js it adds the following import map to the package.json
file.
Let's take the fs
example and use import maps instead of aliases.
Example of using fs
in a project using import maps
fs
in a project using import mapsStart a new terminal project
Replace contents of index.js
with the code from the NPM aliases example.
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.
Last updated