Cannot use import statement outside a module - Middy 3.0.4 w/ Jest, TypeScript
See original GitHub issueDescribe the bug
Cannot run Jest tests with any versions of Middy above 3.x, as imports are not resolving for Middy dependencies. I have attempted using babel-jest to resolve .js dependencies with the same issue. This is a Serverless framework app with about 13 TypeScript functions we deploy to individual Lambdas to AWS.
What is also strange is that the functions will build fine with Serverless framework for deployments. It seems to be only tests that cannot parse import statements correctly.
Error mesage:

Environment (please complete the following information):
Dev dependencies
- Node.js: [14.16.1]
- TypeScript [^4.7.2]
- @middy/core [3.0.4]
- @middy/http-cors: [3.0.4]
- @middy/http-error-handler": [3.0.4]
- @middy/http-json-body-parser": [3.0.4]
- @middy/http-response-serializer": [3.0.4]
- @middy/validator": [3.0.4]
Test dependencies
- Jest [^26.6.3]
- TS-Jest [^26.5.6]
jest.config.js:
module.exports = {
collectCoverage: true,
coverageDirectory: "jest-coverage",
moduleFileExtensions: ["js", "ts", "json", "node"],
modulePaths: ["<rootDir>"],
testMatch: ["**/*.spec.ts"],
transform: {
"^.+\\.ts?$": "ts-jest",
},
verbose: true,
testEnvironment: 'node'
};
tsconfig.json
{
"compilerOptions": {
"baseUrl": "./",
"esModuleInterop": true,
"preserveConstEnums": true,
"strictNullChecks": true,
"sourceMap": true,
"allowJs": false,
"target": "es6",
"typeRoots": ["node_modules/@types"],
"resolveJsonModule": true,
"moduleResolution": "node"
}
}
Issue Analytics
- State:
- Created a year ago
- Reactions:2
- Comments:18 (8 by maintainers)
Top Results From Across the Web
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 >How to resolve "Cannot use import statement outside a ...
Solution: my named imports were coming from index.js files and I believe ts-jest needed them as index.ts files (I'm using Typescript).
Read more >SyntaxError: Cannot use import statement outside a module ...
I suspect the issue is Jest is not configured to run Node code using "type: module" (ECMAScript Modules).
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 >syntaxerror cannot use import statement outside a module ...
The TypeScript jest error "Cannot use import statement outside module" occurs when jest is misconfigured in a TypeScript project. To solve the 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 feel like there is no winning with this one.
The TypeScript community wants something like this: https://gist.github.com/cef62/a14c3003095fe56fba2b69d6c57e5777?permalink_comment_id=4183778#gistcomment-4183778 while AWS is using the backwards compatible notation for older version of NodeJS (<=nodejs12.x), likely because they still have to support them (Note:
moduleis not an officially supported field – https://stackoverflow.com/questions/42708484/what-is-the-module-package-json-field-for). Both on complete opposites.I opted to go with what the NodeJS documentation recommended as best practice for the lowest version we support (>= nodejs14.x). https://nodejs.org/docs/latest-v14.x/api/packages.html#packages_dual_commonjs_es_module_packages
I’ve gone ahead and documented working configurations for the most popular transpilers and bundlers (https://middy.js.org/docs/best-practices/bundling). If a build tool isn’t keeping up to date the community will need to help them update or switch to another tool.
Personally, I use
rollup&esbuildand haven’t had an issue yet in my transition to fully ESM.I’ve released
3.1.0-rc.0that now includesmainand TS 4.7+ support. Let me know if it’s better or worse.