Moralis.enableWeb3 in nodejs

I want use Moralis in my node js

// In a node.js environment
const Moralis = require(‘moralis/node’);

When I want to enableWeb3 with this line

await Moralis.enableWeb3({ privateKey})

I am got this error in console

ReferenceError: window is not defined

what can be done with Moralis on the backend and what would you recommend to me in this situation?

Thanks in advance for your help

1 Like

the error is because the window object is only defined in browsers and node.js is a backend scripting language. and from the error it seems that moralis.enableWeb3 is depednant on the window onject which means i you cant can enable web3 like this unless its in the browser. the way you set up a provider in the likes of node is much different. you can npm install web3 and require it in your script and then set up a provider like this

 const provider = new Web3.providers.WebsocketProvider("http://127.0.0.1:8545")
    web3 = new Web3(provider);

this example is for web socket, which i prefer to use when writing scripts. you can connect by using infura as your provider if you want either

you have an example here:

1 Like

Thank you for your answer, do you have any example in node js for swapping ERC20 tokens?

by swapping you mean using 1Inch plugin or something else?

using 1InchPlugin if possible?

we don’t have an example, you can try to use it with a private key to see if it works

I am got this error when trying

there is this example for opensea plugin:

not sure if it helps you with something

you can also try to use liquidity pools directly