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

Error [ERR_REQUIRE_ESM]: Must use import to load ES #17063

Closed
yogeshgadge opened this issue Nov 15, 2017 · 6 comments
Closed

Error [ERR_REQUIRE_ESM]: Must use import to load ES #17063

yogeshgadge opened this issue Nov 15, 2017 · 6 comments
Labels
esm Issues and PRs related to the ECMAScript Modules implementation. question Issues that look for answers. wrong repo Issues that should be opened in another repository.

Comments

@yogeshgadge
Copy link

  • Version:
    "@std/esm@0.15.0"
  • Platform:
    node 9.2.0
  • Subsystem:

Question asked https://stackoverflow.com/q/47315280/2103767 trying to get node --experimental-module to work with resolve and stumbled across this.

$ node -r @std/esm apple.mjs
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: C:\Users\user\projs\dev\my-proj\test\es2015\test\apple.mjs
    at Module.load (module.js:560:32)
    at tryModuleLoad (module.js:503:12)
    at Function.Module._load (module.js:495:3)
    at Function.Module.runMain (module.js:682:10)
    at startup (bootstrap_node.js:191:16)

apple.mjs contents

// apple.mjs
import {banana} from './banana.mjs'

banana();

export default function apple() {}

@devsnek
Copy link
Member

devsnek commented Nov 15, 2017

afaik @std/esm can't integrate with the loader to do that but cc @jdalton on that point

@mscdex mscdex added esm Issues and PRs related to the ECMAScript Modules implementation. question Issues that look for answers. labels Nov 15, 2017
@jdalton
Copy link
Member

jdalton commented Nov 16, 2017

Hi @yogeshgadge!

This isn't really a Node issue.
In the future you can post questions like this to the @std/esm repo.

You're getting the error

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: 

because you need to opt-in to the @std/esm loader by making it a dep or by adding its options object to the package.json of apple.mjs. So after doing npm i --save @std/esm, when you run

node -r @std/esm apple.mjs

You'll get

/path/to/kale.js:1
export function kale() {}
^

SyntaxError: 'import' and 'export' may only be used in ES modules
    at /path/to/banana.mjs

This is because, by default, @std/esm follows Nodes proposed module rules.
To unlock CJS compat you simply add this option to your package.json

"@std/esm": "cjs"

Then node -r @std/esm apple.mjs will run without error.

@jdalton
Copy link
Member

jdalton commented Nov 16, 2017

@yogeshgadge

FWIW I've fixed the initial error message to be more helpful, so that in a future release when you don't have the @std/esm opt-in you'll get:

Error [ERR_REQUIRE_ESM]: Must opt-in @std/esm to load ES Module: ...

@Fishrock123
Copy link
Contributor

Note that that is not --experimental-modules.

If you wish to run it using node's built-in --experimental-module, you would do:

node --experimental-modules apple.mjs

@bnoordhuis
Copy link
Member

Closing since it's not an issue with node.js core.

@bnoordhuis bnoordhuis added the wrong repo Issues that should be opened in another repository. label Nov 16, 2017
@yogeshgadge
Copy link
Author

Sorry wrong repo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
esm Issues and PRs related to the ECMAScript Modules implementation. question Issues that look for answers. wrong repo Issues that should be opened in another repository.
Projects
None yet
Development

No branches or pull requests

6 participants