Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/fwh1990/this-file
Create dynamic __dirname, __filename and require method for both ESM and CJS
https://github.com/fwh1990/this-file
es-modules esm-dirname esm-filename esm-require
Last synced: about 4 hours ago
JSON representation
Create dynamic __dirname, __filename and require method for both ESM and CJS
- Host: GitHub
- URL: https://github.com/fwh1990/this-file
- Owner: fwh1990
- License: mit
- Created: 2021-05-20T02:28:31.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2021-06-09T06:13:10.000Z (over 3 years ago)
- Last Synced: 2024-04-13T21:54:59.450Z (7 months ago)
- Topics: es-modules, esm-dirname, esm-filename, esm-require
- Language: TypeScript
- Homepage:
- Size: 65.4 KB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# this-file
If you are an author of open source repository, you may want to support both CJS and ESM for users since it's a confused period. It's hard to use `__dirname` and `import.meta.url` in one file specially for typescript project.
# Installation
```bash
yarn add this-file
```# Usage
```typescript
// In per file
import { createContext } from 'this-file';const context = createContext();
// CJS
context.filename IS __filename
context.dirname IS __dirname
context.require IS require// ESM
context.filename IS url.fileURLToPath(import.meta.url)
context.dirname IS path.dirname(context.filename)
context.require IS module.createContext(import.meta.url)
```