Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
dev-johnny-gh opened this issue Sep 6, 2023 · 11 comments
Closed

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

dev-johnny-gh opened this issue Sep 6, 2023 · 11 comments
Labels
bug Something isn't working released

Comments

@dev-johnny-gh
Copy link

dev-johnny-gh commented Sep 6, 2023

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 dev-johnny-gh added the bug Something isn't working label Sep 6, 2023
@dev-johnny-gh
Copy link
Author

@Rubilmax

@Rubilmax
Copy link
Owner

Rubilmax commented Sep 8, 2023

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
Copy link
Author

dev-johnny-gh commented Sep 11, 2023

@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
Copy link

🎉 This issue has been resolved in version 4.1.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

@s123121
Copy link

s123121 commented Oct 4, 2023

@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
Copy link
Owner

Rubilmax commented Oct 4, 2023

@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
Copy link

alxs commented Oct 4, 2023

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
Copy link
Owner

Rubilmax commented Oct 4, 2023

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

@alxs
Copy link

alxs commented Oct 4, 2023

Of course, thanks. Here they are:

package.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
{
  "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
Copy link
Owner

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
Copy link

alxs commented Oct 17, 2023

Amazing, thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working released
Projects
None yet
Development

No branches or pull requests

4 participants