#Can't use `web3` package in Workers

1 messages · Page 1 of 1 (latest)

tardy locust
#

Hey, I'm trying to import the web3 package in my index.js but when I try to deploy I get the following error:
Error: Script startup exceeded CPU time limit. [code: 10021]

This is the only piece of code in the index.js:

import Web3 from "web3";

export default {
    async scheduled(controller, env, ctx) {
        // Nothing
    },
};

Is the web3 package too big? If so, is there anything I can do to solve this?

vocal salmon
tardy locust
#

So I'm forced to buy unbound?

#

(I don't mind but just want to know if it will solve the problem haha)

vocal salmon
#

You could maybe try importing only the parts you need: ```js
import { EthereumSupport } from "web3"; // made-up export

tardy locust
#

Hmm thanks, I'll look into it

tardy locust
#

Welp, looks like it's impossible to develop web3 applications on cloudflare workers. Even with a smaller package (Ethers.js) it won't boot.

Anyone else have any tips on how to develop a web3 app on workers?

vocal salmon
tardy locust
#

3 things, and I'll use Ethers.js in my example because that is the next best thing to Web3.js and a bit smaller. I want access my wallet, I need to set up a provider to communicate with the blockchain and I want to communicate with smart contracts through the provider.

In Ether.js I'd need the following dependencies:

import { JsonRpcProvider, Contract, Wallet, formatEther } from "ethers";

Even with only these 3 "big" and 1 "small" dependencies it wont work.

#

There's not really any package like those 2 that's smaller and does the same stuff

vocal salmon
tardy locust
#

I'll double check that right now

#

http

vocal salmon
tardy locust
#

yea it's just the size of the JsonRpcProvider, that adds like 400kb which makes the startup time take more than 200ms

vocal salmon
#

But yeah, a lot of packages are made for Node, where Memory isn't much(if any) of a concern. A lot of packages just won't work for that reason(not to say that there isn't some niche package hidden somewhere). In this case, if you really want to do this, you might just have to write your own system from scratch.

tardy locust
#

Ehh, I'll just have to accept it then. Do you know a place where I can upload a small piece of JS and trigger it with a cron job? I don't wanna mess with servers anymore

vocal salmon
tardy locust
#

oh those are some nice options, thanks man

tardy locust
#

@vocal salmon hey, just wanted to get back to you and say that apparently you can import on demand instead of importing at the top of the file. So right now I'm doing a const ethers = await import('ethers'); inside of the fetch/scheduled function of the worker. This ensures a sub 200ms boot time of the file while still being able to use huge libraries!