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

ESM index file should have .mjs extension #22

Closed
mattico opened this issue Oct 28, 2024 · 2 comments
Closed

ESM index file should have .mjs extension #22

mattico opened this issue Oct 28, 2024 · 2 comments

Comments

@mattico
Copy link

mattico commented Oct 28, 2024

Hello! Thank you for your work on this package.

I'm having an issue importing this package within an ESM project running on Node.js v20. This is the error:

/workspaces/data-portal/build/node_modules/.pnpm/[email protected]/node_modules/readdir-glob/dist/esm/index.js:1
import * as fs from 'fs';
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at wrapSafe (node:internal/modules/cjs/loader:1281:20)
    at Module._compile (node:internal/modules/cjs/loader:1321:27)
    at Module._extensions..js (node:internal/modules/cjs/loader:1416:10)
    at Module.load (node:internal/modules/cjs/loader:1208:32)
    at Module._load (node:internal/modules/cjs/loader:1024:12)
    at cjsLoader (node:internal/modules/esm/translators:348:17)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:297:7)
    at ModuleJob.run (node:internal/modules/esm/module_job:222:25)
    at async ModuleLoader.import (node:internal/modules/esm/loader:316:24)
    at async asyncRunEntryPointWithESMLoader (node:internal/modules/run_main:123:5)

It appears that the dist/esm/indes.js file is being loaded as a CommonJS module rather than an ESM one. I believe the issue is that the file uses a .js extension which node.js will load as a CommonJS module because the package.json file does not contain "type": "module". To fix this the extension for the ESM's index file should be changed to .mjs so that node.js knows it's an ESM.


Interestingly when I have the same import within a TypeScript project run via tsx this error doesn't occur. I believe that tsx uses its own module loader and detects the module type by looking for ESM import/export statements rather than relying on package.json and the file extension like node.js does.

@Yqnn
Copy link
Owner

Yqnn commented Oct 28, 2024

It should be fixed by #23 in v2.0.1 , let me know if you still encounter this problem.

@mattico
Copy link
Author

mattico commented Oct 28, 2024

Yes, that fixed it. Thank you for the fast response!

@mattico mattico closed this as completed Oct 28, 2024
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

2 participants