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 property 'slice' of undefined #3

Open
victor-homyakov opened this issue Jul 19, 2021 · 4 comments · May be fixed by #4
Open

TypeError: Cannot read property 'slice' of undefined #3

victor-homyakov opened this issue Jul 19, 2021 · 4 comments · May be fixed by #4

Comments

@victor-homyakov
Copy link

Sometimes I see an error when using this package

(node:67818) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'slice' of undefined
    at /project/node_modules/dependencies-tree-builder/src/scoupe_factory.js:61:40
    at Array.filter (<anonymous>)
    at /project/node_modules/dependencies-tree-builder/src/scoupe_factory.js:54:44
    at Array.forEach (<anonymous>)
    at /project/node_modules/dependencies-tree-builder/src/scoupe_factory.js:48:19
    at Array.forEach (<anonymous>)
    at ScoupeFactory._dedupeUsages (/project/node_modules/dependencies-tree-builder/src/scoupe_factory.js:44:27)
    at ScoupeFactory.create (/project/node_modules/dependencies-tree-builder/src/scoupe_factory.js:117:10)
    at module.exports (/project/node_modules/dependencies-tree-builder/src/index.js:45:32)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
@victor-homyakov
Copy link
Author

More info.

At the line

const { usages, tunable } = data;

(https://github.com/itwillwork/dependencies-tree-builder/blob/master/src/scoupe_factory.js#L49)

object data has the following value:

{
  usages: [ undefined, undefined, undefined, undefined, undefined ],
  version: undefined,
  name: undefined,
  tunable: true
}

@victor-homyakov
Copy link
Author

Monkey-patching helps to see at least some results

        const { usages, tunable } = data;

        const carriers = {};

        const sortedUsages = [...usages].sort(byLengthAsc);
-       const dedupedUsages = sortedUsages.filter(usage => {
+       const dedupedUsages = sortedUsages.filter((usage = []) => {
          let startUsagePath = 1;

          if (hasOnlyOneVersion || tunable) {
            startUsagePath = 0;
          }

          const effectiveUsage = usage.slice(startUsagePath);

@victor-homyakov
Copy link
Author

The problem is in package cache. If cache already exists and has any (even outdated) information about some library, then this information is used.

Steps to reproduce:

  1. Create package.json with some dependency, say [email protected] (1.0.0 is the latest version)
  2. Run dependencies tree builder
  3. It creates node_modules/dependencies-tree-builder/.packages.cache.json where are listed all currently known versions of dependency X (e.g. 0.9.0, 1.0.0)
  4. Release the new version of X (e.g. [email protected]), and update dependency in package.json ([email protected] -> [email protected])
  5. Run dependencies tree builder again. Now it finds already existing cache, the cache knows about the library X, but the cache does not contain any information about [email protected]!

@victor-homyakov victor-homyakov linked a pull request Aug 1, 2021 that will close this issue
@victor-homyakov
Copy link
Author

victor-homyakov commented Aug 1, 2021

Right now the easiest way to avoid this error is to delete node_modules/dependencies-tree-builder/.packages.cache.json just before running dependencies tree builder or use corresponding option

const buildTreeAsync = require('dependencies-tree-builder');
const {scoupe} = await buildTreeAsync(packageJson, {useCache: false});

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

Successfully merging a pull request may close this issue.

1 participant