Fetch the current file and directory path, no matter your environment

filedirname

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

Deno

import pkg from 'https://unpkg.com/filedirname@^2.3.0/edition-deno/index.ts'

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-esnext/index.js
  • filedirname/edition-esnext/index.js is TypeScript compiled against ESNext for Node.js 10 || 12 || 13 || 14 with Require for modules
  • filedirname/edition-esnext-esm/index.js is TypeScript compiled against ESNext for Node.js 12 || 13 || 14 with Import for modules
  • filedirname/edition-deno/index.ts is TypeScript source code made to be compatible with Deno

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:

Contributors

These amazing people have contributed code to this project:

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

Download Details:

Author: bevry

Source Code: https://github.com/bevry/filedirname

#deno #nodejs #node #javascript

Fetch the current file and directory path, no matter your environment
2.25 GEEK