-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
[Bug]: SyntaxError: Cannot use 'import.meta' outside a module #12183
Comments
There is no such thing as However, using |
Hi @SimenB, thanks for getting back to me. Looking at the steps in the linked article, if I follow step one:
Changing it to
Of course, if I remove any import it will just keep going down the line and fail anywhere there's an import. Are there instructions for "otherwise configure your transformer to emit ESM rather than the default CommonJS"? For step two:
Trying either version of those options makes no difference to the tests. They still fail at For step three:
I would prefer not to use Is there some other form that the |
Same as running files with |
A small reproduction of your setup and config would allow me to confirm what the issue is, at least 🙂 |
@SimenB Thanks for the quick response. Changing
So if I change
In every test file. Attempting to With the original way I had it, those requires worked fine, all tests using Is there no way for tests to pass with |
I'll work on a getting a small repo up for it. |
@SimenB Here's a repo, running |
@SimenB Any idea about this? I made a repro (https://github.com/taniarascia/jest-issue) but I still can't figure it out. Is it impossible to do what I'm attempting to do? Unfortunately I can't update to webpack 5 until I figure out this issue. |
This issue is not resolved natively, however I made a workaround: Create a module.exports = {
presets: ['@babel/preset-env', '@babel/preset-react'],
plugins: ['@babel/transform-runtime', 'babel-plugin-transform-import-meta'],
} Then add this to transform: {
'\\.js$': ['babel-jest', { configFile: './babel-jest.config.js' }],
}, Hopefully that helps anyone in the future. |
Hi there!
One thing you can do is rename it to tl;dr: if you wanna use That said, I think Jest does the correct thing here (under the restraints Node's implementation of ESM places on us). If you have ideas for how we can improve our docs I'd be happy to tweak them, but I don't think there's a bug here, so I'll close it. Thanks for taking the time to put together a repository so I could investigate! |
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. |
Version
27.4.5
Steps to reproduce
Use
import.meta.url
in code, such asSharedWorker
: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
The text was updated successfully, but these errors were encountered: