sindresorhus / read-package-up

Read the closest package.json file
MIT License
258 stars 18 forks source link

TypeScript typings issue #12

Closed alexgorbatchev closed 5 years ago

alexgorbatchev commented 5 years ago

I'm having an issue with importing the packages after native types were introduces (vs @types/read-pkg-up. I've tried import * as from and import ... = from, same results for both using TypeScript 3.5.1

Object is possibly 'undefined'.ts(2532)

image

sindresorhus commented 5 years ago

I think the error message is pretty clear. .pkg can be undefined as it might not find a package.json file. You need to guard that somehow. If you're absolutely sure there's a package.json file there, you could do readPkgUp.sync({cwd: fileName}).pkg!.name, though it would not recommend it. I'm also confused by your naming of your argument fileName being passed to the cwd option, which expects a path to a directory.