GithubHelp home page GithubHelp logo

rhysd / dirname-filename-esm Goto Github PK

View Code? Open in Web Editor NEW
64.0 5.0 1.0 23 KB

__dirname and __filename for ES Modules environment

Home Page: https://www.npmjs.com/package/dirname-filename-esm

License: MIT License

JavaScript 89.22% TypeScript 10.78%
node es-modules commonjs compatibility

dirname-filename-esm's Introduction

__dirname and __filename for ES Modules on Node.js

CI 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

dirname-filename-esm's People

Contributors

chocolateloverraj avatar jumpman255 avatar rhysd avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Forkers

jumpman255

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.