curve-js icon indicating copy to clipboard operation
curve-js copied to clipboard

#err_require_esm issue

Open quan-unizen opened this issue 1 year ago • 9 comments

Hi guys,

I m trying to install the lib but always get issue like this? image

Any idea to fix it?

quan-unizen avatar Oct 02 '23 07:10 quan-unizen

i've got a similar issue with using this lib on vercel

Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/var/task/node_modules/@curvefi/api/lib/external-api.js' imported from /var/task/node_modules/@curvefi/api/lib/pools/utils.js
    at new NodeError (node:internal/errors:405:5)
    at finalizeResolution (node:internal/modules/esm/resolve:332:11)
    at moduleResolve (node:internal/modules/esm/resolve:995:10)
    at moduleResolveWithNodePath (node:internal/modules/esm/resolve:939:12)
    at defaultResolve (node:internal/modules/esm/resolve:1181:79)
    at nextResolve (node:internal/modules/esm/loader:163:28)
    at ESMLoader.resolve (node:internal/modules/esm/loader:835:30)
    at ESMLoader.getModuleJob (node:internal/modules/esm/loader:424:18)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:77:40)
    at link (node:internal/modules/esm/module_job:76:36) {
  code: 'ERR_MODULE_NOT_FOUND',
  page: '/pool'
}
Error: Runtime exited with error: exit status 1
Runtime.ExitError

ZiggStardust avatar Nov 22 '23 01:11 ZiggStardust

I got the same error, @quan-unizen were you able to solve it?

abhimanyu891998 avatar Dec 04 '23 06:12 abhimanyu891998

@Macket @fedorovdg tagging the top contributors to take a look at this, guys I'm getting the same thing and issue is old and still not fixed.

My Environment:

  • nodejs v18.19.1
  • hardhat v20.22.3 (also reproed on v20.20.1) with typescript
  • ts-node that comes with that v10.9.2

Setup is basic: install a fresh hardhat project with typescript, then add curvefi/api package. Write a sample .ts script and just import the api.

import curve from "@curvefi/api"; 

and then run using this command:

hh run src/test_curve.ts --no-compile

or if you don't have shorthand installed:

npx hardhat run src/test_curve.ts --no-compile

Observe the error:

Error [ERR_REQUIRE_ESM]: require() of ES Module /home/metamodern/github/{projectname}/node_modules/@curvefi/api/lib/index.js from /home/metamodern/github/{projectname}/src/test_curve.ts not supported.
Instead change the require of index.js in /home/metamodern/github/{projectname}/src/test_curve.ts to a dynamic import() which is available in all CommonJS modules.
    at require.extensions.<computed> [as .js] (/home/metamodern/github/{projectname}/node_modules/ts-node/dist/index.js:851:20)
    at /home/metamodern/github/{projectname}/src/test_curve.ts:26:65
    at async main (/home/metamodern/github/{projectname}/src/test_curve.ts:26:17) {
  code: 'ERR_REQUIRE_ESM'
}

We literally can't use your package because if this.

ts-node issue comments suggest operations with tsconfig and package.json files, but those broke the rest of hardhat project.

MetaMmodern avatar May 06 '24 21:05 MetaMmodern

I am also experiencing the same issue, I'm trying to integrate @curvefi/api in my nestjs backend (within an nx monorepo) and I got this error

Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/hadikhairallah/WebstormProjects/adapter-mono/apps/curve/webpack.config.js from /Users/hadikhairallah/WebstormProjects/{projectname}/node_modules/@nx/webpack/src/utils/webpack/resolve-user-defined-webpack-config.js not supported.
webpack.config.js is treated as an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which declares all .js files in that package scope as ES modules.
Instead rename webpack.config.js to end in .cjs, change the requiring code to use dynamic import() which is available in all CommonJS modules, or change "type": "module" to "type": "commonjs" in /Users/hadikhairallah/WebstormProjects/{projectname}/apps/curve/package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).

    at resolveUserDefinedWebpackConfig (/Users/hadikhairallah/WebstormProjects/{projectname}/node_modules/@nx/webpack/src/utils/webpack/resolve-user-defined-webpack-config.js:19:16)
    at getWebpackConfigs (/Users/hadikhairallah/WebstormProjects/{projectname}/node_modules/@nx/webpack/src/executors/webpack/webpack.impl.js:24:110)
    at webpackExecutor (/Users/hadikhairallah/WebstormProjects/{projectname}/node_modules/@nx/webpack/src/executors/webpack/webpack.impl.js:99:27)
    at webpackExecutor.next (<anonymous>)
    at /Users/hadikhairallah/WebstormProjects/{projectname}/node_modules/@nx/js/src/executors/node/node.impl.js:199:44 {
  code: 'ERR_REQUIRE_ESM'
}

Node.js v18.18.0

HadiKhai avatar May 24 '24 12:05 HadiKhai

@HadiKhai @MetaMmodern were you able to get it working? Same error, console Nodejs app.

TechGeorgii avatar Sep 11 '24 09:09 TechGeorgii

@HadiKhai @MetaMmodern were you able to get it working? Same error, console Nodejs app.

No

MetaMmodern avatar Sep 11 '24 09:09 MetaMmodern

It looks like that behavior started happening between @curvefi/[email protected] and @curvefi/[email protected] https://github.com/curvefi/curve-js/compare/v2.30.0...v2.30.1 It's not exactly what I'm expecting from a patch upgrade :see_no_evil:

More specifically it may have been introduced in this pull request https://github.com/curvefi/curve-js/pull/151 and possibly one of these changes:

  • package.json:
    • "type": "module"
  • tsconfig.json
    • "module": "ESNext"
    • "target": "es2020"

AndreMiras avatar Sep 11 '24 20:09 AndreMiras

My workaround (tested with both node 18 and node 20) is:

node --import "data:text/javascript,import {register} from 'node:module'; import {pathToFileURL} from 'node:url'; register('ts-node/esm', pathToFileURL('./'))" my-script.ts

More info there https://github.com/TypeStrong/ts-node/issues/1007#issuecomment-1864819206 Kinda ugly, but that did it for me for my dev setup.

AndreMiras avatar Sep 11 '24 21:09 AndreMiras

Thanks guys. I was able to solve it two ways: first when running by tsx like npx tsx src/index.ts (previously I used ts-node). Second – I set up webpack config and it built my app and it ran without problems.

UPD: I finally got rid of this problem. I was getting it when I added this library into my console application and my console app was CommonJs module, but the library is ESM (newest one). After I changed my app to ESM (that can work with CommonJs as well), it started building and working like charm.

TechGeorgii avatar Sep 12 '24 02:09 TechGeorgii