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

#err_require_esm issue #202

Open
quan-unizen opened this issue Oct 2, 2023 · 9 comments
Open

#err_require_esm issue #202

quan-unizen opened this issue Oct 2, 2023 · 9 comments

Comments

@quan-unizen
Copy link

Hi guys,

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

Any idea to fix it?

@ZiggStardust
Copy link

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

@abhimanyu891998
Copy link

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

@MetaMmodern
Copy link

MetaMmodern commented May 6, 2024

@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.

@HadiKhai
Copy link

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

@TechGeorgii
Copy link

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

@MetaMmodern
Copy link

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

No

@AndreMiras
Copy link

It looks like that behavior started happening between @curvefi/api@2.31.0 and @curvefi/api@2.31.1
v2.30.0...v2.30.1
It's not exactly what I'm expecting from a patch upgrade 🙈

More specifically it may have been introduced in this pull request #151 and possibly one of these changes:

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

@AndreMiras
Copy link

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 TypeStrong/ts-node#1007 (comment)
Kinda ugly, but that did it for me for my dev setup.

@TechGeorgii
Copy link

TechGeorgii commented Sep 12, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants