A TypeScript transformer that add import.meta support for CommonJS module format.

Usage no npm install needed!

<script type="module">
  import magicWorksCommonjsImportMeta from 'https://cdn.skypack.dev/@magic-works/commonjs-import.meta';


import.meta => __filename


npm i -D @magic-works/commonjs-import.meta ttypescript typescript ts-node
yarn add -D @magic-works/commonjs-import.meta ttypescript typescript ts-node
pnpm i -D @magic-works/commonjs-import.meta ttypescript typescript ts-node

This package works with ttypescript.

Add the following to your tsconfig.json.

    "compilerOptions": {
        "plugins": [{ "transform": "@magic-works/commonjs-import.meta" }]

Use with ts-node in tsconfig.json

    "ts-node": {
        "compilerOptions": {
            "module": "CommonJS",
            "plugins": [{ "transform": "@magic-works/commonjs-import.meta" }]
        "compiler": "ttypescript",
        // One of:
        "ignoreDiagnostics": [1343],
        // Or:
        "transpileOnly": true
        // To mute The 'import.meta' meta-property is only allowed when the '--module' option is 'esnext' or 'system'.ts(1343)


This transformer did the following thing:

Add the following code to every file:

const __meta = { url: require('url').pathToFileURL(__filename).href }
Object.setPrototypeOf(__meta, null)

And it replace every import.meta into __meta.