Transforms import.meta for nodejs environments
npx @tessl/cli install tessl/npm-babel-plugin-transform-import-meta@2.3.0Babel Plugin Transform Import Meta is a Babel plugin that transforms ES2020 import.meta expressions into Node.js-compatible equivalents for environments that don't natively support import.meta. The plugin transforms four key properties: import.meta.url, import.meta.dirname, import.meta.filename, and import.meta.resolve().
npm install --save-dev babel-plugin-transform-import-meta// Default import (plugin function)
import transformImportMetaPlugin from "babel-plugin-transform-import-meta";With TypeScript types:
import transformImportMetaPlugin, { type PluginOptions } from "babel-plugin-transform-import-meta";For CommonJS:
const transformImportMetaPlugin = require("babel-plugin-transform-import-meta");Add to your Babel configuration:
{
"plugins": ["babel-plugin-transform-import-meta"]
}With ES6 module output:
{
"plugins": [
["babel-plugin-transform-import-meta", { "module": "ES6" }]
]
}The plugin implements a Babel visitor pattern that traverses the AST and transforms specific import.meta expressions:
import.meta expressionsurl, dirname, filename, resolve)@babel/template for reliable AST node generationThe main default export is a Babel plugin function that returns a PluginObj for transforming import.meta expressions.
/**
* Main Babel plugin function that transforms import.meta expressions
* @returns PluginObj with visitor pattern for AST transformation
*/
export default function (): PluginObj;Configuration options for controlling the plugin's behavior.
export interface PluginOptions {
/** Target module format for output code. Defaults to 'CommonJS' */
module?: 'CommonJS' | 'ES6' | undefined;
}Transforms import.meta.url to Node.js compatible file URL.
Input:
console.log(import.meta.url);CommonJS Output:
console.log(require('url').pathToFileURL(__filename).toString());ES6 Output:
import url from 'url';
console.log(url.pathToFileURL(__filename).toString());Transforms import.meta.dirname to Node.js __dirname.
Input:
console.log(import.meta.dirname);Output (both CommonJS and ES6):
console.log(__dirname);Transforms import.meta.filename to Node.js __filename.
Input:
console.log(import.meta.filename);Output (both CommonJS and ES6):
console.log(__filename);Transforms import.meta.resolve(specifier) to Node.js require.resolve() wrapped with URL conversion. Supports both regular and optional chaining calls.
Input:
console.log(import.meta.resolve('./module'));
console.log(import.meta.resolve?.('./optional-module'));CommonJS Output:
console.log(require('url').pathToFileURL(require.resolve('./module')).toString());
console.log(require('url').pathToFileURL(require.resolve('./optional-module')).toString());ES6 Output:
import { createRequire } from 'module';
import url from 'url';
console.log(url.pathToFileURL(createRequire(url.pathToFileURL(__filename).toString()).resolve('./module')).toString());
console.log(url.pathToFileURL(createRequire(url.pathToFileURL(__filename).toString()).resolve('./optional-module')).toString());The plugin supports two target module formats:
require() statements for dependenciesimport statements, automatically injected at the top of the fileThe plugin implements scope safety features:
url or createRequire conflict with existing scope variablesurl, dirname, filename, resolve)Example with scope conflicts:
// Input
const url = '123';
const createRequire = () => {};
console.log(import.meta.resolve('./module'));
// ES6 Output
import { createRequire as _createRequire } from 'module';
import _url from 'url';
const url = '123';
const createRequire = () => {};
console.log(_url.pathToFileURL(_createRequire(_url.pathToFileURL(__filename).toString()).resolve('./module')).toString());The plugin validates configuration options and throws descriptive errors for invalid configurations.
Error thrown for invalid module option:
Error: Invalid target, must be one of: "CommonJS" or "ES6"The plugin preserves compatibility by:
import.meta without property accessimport.meta.customPropertyfoo.import.metaExamples that remain unchanged:
console.log(import.meta); // Unchanged
console.log(import.meta.customProperty); // Unchanged
console.log(foo.import.meta); // Unchangedexport interface PluginOptions {
/** Target module format for output code. Defaults to 'CommonJS' */
module?: 'CommonJS' | 'ES6' | undefined;
}
interface PluginObj {
name: string;
visitor: {
Program(path: NodePath, state: { opts?: PluginOptions }): void;
};
}
interface NodePath {
node: any;
scope: {
getAllBindings(): Record<string, any>;
generateUidIdentifier(name: string): { name: string };
};
traverse(visitors: object): void;
replaceWith(node: any): void;
}@babel/template: ^7.25.9tslib: ^2.8.1@babel/core: ^7.10.0