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't import CommonJS module from dist/index.js:133 #1

Closed
chrisdotcode opened this issue Aug 16, 2024 · 6 comments · Fixed by #3
Closed

Can't import CommonJS module from dist/index.js:133 #1

chrisdotcode opened this issue Aug 16, 2024 · 6 comments · Fixed by #3

Comments

@chrisdotcode
Copy link

Hi! Thanks for the library. After making sure that my tsconfig.json contains the following:

{
  "compilerOptions": {
    "types": ["node"],
    "target": "es6",
    "lib": ["es2015", "dom"],
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "moduleResolution": "bundler"
  }
}

a mere import in my src/index.js script of (with an invocation of node src/index.js (after TypeScript compilation)) of the following:

import { parse, transform } from "parse-ical";

gives me the following error:

file:///<...snip...>/node_modules/parse-ical/dist/index.js:133
import { rrulestr } from "rrule";
         ^^^^^^^^
SyntaxError: Named export 'rrulestr' not found. The requested module 'rrule' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from 'rrule';
const { rrulestr } = pkg;

    at ModuleJob._instantiate (node:internal/modules/esm/module_job:171:21)
    at async ModuleJob.run (node:internal/modules/esm/module_job:254:5)
    at async onImport.tracePromise.__proto__ (node:internal/modules/esm/loader:482:26)
    at async asyncRunEntryPointWithESMLoader (node:internal/modules/run_main:117:5)

Am I missing something additional, or is there a deeper error here?

Thank you!

@chrisdotcode
Copy link
Author

chrisdotcode commented Aug 16, 2024

Changing that line 133 to the suggested:

import pkg from 'rrule';
const { rrulestr } = pkg;

seems to solve the issue, but I know that that's not a good long-term fix.

@JaninaWibker
Copy link
Owner

JaninaWibker commented Aug 16, 2024

Interesting error, I haven't had this before.
That said, bundling and having to deal with ESM / CJS and typescript is a huge chore and very easy to get wrong with just a few missing / incorrect config options.

I'm not entirely sure what your setup is and if you use tools such as tsup for bundling your typescript. This has worked well for me in the past even when wanting to output commonjs.
My usual setup involves using tsc for typechecking, tsup for transpiling/bundling and using esm whereever possible (but maybe outputting cjs at the very end if need be).

I tried around a fair bit and am not if I correctly fixed the issue, but here are a few findings I made:

  • https://arethetypeswrong.github.io/?p=parse-ical%401.0.4 is a cool tool for diagnosing such issues and the linked explanations are great
  • typescript and esm/cjs are a huge mess sometimes
  • there are different type definition files for esm and cjs. They contain the same content but differ in filename (which is very important to typescript): index.d.ts (esm) and index.d.cts (cjs)
  • .exports["."].require.types (package.json) should reference index.d.cts instead of index.d.ts
  • node10 (aliased to node; value of moduleResolution) doesn't support the exports field, but can use the main & types fields of the package.json
    • this sounds like something that could fix the problem, but I'm not sure if this messes with the other module resolution strategies. If it does the newer strategies take precedence over node10 / node
  • tsc index.js doesn't use the tsconfig.json file in the same directory, for that you'd have to use tsc --project . :)

I'd be interested in the exact setup (mostly how bundling/transpiling is done in your project)

Edit: I noticed that those changes don't really fix your problem which I just got to being able to reproduce, but fix issues I had on the way of trying to get there

@JaninaWibker
Copy link
Owner

Testing around a bit more I stumbled upon this issue.

It seems like your fix is the most easy solution, even though it isn't the most elegant; I'll go ahead and introduce all of the fixes required

@JaninaWibker
Copy link
Owner

[email protected] should work properly now (hopefully) @chrisdotcode

@chrisdotcode
Copy link
Author

chrisdotcode commented Aug 17, 2024

Thanks for the lightning-quick responses! Will have some time to test within the next few days and let you know!

@chrisdotcode
Copy link
Author

chrisdotcode commented Aug 18, 2024

@JaninaWibker, confirmed working on:

$ deno --version
deno 1.45.5 (release, aarch64-apple-darwin)
v8 12.7.224.13
typescript 5.5.2

and "parse-ical": "npm:parse-ical@^1.2.0"

Thank you!

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.

2 participants