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 #127

Closed
calvinh8 opened this issue Oct 4, 2023 · 7 comments · Fixed by #128
Closed

ERR_REQUIRE_ESM #127

calvinh8 opened this issue Oct 4, 2023 · 7 comments · Fixed by #128

Comments

@calvinh8
Copy link

calvinh8 commented Oct 4, 2023

I have "countries-list": "3.0.5" installed in my Turbo setup under the constants workspace in the packages folder. I don't have type specified in my packages/constants/package.json.

packages/constants/src/index.ts

import { getCountryData } from 'countries-list'

console.log(getCountryData('TW'))

packages/constants/tsconfig.json

{
  "compilerOptions": {
    "lib": ["ES2015"],
    "module": "CommonJS",
    "outDir": "./dist",
    "rootDir": "./src"
  },
  "exclude": ["node_modules"],
  "extends": "tsconfig/base.json",
  "include": ["src"]
}

Output:

$ ts-node --esm packages/constants/src/index.ts

/xxx/Library/pnpm/global/5/.pnpm/[email protected]_@[email protected][email protected]/node_modules/ts-node/dist/index.js:851
            return old(m, filename);
                   ^
Error [ERR_REQUIRE_ESM]: require() of ES Module /xxx/node_modules/.pnpm/[email protected]/node_modules/countries-list/index.min.js from /xxx/packages/constants/src/index.ts not supported.
index.min.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 index.min.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 /xxx/node_modules/.pnpm/[email protected]/node_modules/countries-list/package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).

Attempt 1

$ ts-node --esm packages/constants/src/index.ts

TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /xxx/packages/constants/src/index.ts

Attempt 2

Tried changing a few config based on the article here but no luck https://javascript.plainenglish.io/how-to-correctly-use-typescript-module-import-syntax-and-settings-in-various-circumstances-e98bfa87f70f

Attempt 3 (Worked)

I tried changing "type": "module" to "type": "commonjs" in /xxx/node_modules/.pnpm/[email protected]/node_modules/countries-list/package.json and that worked great for me. But this change will not persist since it's modifying node_modules.

Is there a permanent workaround to properly import your module and run it in TypeScript?

@dmythro
Copy link
Member

dmythro commented Oct 5, 2023

This is weird, I tested on one of my projects with turbo and it worked without any issues. Don't think I used ts-node though.

Thanks, gonna check it out.

@calvinh8
Copy link
Author

calvinh8 commented Oct 5, 2023

This is weird, I tested on one of my projects with turbo and it worked without any issues. Don't think I used ts-node though.

Thanks, gonna check it out.

I was trying to test the code first before moving on and saw the error. That was why I used ts-node to run the ts file to see.

@ciriousjoker
Copy link

Same issue here, although it's in the context of firebase cloud functions.

Deployment fails with this error:

Got response code 400; body Failed to generate manifest from function source: Error [ERR_REQUIRE_ESM]: require() of ES Module /redacted/node_modules/countries-list/index.min.js from /redacted/dist/apps/redacted-some-own-file.js not supported.
index.min.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 index.min.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 /redacted/node_modules/countries-list/package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).

@dmythro
Copy link
Member

dmythro commented Oct 20, 2023

So, the package is built the way that .js is a CJS file for require(), .mjs is ESM for import. You can check packages/countries/tsup.config.ts with build configuration and dist/package.json of this repo which describe all of that. Why this error happens I'm not sure yet, maybe I am missing something.

I didn't have any issues on any of my projects/monorepos so far. I don't really want to change module type as mowing towards ESM is the way now.

Open for suggestions, maybe I am missing something. Maybe changing (or removing) package "type" could fix the issue, I'll check it out.

@dmythro
Copy link
Member

dmythro commented Oct 20, 2023

Created a PR #128, could you please try out v3.0.6? Should fix the issue.
@ciriousjoker @calvinh8

@ciriousjoker
Copy link

@dmythro
I refactored some stuff and ran into a different error message:

Got response code 400; body Failed to generate manifest from function source: SyntaxError: Named export 'countries' not found. The requested module 'countries-list' is a CommonJS module, which may not support all module.exports as named exports.

After updating, this error went away. Thanks a lot!
I assume the original error I commented about had the same underlying issue.

@dmythro
Copy link
Member

dmythro commented Oct 22, 2023

Great! Glad it's alright now. Please fire an issue if there will be one. I didn't release hybrid ESM/CJS packages before this one, so thanks for reaching out.

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

Successfully merging a pull request may close this issue.

3 participants