If you encounter the error "the 'import.meta' meta-property is only allowed when the '--module' option is 'es2020', 'es2022', 'esnext', 'system', 'node16', or 'nodenext'", it typically indicates a mismatch in the ECMAScript module settings. Here's how you can resolve it:
"type": "module"
or
node --module es2022 your-script.js
Choose the appropriate module type based on your project requirements.
After making these adjustments, you should be able to resolve the 'import.meta' meta-property issue. If the problem persists, consider reviewing the Node.js documentation for the specific module type you are using for additional guidance.
After making the following changes in the configuration files:
//jest.config.js
module.exports = {
preset: 'ts-jest',
collectCoverage: true,
collectCoverageFrom: ['src/**/*.ts', '!**/*.d.ts'],
testEnvironment: 'node',
testPathIgnorePatterns: ['<rootDir>/node_modules/', '<rootDir>/dist/']
};
//tsconfig.json
{
"compilerOptions": {
"module": "commonjs",
"target": "es2017",
"strict": true,
"sourceMap": true,
// "declaration": true,
"esModuleInterop": true,
"resolveJsonModule": true,
"outDir": "./dist"
},
"include": ["src"]
}
You should now be able to import all your modules, including those from the node_modules directory, in the *.test.ts files.
I've attempted to adhere to various guides addressing the ESM (ECMAScript Modules) issue, and when running tests with:
node --experimental-vm-modules
the tests should function properly. However, it's worth noting that we receive a warning:
ExperimentalWarning: VM Modules is an experimental feature. This feature could change at any time.
It seems that the compiler completely disregards the compilerOptions.module
. Regardless of the module ESxx specified, the compiler behaves the same way.
While this approach resolves the errors, it doesn't address the issue of using import.meta
.