kessler / license-report

create a short report about a project's dependencies (license, url etc)
MIT License
236 stars 39 forks source link

Cannot read property 'version' of undefined #72

Closed flaming-codes closed 2 years ago

flaming-codes commented 2 years ago

Description

After updating from 4.x to 5.0.1, the following crash happens when trying to generate the licenses. I also ran a clean install of all dependencies, with the same effect.

@flaming-codes ➜ /workspaces/flaming.codes/app (master ✗) $ npm run dep:gen

> flaming.codes@1.0.0 dep:gen /workspaces/flaming.codes/app
> license-report --only=prod --output=json > licenses.json

TypeError: Cannot read property 'version' of undefined
    at getInstalledVersions (/workspaces/flaming.codes/app/node_modules/license-report/lib/getInstalledVersions.js:19:75)
    at /workspaces/flaming.codes/app/node_modules/license-report/index.js:77:27
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! flaming.codes@1.0.0 dep:gen: `license-report --only=prod --output=json > licenses.json`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the flaming.codes@1.0.0 dep:gen script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/codespace/.npm/_logs/2022-03-07T21_50_48_740Z-debug.log

Specifically, this code line the lib causes the error.

Solution

I think the logic in the referenced code line has a bug.

if ((packageLockDependency !== undefined) || (packageLockDependency.version !== undefined)) {
 ...
}

packageLockDependency in the second check will always be undefined, as the first check fails if it's actually defined, therefore leading to the execution of the second one.

A simple fix might be to update the logic.

if (packageLockDependency && packageLockDependency.version !== undefined) {
 ...
}

or even simpler

if (packageLockDependency?.version !== undefined) {
 ...
}

Notes

I could work on the fix (fix itself is small, but adding tests) if desired.

BePo65 commented 2 years ago

Can you please show us the package.json that causes the error (or at least the dependency sections)? I would like to take a look at the failing remote package file, as until now I thought that npm defines the version field as mandatory.

BePo65 commented 2 years ago

A quick check showed that the '||' in getInstalledVersions.js line 19 must be '&&'. I will have a deeper look to find out, if there is a test case for this situation and if not, if I can create one.

BePo65 commented 2 years ago

Digging deeper it looks like this error only shows, when a package from package.json does not exist in the package-lock.json. As package-lock.json files usually are too big to be copied into this issue, does this error happen on one of your public repositories (so that I can take my test data from there)?

flaming-codes commented 2 years ago

Sorry for the late reply, here's the package.json that's causing the issue:

{
  "name": "flaming.codes",
  "version": "1.0.0",
  "private": true,
  "engines": {
    "node": "14.x"
  },
  "scripts": {
    "dev": "npx next dev",
    "build": "npm run dep:gen && npx next build",
    "start": "npx next start",
    "test": "jest",
    "test:watch": "jest --watch",
    "test:coverage": "jest --coverage",
    "analyze": "ANALYZE=true npx next build",
    "deploy:login": "npx vercel login",
    "deploy:dev": "next build && npx vercel",
    "deploy:prod": "next build && npx vercel --prod",
    "deploy:all": "next build && npx now && npx vercel --prod",
    "dep:up": "npm-check -u --no-emoji",
    "dep:gen": "license-report --only=prod --output=json > licenses.json"
  },
  "dependencies": {
    "@google-cloud/text-to-speech": "^3.4.0",
    "@google-cloud/translate": "^6.3.1",
    "@headlessui/react": "^1.5.0",
    "@loadable/component": "^5.15.2",
    "@react-hook/window-scroll": "^1.3.0",
    "@sanity/block-content-to-react": "^3.0.0",
    "@sanity/client": "^3.1.0",
    "@sanity/image-url": "^1.0.1",
    "@tailwindcss/typography": "^0.5.2",
    "@use-it/event-listener": "^0.1.7",
    "@vercel/node": "^1.13.0",
    "@wbmnky/license-report-generator": "^2.1.3",
    "algoliasearch": "^4.12.2",
    "clsx": "^1.1.1",
    "daisyui": "^2.2.2",
    "date-fns": "^2.28.0",
    "firebase-admin": "^10.0.2",
    "groq": "^2.15.0",
    "microsoft-cognitiveservices-speech-sdk": "^1.20.0",
    "next-plausible": "^3.1.5",
    "next-plugin-preact": "^3.0.6",
    "next-pwa": "^5.4.4",
    "next-seo": "^5.1.0",
    "nextjs-redirect": "^5.2.1",
    "page-metadata-parser": "^1.1.4",
    "picosanity": "^4.0.0",
    "postcss-flexbugs-fixes": "^5.0.2",
    "postcss-preset-env": "^7.4.2",
    "preact": "^10.6.6",
    "preact-render-to-string": "^5.1.20",
    "query-string": "^7.1.1",
    "react": "npm:@preact/compat@0.0.4",
    "react-dom": "npm:@preact/compat@0.0.4",
    "react-helmet": "^6.1.0",
    "react-lazy-load-image-component": "^1.5.1",
    "react-lazyload": "^3.2.0",
    "react-reveal": "^1.2.2",
    "react-rotating-text": "^1.4.1",
    "react-schemaorg": "^2.0.0",
    "react-spring": "^9.4.3",
    "react-syntax-highlighter": "^15.4.5",
    "react-use-audio-player": "^1.2.5",
    "react-waypoint": "^10.1.0",
    "redaxios": "^0.4.1",
    "remixicon-react": "^1.0.0",
    "scheduler": "^0.20.2",
    "schema-dts": "^1.1.0",
    "seamless-scroll-polyfill": "^2.0.0",
    "sitemap": "^7.1.1",
    "smoothscroll-polyfill": "^0.4.4",
    "styled-components": "^5.3.3",
    "styled-jsx": "^5.0.0",
    "tailwindcss-safe-area": "^0.2.2",
    "use-query-params": "^1.2.3"
  },
  "devDependencies": {
    "@next/bundle-analyzer": "^12.1.0",
    "@testing-library/dom": "^8.11.3",
    "@testing-library/jest-dom": "^5.16.2",
    "@testing-library/react": "^12.1.3",
    "@types/jest": "^27.4.1",
    "@types/node": "^17.0.21",
    "@types/react": "^17.0.39",
    "@types/react-lazy-load-image-component": "^1.5.2",
    "@types/styled-components": "^5.1.24",
    "@types/wicg-mediasession": "^1.1.3",
    "autoprefixer": "^10.4.2",
    "babel-core": "^6.26.3",
    "babel-jest": "^27.5.1",
    "babel-plugin-transform-remove-console": "^6.9.4",
    "babel-preset-env": "^1.7.0",
    "babel-preset-react": "^6.24.1",
    "eslint": "^8.10.0",
    "eslint-config-next": "^12.1.0",
    "jest": "^27.5.1",
    "license-report": "^4.5.0",
    "next": "^12.1.0",
    "next-compose-plugins": "^2.2.1",
    "npm-check": "^5.9.2",
    "postcss": "^8.4.7",
    "postcss-100vh-fix": "^1.0.2",
    "prettier": "^2.5.1",
    "tailwindcss": "^3.0.23",
    "tailwindcss-scroll-snap": "^1.1.0",
    "ts-jest": "^27.1.3",
    "ts-node": "^10.6.0",
    "typescript": "^4.6.2",
    "vercel": "^24.0.0",
    "webpack": "^5.69.1",
    "webpack-modules": "^1.0.0"
  }
}

The repo is actually open source: https://github.com/flaming-codes/spikze-club. The repo is private, but I could DM you the package-lock.json, if you want.

BePo65 commented 2 years ago

So I tried to reproduce the error with your repository, but it didn't show any error. As line 19 of getInstalledVersions.js is wrong anyway, I created a fix in the master branch and it will be published soon.

BePo65 commented 2 years ago

Issue is solved with new version 5.0.2. Thank You for your contribution.