New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More

@itrocks/class-file

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@itrocks/class-file

Retrieve the absolute file path of an imported class


Version published
Maintainers
1
Created
Issues
0

npm version npm downloads GitHub issues discord

class-file

Retrieve the absolute file path of an imported class:

  • Automatic for your CommonJS-compiled projects,
  • Using a @File decorator in ESM-compiled projects.

Installation

npm install @itrocks/class-file

Activation

This library associates each imported class with the absolute file path where it is defined. Its behavior differs depending on the module compilation mode:

CommonJS: Recommended for most use cases.
Add this line to the very start of your main file:

import '@itrocks/class-file/automation'

or:

require('@itrocks/class-file/automation')

This enables automatic file path resolution for all subsequently imported classes.

ESModule: Requires manual setup.
Due to the limitation of ESM, you must explicitly decorate classes with the @File(fileURLToPath(import.meta.url)) decorator to enable file path resolution. Without this decorator, fileOf() will return undefined.

Example

Given a file my-class.js, with CommonJS transpilation:

export default class MyClass {}

Or on transpiling to an ESModule:

import { fileURLToPath } from 'node:url'
@File(fileURLToPath(import.meta.url))
export default class MyClass {}

Retrieving the file path:

import { fileOf } from '@itrocks/class-file'
import MyClass    from './my-class.js'

console.log(fileOf(MyClass))       // Returns the absolute file path
console.log(fileOf(new MyClass))   // Also returns the absolute file path

FAQs

Package last updated on 28 Jan 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts