question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SyntaxError: Cannot use 'import.meta' outside a module

See original GitHub issue

Filing this as a close duplicate of #9299 because that issue is locked, and with the release of Node v12, the createRequireFromPath boilerplate has become more widespread, and it involves import.meta.url:

import {createRequireFromPath as createRequire} from 'module';
import {fileURLToPath as fromURL} from 'url';
const require = createRequire(fromURL(import.meta.url));

As a Babel end-user, I don’t know how to work around the SyntaxError: Cannot use 'import.meta' outside a module, and leaving that issue open might allow someone more knowledgeable to offer a solution.

I personally find it mildly infuriating when conversations are locked without any spam or abuse problems, because nether I nor others can add anything that might help each other. Sometimes the “final word” from the core team is less than intelligible to those new to the topic.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
nicolo-ribaudocommented, Sep 15, 2019

That error isn’t thrown by Babel. Babel throws 'import' and 'export' may appear only with 'sourceType: "module"'

0reactions
naturetruecommented, Sep 17, 2019

maybe the error can be that i havent put the export as default export?

Read more comments on GitHub >

github_iconTop Results From Across the Web

[Bug]: SyntaxError: Cannot use 'import.meta' outside a module ...
import.meta is an ESM only feature and cannot be used in CJS (aka when using require ), so you need the files to...
Read more >
Uncaught SyntaxError: Cannot use 'import.meta' outside a ...
This has been asked already, and the answer is here. You just need to adjust your module.exports = { output: { uniqueName: "MyProj", ......
Read more >
Cannot use import statement outside a module' | HackerNoon
This error arises when we try to use import inside of a project which is not set up for modules - so let's...
Read more >
import.meta - JavaScript - MDN Web Docs
The import.meta meta-property is available in JavaScript modules; using import.meta outside of a module (including direct eval() within a ...
Read more >
How to fix "cannot use import statement outside a module"
This error occurs for one reason: you're trying to use import and you're not inside an ES module. It can happen in a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found