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.

[Bug]: SyntaxError: Cannot use 'import.meta' outside a module

See original GitHub issue

Version

27.4.5

Steps to reproduce

Use import.meta.url in code, such as SharedWorker:

const sharedWorker = new SharedWorker(new URL('path', import.meta.url), {
  type: 'module',
  name: 'whatever',
})

Expected behavior

I expect to be able to continue to run tests without import.meta.url failing.

Actual behavior

SyntaxError: Cannot use 'import.meta' outside a module appears in any test touching the file.

Additional context

Setting NODE_OPTIONS=--experimental-vm-modules does not work. Trying to mock out global.import.meta does not work. global.import.meta cannot be saved to a variable or exported from another file, nor the entire URL object.

Environment

System:
    OS: macOS 11.6.2
    CPU: (8) x64 Intel(R) Core(TM) i7-1068NG7 CPU @ 2.30GHz
  Binaries:
    Node: 14.17.0 - ~/.nvm/versions/node/v14.17.0/bin/node
    npm: 6.14.13 - ~/.nvm/versions/node/v14.17.0/bin/npm
  npmPackages:
    jest: ^27.4.5 => 27.4.5

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:11

github_iconTop GitHub Comments

3reactions
taniarasciacommented, Jan 3, 2022

This issue is not resolved natively, however I made a workaround:

Create a babel-jest.config.js that uses babel-plugin-transform-import-meta.

module.exports = {
  presets: ['@babel/preset-env', '@babel/preset-react'],
  plugins: ['@babel/transform-runtime', 'babel-plugin-transform-import-meta'],
}

Then add this to jest.config.js:

transform: {
    '\\.js$': ['babel-jest', { configFile: './babel-jest.config.js' }],
  },

Hopefully that helps anyone in the future.

0reactions
github-actions[bot]commented, Feb 4, 2022

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Uncaught SyntaxError: Cannot use 'import.meta' outside a ...
I created an angular nx monorepo and created two applications. When I start either of them I get the following error ...
Read more >
Cannot use import statement outside a module
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 >
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 >
Cannot use import statement outside a module [React ...
In this article, we talked about the SyntaxError: Cannot use import statement outside a module error in TypeScript and JavaScript. This error ......
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 >

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