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.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 })
npm install --save filedirname
import pkg from ('filedirname')
const pkg = require('filedirname').default
import pkg from 'https://unpkg.com/filedirname@^2.3.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.js
filedirname/edition-esnext/index.js
is TypeScript compiled against ESNext for Node.js 10 || 12 || 13 || 14 with Require for modulesfiledirname/edition-esnext-esm/index.js
is TypeScript compiled against ESNext for Node.js 12 || 13 || 14 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:
These amazing people have contributed code to this project:
Discover how you can contribute by heading on over to the CONTRIBUTING.md
file.
Author: bevry
Source Code: https://github.com/bevry/filedirname
#deno #nodejs #node #javascript