code-pushup / cli

A CLI to run all kinds of code quality measurements to align your team with company goals
https://code-pushup.dev
MIT License
158 stars 9 forks source link

package.json configuration for @code-pushup/eslint-plugin is blocking it's use #394

Closed beaussan closed 4 months ago

beaussan commented 6 months ago

What happened?

While I was trying to use the project, I followed the readme.

First of all, I tried with the ts config file name, however it wasn't picked automatically (fixed by https://github.com/code-pushup/cli/issues/307 ), but then I was hit with the following error :

code-pushup  --config code-pushup.config.ts 
file:///Users/beaussan/perso/git/beaussan-mono/code-pushup.config.bundled_tqbo9am8nv.mjs:2
import eslintPlugin, { eslintConfigFromNxProjects } from "@code-pushup/eslint-plugin";
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: Named export 'eslintConfigFromNxProjects' not found. The requested module '@code-pushup/eslint-plugin' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from '@code-pushup/eslint-plugin';
const { eslintConfigFromNxProjects } = pkg;

So I circle around the issue by changing the import to the following :

import defaultEslintPlugin from '@code-pushup/eslint-plugin';
import type { CoreConfig } from '@code-pushup/models';

const eslintPlugin = defaultEslintPlugin.eslintPlugin;

const eslintConfigFromNxProjects =
  defaultEslintPlugin.eslintConfigFromNxProjects;

And that gave another error :

code-pushup  --config code-pushup.config.ts 
(node:57828) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
/Users/nicolasbeaussarthatchuel/perso/git/beaussan-mono/node_modules/@code-pushup/eslint-plugin/index.js:2
import { mkdir as mkdir2, writeFile } from "fs/promises";
^^^^^^

SyntaxError: Cannot use import statement outside a module

What would you expect to happen?

I expect typescript to be happy, node to resolve correctly and to be able to run the eslint plugin properly.

After following my hunch, I can confirm that the types of the packages are wrong (report here) :

image

I was able to make it work with the following minimal changes to the package.json in my node_module :

{
  "name": "@code-pushup/eslint-plugin",
  "version": "0.6.5",
+  "type": "module",
+  "main": "index.js",
  "dependencies": {
    "@code-pushup/utils": "*",
    "@code-pushup/models": "*",
    "eslint": "~8.46.0",
    "zod": "^3.22.4"
  },
  "peerDependencies": {
    "@nx/devkit": "^17.0.0"
  },
  "peerDependenciesMeta": {
    "@nx/devkit": {
      "optional": true
    }
  }
}

What steps did you take?

Follow the readme, install the eslint plugin, and run code-pushup. Pr that do this : https://github.com/beaussan/beaussan-mono/pull/127

What operation system are you on?

MacOS

Node version

v18.18.2

Relevant log output

No response

matejchalk commented 5 months ago

@beaussan This looks like a broken build output (we create those package.json fields in our build script). Could you check if this is still a problem with our latest version?

matejchalk commented 4 months ago

Closing as this should now be fixed for future releases by #517. Let me know if it reoccurs.