Destiner / mev-inspect-js

A JS port of "mev-inspect-py" optimised for ease of use.
MIT License
132 stars 14 forks source link

Error [ERR_REQUIRE_ESM]: require() of ES Module #66

Open ethereal1m opened 1 year ago

ethereal1m commented 1 year ago

When executing: const inspector = new Inspector(1, provider);

I got error:

Error [ERR_REQUIRE_ESM]: require() of ES Module /home/myhome/Documents/ethereum/solidity/searcher/node_modules/mev-inspect/lib/index.js from /home/myhome/Documents/ethereum/solidity/searcher/index.ts not supported. Instead change the require of index.js in /home/myhome/Documents/ethereum/solidity/searcher/index.ts to a dynamic import() which is available in all CommonJS modules.

However if I add "type": "module",

in my package.json, I got error:

ReferenceError: exports is not defined in ES module scope

I am using nvm version 0.38.0

ethereal1m commented 1 year ago

reproduceable in 2.14 version. The following is my package.json

{
  "name": "searcher",
  "version": "1.0.0",
  "description": "searching",
  "main": "index.ts",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "ethereal",
  "license": "unlicensed",
  "dependencies": {
    "mev-inspect":"2.1.4",
    "ethers": "5.7.2"
  },
  "devDependencies": {
    "@types/node": "^18.14.2",
    "ts-node": "^10.9.1",
    "tslib": "^2.5.0",
    "typescript": "^4.9.5"
  }
}
Destiner commented 1 year ago

Hey! Could you please share a full example for reproduction? Also, could you please try running the examples following the instruction and see if that works?

ethereal1m commented 1 year ago

I am running nvm v19.7.0 with package.json from above. The following is the index.ts script that broke:

import { ethers } from 'ethers';
import { Inspector } from 'mev-inspect';

const key = 'xx-xxxxWPF22xxxJIdDzbDnUxxxx-xxx';
const provider = new ethers.providers.AlchemyProvider(1, key);
const inspector = new Inspector(1, provider);

It fails at "const inspector" statement.

Gaz0h commented 1 year ago

BUMP having the same issue

mehranhydary commented 1 year ago

Was able to import the inspector in a nest js app:

// Here's how you initialize
import { type Inspector } from 'mev-inspect';

let inspector: Inspector | undefined;

async function getInspector(): Promise<Inspector> {
  if (typeof inspector !== 'undefined') return inspector;
  const mod = await (eval(`import('mev-inspect')`) as Promise<
    typeof import('mev-inspect')
  >);
  return (inspector = new mod.Inspector(1, rpcProvider));
}
// and here's how I am using it in the app:
const mevInspector = await getInspector();