Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getFileName() returns the file name with file:// prefix when "type": "module" is specified #18

Open
joahimg opened this issue Feb 17, 2022 · 3 comments

Comments

@joahimg
Copy link

joahimg commented Feb 17, 2022

Hello,

With Node 16 and when "type": "module" is specified in package.json, the function getFileName() returns the file name with file:// prefix.

Example with the reproducible code below:
$ node index.js

Result:
file:///Users/myuser/projects/myproject/mytest/index.js

Expected result:
/Users/myuser/projects/myproject/mytest/index.js

Reproducible code:
package.json

{
  "name": "mytest",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "callsites": "^4.0.0"
  }
}

index.js

import callsites from 'callsites';

function unicorn() {
	console.log(callsites()[0].getFileName());
}

unicorn();
@sindresorhus
Copy link
Owner

This is caused by Node.js: nodejs/node#39787

@okko
Copy link

okko commented Jun 30, 2022

Node has merged a fix, but would a .replace(/^file:\/\//, '') be a good workaround for compatibility with earlier Node versions?

@okko
Copy link

okko commented Aug 5, 2022

If you are here because of aws-cdk-lib,

hotpatch node_modules/aws-cdk-lib/aws-lambda-nodejs/lib/function.js function findEntry:

  const definingFile = findDefiningFile(),
    extname = path.extname(definingFile),
    tsHandlerFile = definingFile.replace(
      new RegExp(`${extname}$`),
      `.${id}.ts`
    ).replace('file://', ''); // ADD THIS!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants