[Bug]: SyntaxError: Cannot use 'import.meta' outside a module
See original GitHub issueVersion
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:
- Created 2 years ago
- Comments:11
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This issue is not resolved natively, however I made a workaround:
Create a
babel-jest.config.js
that usesbabel-plugin-transform-import-meta
.Then add this to
jest.config.js
:Hopefully that helps anyone in the future.
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.