All Projects → rhysd → dirname-filename-esm

rhysd / dirname-filename-esm

Licence: MIT License
__dirname and __filename for ES Modules environment

Programming Languages

javascript
184084 projects - #8 most used programming language
typescript
32286 projects

Projects that are alternatives of or similar to dirname-filename-esm

mpx-es-check
Checks the version of ES in JavaScript files with simple shell commands
Stars: ✭ 15 (-73.68%)
Mutual labels:  commonjs, compatibility
compat-db
A browser API compatibility database
Stars: ✭ 61 (+7.02%)
Mutual labels:  compatibility
convert
The smallest & fastest library for really easy, totally type-safe unit conversions in TypeScript & JavaScript.
Stars: ✭ 47 (-17.54%)
Mutual labels:  commonjs
importly
import map generator
Stars: ✭ 42 (-26.32%)
Mutual labels:  es-modules
pg global temp tables
Oracle-style global temporary tables for PostgreSQL
Stars: ✭ 16 (-71.93%)
Mutual labels:  compatibility
timezz
With this plugin, you can easily make a stopwatch or timer on your site. Just init, style and enjoy.
Stars: ✭ 35 (-38.6%)
Mutual labels:  commonjs
array-sort-by
Powerful mechanism to sort arrays or array of objects by one or more properties. You can also specify a custom comparer function.
Stars: ✭ 37 (-35.09%)
Mutual labels:  commonjs
yantra
JavaScript Engine for .NET Standard
Stars: ✭ 32 (-43.86%)
Mutual labels:  commonjs
the-super-tiny-web-module-resolver
simple js bundler written in 100 lines of code.
Stars: ✭ 25 (-56.14%)
Mutual labels:  commonjs
npm-es-modules
Breakdown of 7 different ways to use ES modules with npm today.
Stars: ✭ 67 (+17.54%)
Mutual labels:  commonjs
babel-plugin-transform-amd-to-commonjs
✨ Babel plugin that transforms AMD to CommonJS
Stars: ✭ 44 (-22.81%)
Mutual labels:  commonjs
titaniumifier
Get a Titanium™ SDK CommonJS module out of a Node package!
Stars: ✭ 95 (+66.67%)
Mutual labels:  commonjs
babel-plugin-transform-export-default-name
Babel plugin that transforms default exports to named exports.
Stars: ✭ 20 (-64.91%)
Mutual labels:  es-modules
oconfigure
configuration script for BSD.lv projects
Stars: ✭ 36 (-36.84%)
Mutual labels:  compatibility
graphene
Graphene / Graphene-SGX - a library OS for Linux multi-process applications, with Intel SGX support
Stars: ✭ 741 (+1200%)
Mutual labels:  compatibility
license-compatibility
©️ Check compatibility between different SPDX licenses
Stars: ✭ 31 (-45.61%)
Mutual labels:  compatibility
mock-spy-module-import
JavaScript import/require module testing do's and don'ts with Jest
Stars: ✭ 40 (-29.82%)
Mutual labels:  commonjs
trivial-with-current-source-form
Helps macro writers produce better errors for macro users
Stars: ✭ 22 (-61.4%)
Mutual labels:  compatibility
WSHModule
WSHModule is a JavaScript virtual machine built in WSH/JScript.
Stars: ✭ 28 (-50.88%)
Mutual labels:  commonjs
node-compat-require
Easily allow your Node program to run in a target node version range to maximize compatibility.
Stars: ✭ 22 (-61.4%)
Mutual labels:  compatibility

__dirname and __filename for ES Modules on Node.js

CI status npm

Node.js v13.2.0 was released with support for ES Modules. Official announcement explains the overview.

In ES Modules environment, global variables such as require, exports, module.exports, __filename, __dirname are not available. require, exports, module.exports are no longer necessary in favor of ESM, but we still need __filename and __dirname which can be calculated from import.meta.url.

To make migration from commonjs to ES Modules smooth, dirname-filename-esm provides functions to create __dirname and __filename variables easily in ES Modules environment.

Installation

npm install --save dirname-filename-esm

Usage

Usage is very straightforward.

// In ".mjs" script or a script under "type": "module" package
import { dirname, filename } from 'dirname-filename-esm';

const __dirname = dirname(import.meta);
const __filename = filename(import.meta);

// Use __dirname and __filename like under commonjs module

Two functions are exported.

  • dirname() takes import.meta object and returns __dirname value of the script.
  • filename() takes import.meta object and returns __filename value of the script.

This package has zero dependency.

This package offers TypeScript support. Since this package is for ES Modules environment, ensure that module option is set to es2020 or later in your tsconfig.json.

License

the MIT License

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].