Share your Solidity library with the world
If you’ve written Solidity code, chances are you’ve used an existing contract/library, probably from a few openzeppelin
, Something like this:
import "@openzeppelin/contracts/utils/Strings.sol";
Today we will learn how to build our own Solidity library and publish it on npm.
Before writing any Solidity code, let’s create our directory first. I’m assuming you’re using Mac/Linux, so we can create our directory:
mkdir YOUR_DIR_NAME
suppose you have npm
Installed, we can now initialize an npm directory in it:
cd YOUR_DIR_NAME
npm init -y
Now that you have the repo, we can build our first Solidity library. required libraries in solidity library
keyword instead contract
define.
For this tutorial, we will create a library that contains only one function. This function will simply concatenate two strings together. Here’s the code I came up with:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;library Concatenator
function concatenate(string calldata _str, string calldata _str1)
public pure returns(string memory)
return string(abi.encodePacked(_str, _str1));
Once you are done, you can now publish this library by simply running:
npm publish
If you are not logged into a npm
account in your terminal, you may need to:
npm adduser
Complete! Now your library is available to millions of npm users around the world!
If you want to test your library, you can do it by writing a basic contract like:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;import "YOUR_DIR_NAME/Concatenator.sol";
contract Tester
using Concatenator for string;
function concatenate(string calldata _str, string calldata _str1) public pure returns(string memory)
return _str.concatenate(_str1);