(TSDX Jest) SyntaxError: Cannot use import statement outside a module
See original GitHub issueHi there,
I am using node-fetch api and when I run yarn run test
, I get the following error:
Test suite failed to run
Jest encountered an unexpected token
This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.
By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".
Here's what you can do:
• To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
• If you need a custom transformation specify a "transform" option in your config.
• If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.
You'll find more details and examples of these config options in the docs:
https://jestjs.io/docs/en/configuration.html
Details:
/Users/ssaurabh/Projects/typescript-images-api/node_modules/node-fetch/src/index.js:9
import http from 'http';
^^^^^^
SyntaxError: Cannot use import statement outside a module
> 1 | import fetch from 'node-fetch';
| ^
2 | import { RequestInit, Response } from 'node-fetch';
3 |
4 | class FetchProxy {
at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1258:14)
at Object.<anonymous> (src/FetchProxy.ts:1:1)
Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 3.166s
Ran all test suites.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
environment
System:
OS: macOS 11.5.2
CPU: (8) x64 Intel(R) Core(TM) i7-7920HQ CPU @ 3.10GHz
Memory: 125.96 MB / 16.00 GB
Shell: 5.8 - /bin/zsh
Binaries:
Node: 12.22.4 - ~/.nvm/versions/node/v12.22.4/bin/node
Yarn: 1.22.10 - /usr/local/bin/yarn
npm: 7.20.3 - ~/.nvm/versions/node/v12.22.4/bin/npm
Browsers:
Chrome: 93.0.4577.82
Firefox: 89.0.2
Safari: 14.1.2
npmPackages:
tsdx: ^0.14.1 => 0.14.1
typescript: ^4.4.3 => 4.4.3
npmGlobalPackages:
typescript: 4.4.3
I choose this package/lib because I don’t know much about configuration stuff (Jest, node, tsconfig) but apparently I am stuck and can’t find any solution. Can you please let me know what am I missing here?
Issue Analytics
- State:
- Created 2 years ago
- Reactions:2
- Comments:5
Top Results From Across the Web
SyntaxError: Cannot use import statement outside a module ...
This is a React, Typescript, Webpack project. I am trying to test a module. But Jest won't transform the module to plain javascript...
Read more >SyntaxError: Cannot use import statement outside a ... - GitHub
I suspect the issue is Jest is not configured to run Node code using "type: module" (ECMAScript Modules).
Read more >TypeScript Jest: Cannot use import statement outside module
The TypeScript jest error "Cannot use import statement outside module" occurs when we misconfigure jest in a TypeScript project and run test files...
Read more >cannot use import statement outside a module jest - You.com
TypeScript Jest: Cannot use import statement outside module # The TypeScript jest error "Cannot use import statement outside module" occurs when we misconfigure ......
Read more >Cannot use import statement outside a module [React ...
When building a web application, you may encounter the SyntaxError: Cannot use import statement outside a module error.
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 Free
Top 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
I figured it out. While installing
@babel/plugin-transform-modules-commonjs
I saw a warning saying I should also installjest-babel
which is not required, removing it solved the problem. Thanks a lot for your help 😃I have the same issue. I’m trying to import something to a test file but still get
SyntaxError: Cannot use import statement outside a module.
In my opinion, the issue should be reopened as the problem still occurs on the master branch.