core: Cannot read property 'when' of undefined

I’m running a project that uses angular hybrid with the following versions:

When executing unit tests I’m getting “Cannot read property ‘when’ of undefined” in most of the tests (karma with chai and mocha). I tried to debug and see that is pointing to node_modules/@uirouter/core/lib-esm/resolve/resolvable.js this.promise = this.resolved ? services.$q.when(this.data) : undefined; ( https://github.com/ui-router/core/blob/master/src/resolve/resolvable.ts#L84 ) If I guard against this.resolved the error is gone this.promise = this.resolved && services ? services.$q.when(this.data) : undefined;

Could you suggest in which cases services can be undefined and if this can be fixed on master? Should I create a pull request for it?

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Comments: 19 (4 by maintainers)

Most upvoted comments

I had the same problem.

npm ls @uirouter/core

Output:

my-project-packages@0.1.1 
+-- @uirouter/angular@9.1.0
| `-- @uirouter/core@6.0.9
+-- @uirouter/angularjs@1.0.30
| `-- @uirouter/core@6.0.8
`-- @uirouter/rx@1.0.0
  `-- @uirouter/core@6.0.9 deduped

As of npm cli v8.3.0 (2021-12-09) this can be solved using the overrides field of package.json

First add an explicit install of @uirouter/core. I used version 6.0.8 because I am not sure if the latest version 6.0.9 is supported on @uirouter/angularjs.

npm i @uirouter/core@6.0.8 --P -E

Your package.json should now have this line added to it.

"@uirouter/core": "6.0.8",

Next specify that this installed version of @uirouter/core should be used by the dependent libraries. Manually update your package.json and add/update the overrides section.

"overrides": {
  "@uirouter/rx": {
    "@uirouter/core": "$@uirouter/core"
  },
  "@uirouter/angular": {
    "@uirouter/core": "$@uirouter/core"
  }
}

Run your install one more time.

npm i

And finally check the dependent versions one last time.

npm ls @uirouter/core

my-project-packages@0.1.1 
+-- @uirouter/angular@9.1.0 overridden
| `-- @uirouter/core@6.0.8 deduped
+-- @uirouter/angularjs@1.0.30
| `-- @uirouter/core@6.0.8 deduped
+-- @uirouter/core@6.0.8 overridden
`-- @uirouter/rx@1.0.0 overridden
  `-- @uirouter/core@6.0.8 deduped

@eydrian or @mansevmann did you ever solve this?

for angularjs/angular-hybrid when uirouter/core@6.0.8 is explicitly installed, exactly this error will happen! Only works with @uirouter/core@6.0.7

I also think this can be caused by multiple copies of @uirouter libraries in your node_modules

Try running:

yarn why @uirouter/core
yarn why @uirouter/angular
yarn why @uirouter/angularjs
yarn why @uirouter/angular-hybrid

or

npm ls @uirouter/core
npm ls @uirouter/angular
npm ls @uirouter/angularjs
npm ls @uirouter/angular-hybrid

Look for any uirouter packages with two different versions listed.