├── .gitignore ├── .travis.yml ├── index.js ├── LICENSE.txt ├── CHANGELOG.md ├── package.json ├── README.md └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .DS_Store 3 | .nyc_output 4 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | os: 3 | - linux 4 | node_js: 5 | - "4" 6 | - "6" 7 | - "stable" 8 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | module.exports = function (_require) { 2 | _require = _require || require 3 | var main = _require.main 4 | if (main && isIISNode(main)) return handleIISNode(main) 5 | else return main ? main.filename : process.cwd() 6 | } 7 | 8 | function isIISNode (main) { 9 | return /\\iisnode\\/.test(main.filename) 10 | } 11 | 12 | function handleIISNode (main) { 13 | if (!main.children.length) { 14 | return main.filename 15 | } else { 16 | return main.children[0].filename 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2016, Contributors 2 | 3 | Permission to use, copy, modify, and/or distribute this software 4 | for any purpose with or without fee is hereby granted, provided 5 | that the above copyright notice and this permission notice 6 | appear in all copies. 7 | 8 | THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 | WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES 10 | OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE 11 | LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES 12 | OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, 13 | WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 14 | ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | 3 | All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. 4 | 5 | 6 | # [2.0.0](https://github.com/yargs/require-main-filename/compare/v1.0.2...v2.0.0) (2019-01-28) 7 | 8 | 9 | ### Chores 10 | 11 | * drop support for Node 0.10 ([#11](https://github.com/yargs/require-main-filename/issues/11)) ([87f4e13](https://github.com/yargs/require-main-filename/commit/87f4e13)) 12 | 13 | 14 | ### BREAKING CHANGES 15 | 16 | * drop support for Node 0.10/0.12 17 | 18 | 19 | 20 | 21 | ## [1.0.2](https://github.com/yargs/require-main-filename/compare/v1.0.1...v1.0.2) (2017-06-16) 22 | 23 | 24 | ### Bug Fixes 25 | 26 | * add files to package.json ([#4](https://github.com/yargs/require-main-filename/issues/4)) ([fa29988](https://github.com/yargs/require-main-filename/commit/fa29988)) 27 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "require-main-filename", 3 | "version": "2.0.0", 4 | "description": "shim for require.main.filename() that works in as many environments as possible", 5 | "main": "index.js", 6 | "scripts": { 7 | "pretest": "standard", 8 | "test": "tap --coverage test.js", 9 | "release": "standard-version" 10 | }, 11 | "repository": { 12 | "type": "git", 13 | "url": "git+ssh://git@github.com/yargs/require-main-filename.git" 14 | }, 15 | "keywords": [ 16 | "require", 17 | "shim", 18 | "iisnode" 19 | ], 20 | "files": [ 21 | "index.js" 22 | ], 23 | "author": "Ben Coe ", 24 | "license": "ISC", 25 | "bugs": { 26 | "url": "https://github.com/yargs/require-main-filename/issues" 27 | }, 28 | "homepage": "https://github.com/yargs/require-main-filename#readme", 29 | "devDependencies": { 30 | "chai": "^4.0.0", 31 | "standard": "^10.0.3", 32 | "standard-version": "^5.0.0", 33 | "tap": "^11.0.0" 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # require-main-filename 2 | 3 | [![Build Status](https://travis-ci.org/yargs/require-main-filename.png)](https://travis-ci.org/yargs/require-main-filename) 4 | [![Coverage Status](https://coveralls.io/repos/yargs/require-main-filename/badge.svg?branch=master)](https://coveralls.io/r/yargs/require-main-filename?branch=master) 5 | [![NPM version](https://img.shields.io/npm/v/require-main-filename.svg)](https://www.npmjs.com/package/require-main-filename) 6 | 7 | `require.main.filename` is great for figuring out the entry 8 | point for the current application. This can be combined with a module like 9 | [pkg-conf](https://www.npmjs.com/package/pkg-conf) to, _as if by magic_, load 10 | top-level configuration. 11 | 12 | Unfortunately, `require.main.filename` sometimes fails when an application is 13 | executed with an alternative process manager, e.g., [iisnode](https://github.com/tjanczuk/iisnode). 14 | 15 | `require-main-filename` is a shim that addresses this problem. 16 | 17 | ## Usage 18 | 19 | ```js 20 | var main = require('require-main-filename')() 21 | // use main as an alternative to require.main.filename. 22 | ``` 23 | 24 | ## License 25 | 26 | ISC 27 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | /* global describe, it */ 2 | 3 | var requireMainFilename = require('./') 4 | 5 | require('tap').mochaGlobals() 6 | require('chai').should() 7 | 8 | describe('require-main-filename', function () { 9 | it('returns require.main.filename in normal circumstances', function () { 10 | requireMainFilename().should.match(/test\.js/) 11 | }) 12 | 13 | it('should use children[0].filename when running on iisnode', function () { 14 | var main = { 15 | filename: 'D:\\Program Files (x86)\\iisnode\\interceptor.js', 16 | children: [ {filename: 'D:\\home\\site\\wwwroot\\server.js'} ] 17 | } 18 | requireMainFilename({ 19 | main: main 20 | }).should.match(/server\.js/) 21 | }) 22 | 23 | it('should not use children[0] if no children exist', function () { 24 | var main = { 25 | filename: 'D:\\Program Files (x86)\\iisnode\\interceptor.js', 26 | children: [] 27 | } 28 | requireMainFilename({ 29 | main: main 30 | }).should.match(/interceptor\.js/) 31 | }) 32 | 33 | it('should default to process.cwd() if require.main is undefined', function () { 34 | requireMainFilename({}).should.match(/require-main-filename/) 35 | }) 36 | }) 37 | --------------------------------------------------------------------------------