To link library in migrations I use:

await deployer.deploy(MyTokenL)
await deployer.link(MyTokenL, MyToken)

We want to use migrations as infrastructure deployment and then have a deployment factory nodejs for tokens that use the infrastructure. I read that migrations are not ideal for this factory approach because migrations are not intended to deploy the same token multiple times.

So I have the deployed MyTokenL and want to link it to a new contract deployed without migrations via:

//how link existing library, there is no deployer available???
const newlyDeployedToken = await MyToken.new()

1 Answer 1


This is the gold:

const linkContractWithLibrary = async (
) => {
  const libraryName = LibraryArtifact.contractName;
  const libraryInstance = await LibraryArtifact.at(libraryAddress);

  //link function https://github.com/trufflesuite/truffle-contract/blob/develop/contract.js#L564
  ContractArtifact.link(libraryName, libraryInstance.address);

  console.log("=> Used lib " + libraryName + ":" + libraryInstance.address);

Use it like this:

const MyToken = artifacts.require("MyToken.sol");
const MyTokenL = artifacts.require("MyTokenL.sol");

const myTokenLAddress = "0x4921e3822Ff02E0FbfEF1CB906b785484ccaf074";

//linking existing library
await linkContractWithLibrary(

artifacts.require() is also only available in tests but I guess you can load the abi as first parameter and do the same trick...

  • This solved my problem :D
    – Sr.PEDRO
    Sep 4, 2020 at 2:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.