Skip to content
This repository was archived by the owner on Sep 9, 2024. It is now read-only.
This repository was archived by the owner on Sep 9, 2024. It is now read-only.

TypeError: Cannot read properties of undefined (reading 'filename') #31

@ericcornelissen

Description

@ericcornelissen

I noticed an error while using this package with Node.js v16.13.2 1. Specifically, when not providing a filename myself, I'm seeing the error:

.../node_modules/eval/eval.js:38
  var _filename = filename || module.parent.filename;
                                            ^

TypeError: Cannot read properties of undefined (reading 'filename')
    at module.exports (.../node_modules/eval/eval.js:38:45)
    at file:.../test.js:3:1
    at ModuleJob.run (node:internal/modules/esm/module_job:198:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:385:24)
    at async loadESM (node:internal/process/esm_loader:88:5)
    at async handleMainPromise (node:internal/modules/run_main:61:12)
View the test.js script
import jsEval from "eval";

jsEval("module.exports = { };");

This seems to me related to the fact that I'm using CommonJS code (namely this package) from ESModules code. Because ESModule doesn't use module, I'm guessing module.parent is undefined and hence the filename cannot be obtained.

The work-around is pretty simple (at least for now): just provide a filename. However, as the filename is optional in the API, I think it would make sense to find an alternative default filename to be used when none is provided - one that works when calling this package from ESModule code.

Also, see ericcornelissen/svgo-action#548 (comment) for more background information on how I encountered this problem.

Footnotes

  1. I did not try to figure out the exact version(s) of Node.js where this issue first happens.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions