filedirname

Fetch the current file and directory path, no matter your environment (Deno, Node.js, Web Browsers, ESM, CJS)

Usage no npm install needed!

<script type="module">
  import filedirname from 'https://cdn.skypack.dev/filedirname';
</script>

README

filedirname

Status of the GitHub Workflow: bevry NPM version NPM downloads Dependency Status Dev Dependency Status
GitHub Sponsors donate button Patreon donate button Flattr donate button Liberapay donate button Buy Me A Coffee donate button Open Collective donate button crypto donate button PayPal donate button Wishlist browse button

Fetch the current file and directory path, no matter your environment (Deno, Node.js, Web Browsers, ESM, CJS)

Usage

Complete API Documentation.

Without Arguments

The following works for ESM (ECMAScript Modules) and CJS (CommonJS Modules) enviroments:

// for deno
import filedirname from 'https://unpkg.com/filedirname/edition-deno/index.ts'
// for web browsers
import filedirname from 'https://unpkg.com/filedirname'
// for node.js
import filedirname from 'filedirname'
// for commonjs
const filedirname = require('filedirname').default

// usage
const [__filename, __dirname] = filedirname()
console.log({ __filename, __dirname })

Via Error

The following works for ESM (ECMAScript Modules) and CJS (CommonJS Modules) environments:

// for deno
import filedirname from 'https://unpkg.com/filedirname/edition-deno/index.ts'
// for web browsers
import filedirname from 'https://unpkg.com/filedirname'
// for node.js
import filedirname from 'filedirname'
// for commonjs
const filedirname = require('filedirname').default

// usage
const [__filename, __dirname] = filedirname(new Error())
console.log({ __filename, __dirname })

Via import.meta.url

The following works only for ESM (ECMAScript Modules) environments and is the quickest solution:

// for deno
import filedirname from 'https://unpkg.com/filedirname/edition-deno/index.ts'
// for web browsers
import filedirname from 'https://unpkg.com/filedirname'
// for node.js
import filedirname from 'filedirname'

// usage
const [__filename, __dirname] = filedirname(import.meta.url)
console.log({ __filename, __dirname })

Install

npm

  • Install: npm install --save filedirname
  • Import: import pkg from ('filedirname')
  • Require: const pkg = require('filedirname').default

Editions

This package is published with the following editions:

  • filedirname/source/index.ts is TypeScript source code with Import for modules
  • filedirname aliases filedirname/edition-es2019/index.js
  • filedirname/edition-es2019/index.js is TypeScript compiled against ES2019 for Node.js 10 || 12 || 14 || 16 with Require for modules
  • filedirname/edition-es2019-esm/index.js is TypeScript compiled against ES2019 for Node.js 12 || 14 || 16 with Import for modules

History

Discover the release history by heading on over to the HISTORY.md file.

Contribute

Discover how you can contribute by heading on over to the CONTRIBUTING.md file.

Backers

Maintainers

These amazing people are maintaining this project:

Sponsors

No sponsors yet! Will you be the first?

GitHub Sponsors donate button Patreon donate button Flattr donate button Liberapay donate button Buy Me A Coffee donate button Open Collective donate button crypto donate button PayPal donate button Wishlist browse button

Contributors

These amazing people have contributed code to this project:

Discover how you can contribute by heading on over to the CONTRIBUTING.md file.

License

Unless stated otherwise all works are:

and licensed under: