Git Product home page Git Product logo

get-caller-file's Introduction

get-caller-file

Build Status Build status

This is a utility, which allows a function to figure out from which file it was invoked. It does so by inspecting v8's stack trace at the time it is invoked.

Inspired by http://stackoverflow.com/questions/13227489

note: this relies on Node/V8 specific APIs, as such other runtimes may not work

Installation

yarn add get-caller-file

Usage

Given:

// ./foo.js
const getCallerFile = require('get-caller-file');

module.exports = function() {
  return getCallerFile(); // figures out who called it
};
// index.js
const foo = require('./foo');

foo() // => /full/path/to/this/file/index.js

Options:

  • getCallerFile(position = 2): where position is stack frame whos fileName we want.

get-caller-file's People

Contributors

dyniper avatar locks avatar mportuga avatar stefanpenner avatar zomarg avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

get-caller-file's Issues

returns null with async functions

const getCallerFile = require('get-caller-file');

const numArray = [1,2,3,4,5];
if (numArray.some(num => {
    const file = getCallerFile();
    console.log(`we are in ${file}`);
    return true;
})) {
  console.log("see above ^^ shouldn't be null");
}

output:

we are in null
see above ^^ shouldn't be null

Error parsing sourcemaps

Seeing this error when I try to build an application with webpack that has get-caller-file as an npm dependency:

Module Warning (from ../../node_modules/source-map-loader/dist/cjs.js):
Failed to parse source map from '/home/me/app/node_modules/get-caller-file/index.ts' file: 
Error: ENOENT: no such file or directory, open '/home/me/app/node_modules/get-caller-file/index.ts'

Your sourcemap file is referencing index.ts as a source, but that file isn't present in the downloaded package.

Missing LICENSE file

Please add a license file as the ISC license requires it to be with the code.

Wrong file returned - ATENTION

`// root/index.js
module.exports = function() { return getCallerFile(); }

// root/dir/app.js
const func = require('../index.js');
func(); // returns root/index.js`

Firstly encourntered this when using TS-NODE. It occurs in node too. Caller file is app.js yet definition file index.js is returned.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.