enableWeb3 return "window is not defined"

I’m trying to execture some functions from node.js env.

const Moralis = require(“moralis/node”)
import { utils } from “ethers”
import { moralisServer, ABI } from “./moralisClient”


class MoralisService {
  async connectBlockchain() {
    try {
      await Moralis.start(moralisServer)
      const _id =
        process.env.ENV == "development"
          ? Number(process.env.BLOCKCHAIN_TESTNET_ID)
          : Number(process.env.BLOCKCHAIN_MAINNET_ID)

      const chainId = utils.hexValue(_id)
      await Moralis.enableWeb3({
        chainId: Number(chainId),
        privateKey: process.env.WALLET_PK,
      })
      return Moralis
    } catch (error) {
      console.log('err',error)
      return false
    }
  }

errors:

err ReferenceError: window is not defined
    at InjectedWeb3Connector.verifyEthereumBrowser (/Users/connected/_dev/test/server/node_modules/moralis/lib/node/Web3Connector/InjectedWeb3Connector.js:66:5)
    at InjectedWeb3Connector.activate (/Users/connected/_dev/test/server/node_modules/moralis/lib/node/Web3Connector/InjectedWeb3Connector.js:72:10)
    at InternalWeb3Provider.activate (/Users/connected/_dev/test/server/node_modules/moralis/lib/node/InternalWeb3Provider.js:72:30)
    at Function.enableWeb3 (/Users/connected/_dev/test/server/node_modules/moralis/lib/node/MoralisWeb3.js:216:45)

any ideas why it’s trying to look for browsers’ window if I explicitly import node.js version?

You have to use enableWeb3 with a private key if you want to use it in nodejs

how is that part


 await Moralis.enableWeb3({
        chainId: Number(chainId),
        privateKey: process.env.WALLET_PK,
      })

not addressing your comment?
Did you even read the whole question my friend?

This was working fine for me:

1 Like

man, I know how to set it up - but it is not working for some reason, thus this post.
The documentation is pretty clear, but there is something wrong with the set up.

can you paste minimal code example that replicates that error?

I literally did it in the first post.

that is quite complex, with multiple imports, on what line gives that error?

dude, sorry for that but are you bot or just trying to spam as many posts as possible?
You have stacktrace in the first post. It’s obvious it came from enableWeb3

that line is not from the code that you pasted, I don’t know which line generates that error in the original code that you pasted.

You can post a minimal code to replicate the error, not a complex one. I can not test that code that you pasted.

1 Like

You can share a repo.

Are you using your moralisSecret in Moralis.start() from moralisServer?