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

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

Closed
Rinse12 opened this issue Nov 20, 2022 · 6 comments
Closed

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

Rinse12 opened this issue Nov 20, 2022 · 6 comments

Comments

@Rinse12
Copy link

Rinse12 commented Nov 20, 2022

Node (ESM) fails on importing oclif/test due to this line

loadConfig.root = module.parent!.filename

module.parent is undefined in ESM.

@Rinse12
Copy link
Author

Rinse12 commented Nov 22, 2022

I replaced module.parent.filename with require.main.filename and it seems to work

I created https://github.com/Rinse12/oclif-test as a temporary until the fix is merged.

@phil-lgr
Copy link

phil-lgr commented Dec 12, 2022

Edit: with require.main.filename, I get the error:

TypeError: Cannot read properties of null (reading 'filename')

@phil-lgr
Copy link

@mdonnalley Do you have any idea in this case? We can submit a fix if we know how to approach this

@mrloop
Copy link

mrloop commented Jan 7, 2023

Looking at sindresorhus/meow#157 one suggested fix is passing the filename from the calling code. Maybe the API for test could be updated so for example in your test

test
.stderr({ print: true })
.command(['deploy', name])

becomes

test({ filename })
.stderr({print: true })
.command(['deploy', name])'

and if no filename provided would fall back to current behaviour?

mrloop added a commit to mrloop/netlify-git-branch that referenced this issue Jan 7, 2023
mrloop added a commit to mrloop/netlify-git-branch that referenced this issue Jan 15, 2023
@kaushals-mecha
Copy link

kaushals-mecha commented Apr 10, 2023

Another ugly workaround is below
Just use this in single file, other files will not require import changes

import { createRequire } from 'module';
import { expect as expectType, test as testType } from '@oclif/test';
const { expect, test } = createRequire(import.meta.url)('@oclif/test') as {
  expect: typeof expectType;
  test: typeof testType;
};

Reference Link

@mdonnalley
Copy link
Contributor

I believe this was fixed in 2.4.0 - please reopen if you think there's still an issue here.

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

5 participants