$ npm install filedirname
Fetch the current file and directory path, no matter your environment (Deno, Node.js, Web Browsers, ESM, CJS)
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 })
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 })
import.meta.urlThe 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 })
npm install --save filedirnameimport pkg from ('filedirname')const pkg = require('filedirname').defaultimport pkg from 'https://unpkg.com/filedirname@^2.4.0/edition-deno/index.ts'
This package is published with the following editions:
filedirname/source/index.ts is TypeScript source code with Import for modulesfiledirname aliases filedirname/edition-esnext/index.jsfiledirname/edition-esnext/index.js is TypeScript compiled against ESNext for Node.js 10 || 12 || 14 || 15 with Require for modulesfiledirname/edition-esnext-esm/index.js is TypeScript compiled against ESNext for Node.js 12 || 14 || 15 with Import for modulesfiledirname/edition-deno/index.ts is TypeScript source code made to be compatible with DenoDiscover the release history by heading on over to the HISTORY.md file.
Discover how you can contribute by heading on over to the CONTRIBUTING.md file.
These amazing people are maintaining this project:
No sponsors yet! Will you be the first?
These amazing people have contributed code to this project:
Discover how you can contribute by heading on over to the CONTRIBUTING.md file.
Unless stated otherwise all works are:
and licensed under:
© 2010 - cnpmjs.org x YWFE | Home | YWFE