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

"ReferenceError: require is not defined" in imported *.mjs file #696

Closed
btakita opened this issue Jan 2, 2019 · 4 comments
Closed

"ReferenceError: require is not defined" in imported *.mjs file #696

btakita opened this issue Jan 2, 2019 · 4 comments
Labels

Comments

@btakita
Copy link

btakita commented Jan 2, 2019

I'm not sure how to configure esm to keep require & module defined in *.mjs files.

I tried setting { cjs: true, force: true }, but that does not seem to fix the error. I don't seem to understand the options & I tried almost every combination. What am I doing wrong?

@jdalton
Copy link
Member

jdalton commented Jan 2, 2019

Hi @btakita!

This is by design to avoid providing a false sense of comfort with the state of experimental .mjs support in Node. The doc note on it is:

🔒 .mjs files are limited to basic functionality without support for esm options.

If you're wanting better interop you'll want to use .js and you get it out-of-the-box without having to provide extra options.

@jdalton jdalton closed this as completed Jan 2, 2019
@btakita
Copy link
Author

btakita commented Jan 2, 2019

Thank you. I tried moving the require & import into a *.js file & got the following error: Error: Cannot load ES module from .mjs:

I just don't see how the *.js & *.mjs interop would work. This does work with rollup & webpack, however.

Is there any chance that we can have an option to relax the restriction for having require in *.mjs files? I'd rather be less pedantic to get my libraries working with my *.js scripts (which require *.mjs) being called using node. I can always update my code when the platform & libraries evolve.


edit: I have to call require in my mjs file due to this issue: aws/aws-sdk-js#1769

I can always use rollup for everything, however this is inconvenient when adding a one-off *.js script that loads *.mjs.

@jdalton
Copy link
Member

jdalton commented Jan 2, 2019

I just don't see how the *.js & *.mjs interop would work.

Oh I meant you need to rename your .mjs file to .js. Files that use .mjs run under strict rules that align with current experimental support in Node. As such, it's very limited in terms of interop. Your best bet for interop is to not use .mjs and stick with .js for your ES modules. You'll find it easier to work with in esm and bundlers to boot.

@jtibble
Copy link

jtibble commented Jul 31, 2019

I just don't see how the *.js & *.mjs interop would work.

Oh I meant you need to rename your .mjs file to .js. Files that use .mjs run under strict rules that align with current experimental support in Node. As such, it's very limited in terms of interop. Your best bet for interop is to not use .mjs and stick with .js for your ES modules. You'll find it easier to work with in esm and bundlers to boot.

Hey @jdalton this detailed information would be super-helpful on the README of esm and would save folks like me a lot of google searching and trying to understand why esm is treating .mjs files differently than expected.

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

No branches or pull requests

3 participants