question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"Cannot read property 'lastIndexOf' of undefined" when using import in typescript plugins file

See original GitHub issue

Current behavior:

It seems as soon as I am trying to use an import { ... } from '...' (import a separate .ts Typescript file from the project to be tested) in a plugins/index.ts, cypress fails directly when executing cypress open with the following error and stack trace: image

Message: Cannot read property ‘lastIndexOf’ of undefined

Stack trace:

TypeError: Cannot read property 'lastIndexOf' of undefined
    at Object.startsWith (C:\Users\me\AppData\Local\Cypress\Cache\4.7.0\Cypress\resources\app\packages\server\node_modules\typescript\lib\typescript.js:2060:20)
    at mangleScopedPackageName (C:\Users\me\AppData\Local\Cypress\Cache\4.7.0\Cypress\resources\app\packages\server\node_modules\typescript\lib\typescript.js:30540:16)
    at mangleScopedPackageNameWithTrace (C:\Users\me\AppData\Local\Cypress\Cache\4.7.0\Cypress\resources\app\packages\server\node_modules\typescript\lib\typescript.js:30527:23)
    at loadModuleFromImmediateNodeModulesDirectory (C:\Users\me\AppData\Local\Cypress\Cache\4.7.0\Cypress\resources\app\packages\server\node_modules\typescript\lib\typescript.js:30458:83)
    at C:\Users\me\AppData\Local\Cypress\Cache\4.7.0\Cypress\resources\app\packages\server\node_modules\typescript\lib\typescript.js:30435:39
    at Object.forEachAncestorDirectory (C:\Users\me\AppData\Local\Cypress\Cache\4.7.0\Cypress\resources\app\packages\server\node_modules\typescript\lib\typescript.js:6784:26)
    at loadModuleFromNearestNodeModulesDirectoryWorker (C:\Users\me\AppData\Local\Cypress\Cache\4.7.0\Cypress\resources\app\packages\server\node_modules\typescript\lib\typescript.js:30429:19)
    at loadModuleFromNearestNodeModulesDirectoryTypesScope (C:\Users\me\AppData\Local\Cypress\Cache\4.7.0\Cypress\resources\app\packages\server\node_modules\typescript\lib\typescript.js:30425:16)
    at tryResolve (C:\Users\me\AppData\Local\Cypress\Cache\4.7.0\Cypress\resources\app\packages\server\node_modules\typescript\lib\typescript.js:30604:28)
    at classicNameResolver (C:\Users\me\AppData\Local\Cypress\Cache\4.7.0\Cypress\resources\app\packages\server\node_modules\typescript\lib\typescript.js:30580:24)
    at Object.resolveModuleName (C:\Users\me\AppData\Local\Cypress\Cache\4.7.0\Cypress\resources\app\packages\server\node_modules\typescript\lib\typescript.js:29897:30)
    at tsLookup (C:\Users\me\AppData\Local\Cypress\Cache\4.7.0\Cypress\resources\app\packages\server\node_modules\filing-cabinet\index.js:221:26)
    at cabinet (C:\Users\me\AppData\Local\Cypress\Cache\4.7.0\Cypress\resources\app\packages\server\node_modules\filing-cabinet\index.js:75:18)
    at Function.module.exports._getDependencies (C:\Users\me\AppData\Local\Cypress\Cache\4.7.0\Cypress\resources\app\packages\server\node_modules\dependency-tree\index.js:104:20)
    at traverse (C:\Users\me\AppData\Local\Cypress\Cache\4.7.0\Cypress\resources\app\packages\server\node_modules\dependency-tree\index.js:149:37)
    at traverse (C:\Users\me\AppData\Local\Cypress\Cache\4.7.0\Cypress\resources\app\packages\server\node_modules\dependency-tree\index.js:171:24)
    at traverse (C:\Users\me\AppData\Local\Cypress\Cache\4.7.0\Cypress\resources\app\packages\server\node_modules\dependency-tree\index.js:171:24)
    at traverse (C:\Users\me\AppData\Local\Cypress\Cache\4.7.0\Cypress\resources\app\packages\server\node_modules\dependency-tree\index.js:171:24)
    at traverse (C:\Users\me\AppData\Local\Cypress\Cache\4.7.0\Cypress\resources\app\packages\server\node_modules\dependency-tree\index.js:171:24)
    at traverse (C:\Users\me\AppData\Local\Cypress\Cache\4.7.0\Cypress\resources\app\packages\server\node_modules\dependency-tree\index.js:171:24)
    at traverse (C:\Users\me\AppData\Local\Cypress\Cache\4.7.0\Cypress\resources\app\packages\server\node_modules\dependency-tree\index.js:171:24)
    at traverse (C:\Users\me\AppData\Local\Cypress\Cache\4.7.0\Cypress\resources\app\packages\server\node_modules\dependency-tree\index.js:171:24)
    at traverse (C:\Users\me\AppData\Local\Cypress\Cache\4.7.0\Cypress\resources\app\packages\server\node_modules\dependency-tree\index.js:171:24)
    at traverse (C:\Users\me\AppData\Local\Cypress\Cache\4.7.0\Cypress\resources\app\packages\server\node_modules\dependency-tree\index.js:171:24)
    at traverse (C:\Users\me\AppData\Local\Cypress\Cache\4.7.0\Cypress\resources\app\packages\server\node_modules\dependency-tree\index.js:171:24)
    at traverse (C:\Users\me\AppData\Local\Cypress\Cache\4.7.0\Cypress\resources\app\packages\server\node_modules\dependency-tree\index.js:171:24)
    at traverse (C:\Users\me\AppData\Local\Cypress\Cache\4.7.0\Cypress\resources\app\packages\server\node_modules\dependency-tree\index.js:171:24)
    at traverse (C:\Users\me\AppData\Local\Cypress\Cache\4.7.0\Cypress\resources\app\packages\server\node_modules\dependency-tree\index.js:171:24)
    at traverse (C:\Users\me\AppData\Local\Cypress\Cache\4.7.0\Cypress\resources\app\packages\server\node_modules\dependency-tree\index.js:171:24)
    at traverse (C:\Users\me\AppData\Local\Cypress\Cache\4.7.0\Cypress\resources\app\packages\server\node_modules\dependency-tree\index.js:171:24)
    at traverse (C:\Users\me\AppData\Local\Cypress\Cache\4.7.0\Cypress\resources\app\packages\server\node_modules\dependency-tree\index.js:171:24)
    at traverse (C:\Users\me\AppData\Local\Cypress\Cache\4.7.0\Cypress\resources\app\packages\server\node_modules\dependency-tree\index.js:171:24)
    at traverse (C:\Users\me\AppData\Local\Cypress\Cache\4.7.0\Cypress\resources\app\packages\server\node_modules\dependency-tree\index.js:171:24)
    at traverse (C:\Users\me\AppData\Local\Cypress\Cache\4.7.0\Cypress\resources\app\packages\server\node_modules\dependency-tree\index.js:171:24)
    at traverse (C:\Users\me\AppData\Local\Cypress\Cache\4.7.0\Cypress\resources\app\packages\server\node_modules\dependency-tree\index.js:171:24)
    at Module.module.exports (C:\Users\me\AppData\Local\Cypress\Cache\4.7.0\Cypress\resources\app\packages\server\node_modules\dependency-tree\index.js:35:19)
    at Function.module.exports.toList (C:\Users\me\AppData\Local\Cypress\Cache\4.7.0\Cypress\resources\app\packages\server\node_modules\dependency-tree\index.js:72:17)
    at Watchers.watchTree (C:\Users\me\AppData\Local\Cypress\Cache\4.7.0\Cypress\resources\app\packages\server\lib\watchers.js:60:30)
    at C:\Users\me\AppData\Local\Cypress\Cache\4.7.0\Cypress\resources\app\packages\server\lib\project.js:272:28

Desired behavior:

import {...} from '...' can be used in plugins/index.ts.

Test code to reproduce

  1. Add import {...} from '../../myproject/any/file.ts' to the top of your plugins/index.ts` Note: The import must not be used at all, it seems having it is enough to break displaying the tests list.
  2. tsconfig in cypress directory is
{
  "compilerOptions": {
    "strict": true,
    "baseUrl": "../node_modules",
    "target": "es5",
    "lib": ["es5", "dom"],
    "types": ["cypress"]
  },
  "include": [
    "**/*.ts"
  ]
}

Versions

Cypress 4.7.0

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
capajcommented, Apr 2, 2021

@jennifer-shehane I got this error on my machine with cypress 6.8.0 when I had this line in my code:

 const { messages } = await import(`../locale/${kebabCaseLocale}/messages`)

as soon as I removed it, cypress worked fine. I worked around this by replacing it with require for now, but require sucks, bucause they are not type safe in TS. Dynamic imports are.

Do you have any spec with a dynamic import in place here in the repository?

1reaction
capajcommented, Apr 2, 2021

Also should I open a new bug for this dynamic import issue?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Documentation - Modules - TypeScript
Modules are declarative; the relationships between modules are specified in terms of imports and exports at the file level. Modules import one another...
Read more >
Cannot read property 'indexOf' of undefined - Stack Overflow
The problem is that you're modifying the array while jQuery's $.each is looping over it, so by the time it gets to the...
Read more >
Issue with 4.9.0 + webpack + typescript
app.js:28654 Uncaught TypeError: Cannot assign to read only property 'exports' of object '#<Object>' at Object.<anonymous> (app.js:28654) at Object.push.
Read more >
typeerror: cannot read properties of undefined ... - You.com
Reason : As there is no named export of ReactDOM from "react-dom", it is not imported and the reference will be undefined. So,...
Read more >
rewiremock - npm
js file and require it - in this way, you can preconfigure rewiremock for all tests. for ts/es6/esm use import. // rewiremock.es6.js import...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found