Swarm.js: Decentralized Storage Client
Swarm.js is a JavaScript library designed to facilitate interaction with the Swarm network, a decentralized storage platform. The package provides functionalities for uploading and downloading files and directories to and from Swarm, as well as hashing various data types. It specifically targets both Node.js and browser environments for these operations. The current stable version appears to be 0.1.42, indicating it never reached a 1.0 release. The library's development has ceased, with the maintainer explicitly stating it is no longer actively maintained and recommending Erebos (https://erebos.js.org/) as an alternative. Therefore, users should be aware of its abandoned status and consider alternatives for new projects, or for maintaining existing ones.
Common errors
-
TypeError: swarm.at is not a function
cause Attempting to import `at` as a named export (`import { at } from 'swarm-js';`) or incorrectly calling `require('swarm-js')` without chaining `.at()` immediately.fixEnsure you are using the CommonJS `require` syntax and calling the `.at()` method immediately: `const swarm = require('swarm-js').at('http://swarm-gateways.net');` -
ERR_REQUIRE_ESM
cause Attempting to use `require()` in an ES Module context, or using `import` syntax directly with this CommonJS-only library without proper transpilation.fixChange `import` statements to `const swarm = require('swarm-js').at(...)` or configure your build process to transpile CommonJS modules for ESM consumption in an `ESM` project. If in an ES Module environment, you might need a wrapper or dynamic import if `require` is not available. -
Error: connect ECONNREFUSED
cause The Swarm gateway specified in `.at()` is unreachable, offline, or refusing connections from your environment.fixVerify the Swarm gateway URL is correct and the gateway is operational and accessible from your network. Try a different public gateway URL if available, or ensure your firewall/network settings allow the connection.
Warnings
- breaking This library is explicitly unmaintained and abandoned by its creator. No further updates, bug fixes, or security patches will be released, making it unsuitable for production use.
- gotcha The package is still in a pre-1.0 version (0.1.42), indicating it was never considered stable by its original maintainer. This implies potential API instability, incomplete features, and lack of long-term support.
- gotcha The primary usage pattern shown is CommonJS (`require`). Modern Node.js and browser environments typically prefer ES Modules (`import`). Direct ESM compatibility is not guaranteed without bundling or transpilation, and using `import` directly may lead to errors.
Install
-
npm install swarm-js -
yarn add swarm-js -
pnpm add swarm-js
Imports
- swarm (configured instance)
import { at } from 'swarm-js';const swarm = require("swarm-js").at("http://swarm-gateways.net"); - swarm.upload
import { upload } from 'swarm-js';swarm.upload(data)
- swarm.download
import { download } from 'swarm-js';swarm.download(hash)
- swarm.hash
import { hash } from 'swarm-js';swarm.hash(value)
Quickstart
const swarm = require("swarm-js").at("http://swarm-gateways.net");
const fileContent = "Hello Swarm! This is a test file.";
swarm.upload(fileContent)
.then(hash => {
console.log("Uploaded file. Address:", hash);
return swarm.download(hash);
})
.then(downloadedArray => {
console.log("Downloaded file content:", swarm.toString(downloadedArray));
})
.catch(error => {
console.error("An error occurred:", error);
});
const dir = {
"/hello.txt": { type: "text/plain", data: "Hello from directory!" },
"/meta.json": { type: "application/json", data: JSON.stringify({ version: "1.0" }) }
};
swarm.upload(dir)
.then(dirHash => {
console.log("Uploaded directory. Address:", dirHash);
return swarm.download(dirHash);
})
.then(downloadedDir => {
console.log("Downloaded directory:");
for (let path in downloadedDir) {
console.log(`- ${path}: ${downloadedDir[path].data.toString()}`);
}
})
.catch(error => {
console.error("Directory operation failed:", error);
});