eslint-plugin-import: `eslint-plugin-import` breaks `eslint` if no `package.json`-file is present

If there is no package.json-file in the file to check (or any parent directory), the eslint-run fails due to this piece of code: https://github.com/benmosher/eslint-plugin-import/blob/802ce7d49d912289d590f735bf9bd2d931064863/src/core/packagePath.js#L11-L12

Following error is reported:

TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received null
Occurred while linting {filename}.js

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Comments: 27 (12 by maintainers)

Commits related to this issue

Most upvoted comments

@ljharb sorry, but I completely disagree. The purpose of the package.json is to create a npm package to share a project. If you don’t want to share your project, you don’t need a npm package and therefore also no package.json.

Furthermore eslint is for linting any javascript code, not only javascript code which is part of a npm package. So why does this restriction exist for eslint-plugin-import? I’m using several other third party plugins and not one of them is making this restriction.

And this has also nothing to do with not following node conventions. There aren’t any node conventions which force you to create a npm package. Your arguments are not convincing and I’m still having the opinion, that this behavior should be considered as a bug.

We should definitely have a better error message - but why would you ever be running eslint outside of a node project with a package.json? global installs and personal configs are deprecated.

I also don’t have a package.json, just because I’m not deploying any node modules in my project and therefore I simply don’t need a package.json. IMHO this should be considered as a bug as it worked before without a package.json.

@g1thuser that’s not the only purpose of package.json. node itself uses it even for non-published apps for a few purposes. There ABSOLUTELY are node conventions that force a package.json, in other words.

You’re welcome to opt out by not using this plugin, but javascript without a package.json is JS that’s stuck in the early part of the previous decade, so I’m not concerned with linting that. In particular, eslint-plugin-import doesn’t apply for anything except node, and only recently, ESM - but we don’t truly yet support native ESM, only transpiled ESM, so there’s not yet a use case to omit package.json for this plugin.

@ljharb I’m experiencing similar issues under bazel. Although the root of the project may contain a package.json, the environment where the linting takes place may not. Since everything is sandboxed in bazel the package.json would need to be explicitly added to each lint target. You can almost think of it as a docker container which only includes the files to be linted + eslint (binary & plugins) per defined unit.

I think the requirement of a package.json is fine but, the error message threw me for a loop, coupled with a long debugging session to realize the missing dependency.

We should definitely have a better error message - but why would you ever be running eslint outside of a node project with a package.json? global installs and personal configs are deprecated.

Thanks, @ljharb. I appreciate your thoughts. From my perspective, having a clearer error message would have been really helpful in debugging this.

@ljharb

eslint-plugin-import doesn’t apply for anything except node,

This is definitely wrong. I’m using this plugin for my typescript projects, which aren’t node projects and therefore don’t require a package.json. Furthermore I’m using eslint also for the relevant gulpfile.jsm files in these projects for the build and even for their .eslintrc.js files.

Only because you don’t see any reasonable use cases to use this plugin without a package.json, this doesn’t mean that there aren’t any. And yes, my non-package.json use case is valid and, of course, also the one from @GiladShoham .

But as you have asked for a repro repo, here it is.

The two eslint config files in this repo are crashing eslint with different stack traces. The included rules are the only rules from eslint-plugin-import, which I’m using, which cause a crash. All other rules from this plugin are fine for me.

It’s certainly a scenario that’s incompatible with this plugin.

We should improve the error message here, though.