Cannot use import statement outside a module - Middy 3.0.4 w/ Jest, TypeScript

See original GitHub issue

Describe 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: Screen Shot 2022-06-07 at 11 42 09 AM

Environment (please complete the following information):

Dev dependencies

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:closed
  • Created a year ago
  • Reactions:2
  • Comments:18 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
willfarrellcommented, Jun 17, 2022

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: module is 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 & esbuild and haven’t had an issue yet in my transition to fully ESM.

1reaction
willfarrellcommented, Jun 18, 2022

I’ve released 3.1.0-rc.0 that now includes main and TS 4.7+ support. Let me know if it’s better or worse.

Read more comments on GitHub >

github_iconTop 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 >

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