Rubilmax / ethers-multicall-provider

⚡🕰️ Call any set of functions from any set of smart contracts in a single RPC query, seamlessly using ethers' providers API!
https://github.com/Rubilmax/ethers-multicall-provider
MIT License
62 stars 7 forks source link

Can we remove "type": "module" from package.json #51

Closed dev-johnny-gh closed 1 year ago

dev-johnny-gh commented 1 year ago

After I upgraded this package to 4.1.0 (for using ethers v6), I got ERR_REQUIRE_ESM error on ts-node:

Error [ERR_REQUIRE_ESM]: require() of ES Module ... not supported.

It is caused by the "type": "module" on the package.json. My project is not an ES module project and it's hard to upgrade it to an ES module project, I got a lot of errors when I was trying to use the ES module on my project.

The current situation of the ES module on the node ecosystem is chaotic and fucked up.

So, can we remove the type: module restriction so that I can use the commonjs module resolution on typescript to import this package?

dev-johnny-gh commented 1 year ago

@Rubilmax

Rubilmax commented 1 year ago

Hi, I'll look into it next week as I can't ATM sorry I don't think we can because ethers-v6 actually forces this but I need to test some things

dev-johnny-gh commented 1 year ago

@Rubilmax what if we use some bundlers to output both esm and cjs dist? you can try to use it on ts-node with cjs project. then you should be able to reproduce it. if you can manage to avoid the error, let me know.

github-actions[bot] commented 1 year ago

:tada: This issue has been resolved in version 4.1.1 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

s123121 commented 1 year ago

@Rubilmax Removing this cause problem with esm project though

import { MulticallWrapper } from 'ethers-multicall-provider';
         ^^^^^^^^^^^^^^^^
SyntaxError: Named export 'MulticallWrapper' not found. The requested module 'ethers-multicall-provider' is a CommonJS module, which may not support all module.exports as named exports.
(node:61259) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
export { default as MulticallWrapper } from "./multicall-provider";
^^^^^^
Rubilmax commented 1 year ago

@Rubilmax Removing this cause problem with esm project though

import { MulticallWrapper } from 'ethers-multicall-provider';
         ^^^^^^^^^^^^^^^^
SyntaxError: Named export 'MulticallWrapper' not found. The requested module 'ethers-multicall-provider' is a CommonJS module, which may not support all module.exports as named exports.
(node:61259) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
export { default as MulticallWrapper } from "./multicall-provider";
^^^^^^

What's the framework you use? Your setup? I think in this case, you should use a bundler to convert ethers-multicall-provider to an esm module

alxs commented 1 year ago

It still doesn't seem to work well with cjs projects. Here's what I get:

[project-dir]/node_modules/ethers-multicall-provider/lib/index.js:1
export { default as MulticallWrapper } from "./multicall-provider";
^^^^^^

SyntaxError: Unexpected token 'export'
    at internalCompileFunction (node:internal/vm:73:18)
    at wrapSafe (node:internal/modules/cjs/loader:1178:20)
    at Module._compile (node:internal/modules/cjs/loader:1220:27)
    at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
    at Object.require.extensions.<computed> [as .js] (/Users/aleixo/Downloads/rfq-service/node_modules/ts-node/src/index.ts:1608:43)
    at Module.load (node:internal/modules/cjs/loader:1119:32)
    at Function.Module._load (node:internal/modules/cjs/loader:960:12)
[...]
Rubilmax commented 1 year ago

Can you provide me with more details on your setup pls? Package.json, tsconfig.json

alxs commented 1 year ago

Of course, thanks. Here they are:

package.json ```json { "name": "rfq-service", "version": "1.0.0", "description": "", "main": "main.ts", "directories": { "doc": "docs" }, "dependencies": { "@types/express": "^4.17.13", "@types/node": "^18.16.0", "axios": "^1.5.1", "dotenv": "^16.3.1", "ethers": "^6.7.1", "ethers-multicall-provider": "^4.2.0", "express": "^4.18.2", "reflect-metadata": "^0.1.13", "typedi": "^0.10.0", "typescript": "^5.2.2", "winston": "^3.10.0", "winston-loki": "6.0.5" }, "devDependencies": { "@types/chai": "^4.3.6", "@types/mocha": "^9.1.1", "chai": "^4.3.10", "mocha": "^10.0.0", "nock": "^13.3.3", "patch-package": "^8.0.0", "prettier": "^3.0.3", "ts-node": "^10.9.1" }, "scripts": { "start": "ts-node src/main.ts" } ... } ```
tsconfig.json ```json { "include": [ "**/*.ts" ], "exclude": [], "compilerOptions": { "types": [ "node", "mocha" ], "incremental": true, /* Enable incremental compilation */ "target": "ES6", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */ "module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */ "importHelpers": true, /* Import emit helpers from 'tslib'. */ "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */ "strict": true, /* Enable all strict type-checking options. */ "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */ "esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */ "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */ "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */ "skipLibCheck": true, /* Skip type checking of declaration files. */ "forceConsistentCasingInFileNames": true, /* Disallow inconsistently-cased references to the same file. */ "resolveJsonModule": true, } } ```
Rubilmax commented 1 year ago

Sorry for the delay, it should be good with v5. It wasn't an easy one: the fix was to use module: "nodenext" & moduleResolution: "nodenext"

alxs commented 1 year ago

Amazing, thanks a lot!