Error: 'import.meta' meta-property issue

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:

  1. Check Module Type: Ensure that your module type is set to one of the allowed options. You can do this by updating your script's configuration in your package.json or using the --module flag when running your script. For example:
"type": "module"
or
node --module es2022 your-script.js

Choose the appropriate module type based on your project requirements.

  1. Update Node.js Version: Make sure you are using a Node.js version that supports the specified module type. Upgrade Node.js to a version that includes support for ECMAScript modules. For example, Node.js 16 or later.
  1. Verify import.meta Usage: Check your code for unnecessary or incorrect usage of 'import.meta'. Ensure that it is used in a valid context within your ECMAScript module.

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.