Hello, I am currently facing an issue while trying to utilize the file-type module, which is pure ESM, in a TypeScript project with Jest. Despite following the ESM guidelines outlined here, I continue to encounter a
SyntaxError: Cannot use import statement outside a module
error.
To better illustrate my problem, I have created a sandbox that you can access here.
Here is a summary of my code:
import { fileTypeFromBuffer } from "file-type";
Below is a snippet of my Jest configuration:
export default {
testEnvironment: "jest-environment-node",
globals: {
extensionsToTreatAsEsm: [".ts"],
"ts-jest": {
useESM: true,
},
},
transform: {
"^.+\\.(ts|tsx|js|jsx)?$": "ts-jest",
},
moduleNameMapper: {
"^(\\.{1,2}/.*)\\.js$": "$1",
},
preset: "ts-jest",
};
Furthermore, here is my TSConfig setup:
{
"extends": "@tsconfig/node14/tsconfig.json",
"compilerOptions": {
"target": "ES2018",
"module": "commonjs",
"lib": ["es2018"],
"declaration": true,
"strict": true,
"strictNullChecks": true,
"alwaysStrict": true,
"noImplicitReturns": false,
"noImplicitThis": false,
"noUnusedLocals": false,
"noUnusedParameters": false,
"noEmit": true,
"noFallthroughCasesInSwitch": false,
"inlineSourceMap": true,
"inlineSources": true,
"experimentalDecorators": true,
"strictPropertyInitialization": false,
"resolveJsonModule": true,
"outDir": "dist",
"baseUrl": ".",
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
}
}
If you have any insights or suggestions on how to resolve this, I would greatly appreciate it. Thank you!