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.

plugins.js - async/await causes invalid import errors

See original GitHub issue

Current behavior:

Using async within a plugins.ts file results in a (non-informative) error.

**Message:** The plugins file is missing or invalid.

Your `pluginsFile` is set to `/srv/mossaino/common/asset_files/pwa/tests/plugins/index.ts`, but either the file is missing, it contains a syntax error, or threw an error when required. The `pluginsFile` must be a `.js` or `.coffee` file.

Or you might have renamed the extension of your `pluginsFile` to `.ts`. If that's the case, restart the test runner.

Please fix this, or set `pluginsFile` to `false` if a plugins file is not necessary for your project.

**Details:** /srv/mossaino/common/asset_files/pwa/tests/plugins/index.ts:13
import { __awaiter } from "tslib";
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at Module._compile (internal/modules/cjs/loader.js:896:18)
    at Module.m._compile (/home/ebruines/.cache/Cypress/4.4.0/Cypress/resources/app/packages/server/node_modules/ts-node/src/index.ts:536:23)
    at Module._extensions..js (internal/modules/cjs/loader.js:986:10)
    at Object.require.extensions.<computed> [as .ts] (/home/ebruines/.cache/Cypress/4.4.0/Cypress/resources/app/packages/server/node_modules/ts-node/src/index.ts:539:12)
    at Module.load (internal/modules/cjs/loader.js:816:32)
    at Module._load (internal/modules/cjs/loader.js:728:14)
    at Module._load (electron/js2c/asar.js:717:26)
    at Function.Module._load (electron/js2c/asar.js:717:26)
    at Module.require (internal/modules/cjs/loader.js:853:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at module.exports (/home/ebruines/.cache/Cypress/4.4.0/Cypress/resources/app/packages/server/lib/plugins/child/run_plugins.js:206:15)
    at Object.<anonymous> (/home/ebruines/.cache/Cypress/4.4.0/Cypress/resources/app/packages/server/lib/plugins/child/index.js:9:25)
    at Module._compile (internal/modules/cjs/loader.js:968:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:986:10)
    at Module.load (internal/modules/cjs/loader.js:816:32)
    at Module._load (internal/modules/cjs/loader.js:728:14)

**Stack trace:**

[object Object]
    at Object.get (/home/ebruines/.cache/Cypress/4.4.0/Cypress/resources/app/packages/server/lib/errors.js:348:11)
    at EventEmitter.<anonymous> (/home/ebruines/.cache/Cypress/4.4.0/Cypress/resources/app/packages/server/lib/plugins/index.js:121:21)
    at EventEmitter.emit (events.js:210:5)
    at ChildProcess.<anonymous> (/home/ebruines/.cache/Cypress/4.4.0/Cypress/resources/app/packages/server/lib/plugins/util.js:25:29)
    at ChildProcess.emit (events.js:210:5)
    at emit (internal/child_process.js:876:12)
    at processTicksAndRejections (internal/process/task_queues.js:81:21)
From previous event:
    at fn (file:///home/ebruines/.cache/Cypress/4.4.0/Cypress/resources/app/packages/desktop-gui/dist/app.js:88741:14)
    at ipcBus (file:///home/ebruines/.cache/Cypress/4.4.0/Cypress/resources/app/packages/desktop-gui/dist/app.js:88760:10)
    at Object.ipc.<computed> [as openProject] (file:///home/ebruines/.cache/Cypress/4.4.0/Cypress/resources/app/packages/desktop-gui/dist/app.js:88801:69)
    at Object.openProject (file:///home/ebruines/.cache/Cypress/4.4.0/Cypress/resources/app/packages/desktop-gui/dist/app.js:91213:67)
    at Project.componentDidMount (file:///home/ebruines/.cache/Cypress/4.4.0/Cypress/resources/app/packages/desktop-gui/dist/app.js:90797:78)
    at ik (file:///home/ebruines/.cache/Cypress/4.4.0/Cypress/resources/app/packages/desktop-gui/dist/app.js:75383:360)
    at ../../node_modules/scheduler/cjs/scheduler.production.min.js.exports.unstable_runWithPriority (file:///home/ebruines/.cache/Cypress/4.4.0/Cypress/resources/app/packages/desktop-gui/dist/app.js:85486:437)
    at fg (file:///home/ebruines/.cache/Cypress/4.4.0/Cypress/resources/app/packages/desktop-gui/dist/app.js:75252:325)
    at Yj (file:///home/ebruines/.cache/Cypress/4.4.0/Cypress/resources/app/packages/desktop-gui/dist/app.js:75376:437)
    at Lj (file:///home/ebruines/.cache/Cypress/4.4.0/Cypress/resources/app/packages/desktop-gui/dist/app.js:75355:420)
    at file:///home/ebruines/.cache/Cypress/4.4.0/Cypress/resources/app/packages/desktop-gui/dist/app.js:75253:115
    at ../../node_modules/scheduler/cjs/scheduler.production.min.js.exports.unstable_runWithPriority (file:///home/ebruines/.cache/Cypress/4.4.0/Cypress/resources/app/packages/desktop-gui/dist/app.js:85486:437)
    at fg (file:///home/ebruines/.cache/Cypress/4.4.0/Cypress/resources/app/packages/desktop-gui/dist/app.js:75252:325)
    at ig (file:///home/ebruines/.cache/Cypress/4.4.0/Cypress/resources/app/packages/desktop-gui/dist/app.js:75253:61)
    at jg (file:///home/ebruines/.cache/Cypress/4.4.0/Cypress/resources/app/packages/desktop-gui/dist/app.js:75252:496)
    at bk (file:///home/ebruines/.cache/Cypress/4.4.0/Cypress/resources/app/packages/desktop-gui/dist/app.js:75356:156)
    at reactionScheduler (file:///home/ebruines/.cache/Cypress/4.4.0/Cypress/resources/app/packages/desktop-gui/dist/app.js:48924:47)
    at runReactions (file:///home/ebruines/.cache/Cypress/4.4.0/Cypress/resources/app/packages/desktop-gui/dist/app.js:48900:5)
    at endBatch (file:///home/ebruines/.cache/Cypress/4.4.0/Cypress/resources/app/packages/desktop-gui/dist/app.js:48600:9)
From previous event:
    at App.componentDidMount (file:///home/ebruines/.cache/Cypress/4.4.0/Cypress/resources/app/packages/desktop-gui/dist/app.js:86770:77)
    at ik (file:///home/ebruines/.cache/Cypress/4.4.0/Cypress/resources/app/packages/desktop-gui/dist/app.js:75383:360)
    at ../../node_modules/scheduler/cjs/scheduler.production.min.js.exports.unstable_runWithPriority (file:///home/ebruines/.cache/Cypress/4.4.0/Cypress/resources/app/packages/desktop-gui/dist/app.js:85486:437)
    at fg (file:///home/ebruines/.cache/Cypress/4.4.0/Cypress/resources/app/packages/desktop-gui/dist/app.js:75252:325)
    at Yj (file:///home/ebruines/.cache/Cypress/4.4.0/Cypress/resources/app/packages/desktop-gui/dist/app.js:75376:437)
    at Lj (file:///home/ebruines/.cache/Cypress/4.4.0/Cypress/resources/app/packages/desktop-gui/dist/app.js:75355:420)
    at Rg (file:///home/ebruines/.cache/Cypress/4.4.0/Cypress/resources/app/packages/desktop-gui/dist/app.js:75346:137)
    at rk (file:///home/ebruines/.cache/Cypress/4.4.0/Cypress/resources/app/packages/desktop-gui/dist/app.js:75411:43)
    at file:///home/ebruines/.cache/Cypress/4.4.0/Cypress/resources/app/packages/desktop-gui/dist/app.js:75414:301
    at ck (file:///home/ebruines/.cache/Cypress/4.4.0/Cypress/resources/app/packages/desktop-gui/dist/app.js:75356:210)
    at zk (file:///home/ebruines/.cache/Cypress/4.4.0/Cypress/resources/app/packages/desktop-gui/dist/app.js:75414:287)
    at render (file:///home/ebruines/.cache/Cypress/4.4.0/Cypress/resources/app/packages/desktop-gui/dist/app.js:75418:416)
    at Object.start (file:///home/ebruines/.cache/Cypress/4.4.0/Cypress/resources/app/packages/desktop-gui/dist/app.js:89373:61)
    at file:///home/ebruines/.cache/Cypress/4.4.0/Cypress/resources/app/packages/desktop-gui/dist/index.html:29:11

Desired behavior:

No errors, as well as being able to use async.

Test code to reproduce

plugins.ts

module.exports = (on: Cypress.PluginEvents, config: Cypress.PluginConfig) => {
  on('task', {
    'hello-world': () => async () {
      return true
    }
  })
}

and alternatively

plugins.js

module.exports = (on, config) => {
  on('task', {
    'hello-world': () => async () {
      return true
    }
  })
}

Versions

Note that the plugins.js file mentioned, was valid code in Cypress 4.3.0.

Cypress: 4.4.0, 4.4.1, 4.5.0
OS: Linux
Browser: not applicable

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:11 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
garvikcommented, Apr 29, 2020

I’m having similar problems when setting “importHelpers” to true in the tsconfig.json file, although I’m not 100% sure this is the same issue, given that my plugins file is still a “.js”. Setting “importHelpers” to false in cypress’ tsconfig.json does not help.

I can prepare a minimal reproduction if you think it’d be helpful in any way.

1reaction
EtienneBruinescommented, Apr 30, 2020

Tested with the latest on develop, b515e036dd0c0147a6e699ab252d5571d4420002 at the moment.

I have narrowed it down to this minimal tsconfig.json:

{
  "compilerOptions": {
    "module": "esnext",
    "allowJs": true,
    "importHelpers": true
  }
}

Removing allowJs -> compiles. Removing importHelpers -> compiles. But those combined, causes issues.

import { __awaiter, __generator } from "tslib";
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at Module._compile (internal/modules/cjs/loader.js:896:18)
    at Module.m._compile (/home/ebruines/.cache/Cypress/4.5.1/Cypress/resources/app/packages/server/node_modules/ts-node/src/index.ts:536:23)
    at Module._extensions..js (internal/modules/cjs/loader.js:986:10)
    at Object.require.extensions.<computed> [as .js] (/home/ebruines/.cache/Cypress/4.5.1/Cypress/resources/app/packages/server/node_modules/ts-node/src/index.ts:539:12)
    at Module.load (internal/modules/cjs/loader.js:816:32)
    at Module._load (internal/modules/cjs/loader.js:728:14)
    at Module._load (electron/js2c/asar.js:717:26)
    at Function.Module._load (electron/js2c/asar.js:717:26)
    at Module.require (internal/modules/cjs/loader.js:853:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at module.exports (/home/ebruines/.cache/Cypress/4.5.1/Cypress/resources/app/packages/server/lib/plugins/child/run_plugins.js:206:15)
    at Object.<anonymous> (/home/ebruines/.cache/Cypress/4.5.1/Cypress/resources/app/packages/server/lib/plugins/child/index.js:9:25)
    at Module._compile (internal/modules/cjs/loader.js:968:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:986:10)
    at Module.load (internal/modules/cjs/loader.js:816:32)
    at Module._load (internal/modules/cjs/loader.js:728:14)

Having no tsconfig.json at all, does make it compile correctly as well.

Full tsconfig.json - just for those that are interested

{
  "compilerOptions": {
    "experimentalDecorators": true,
    "outDir": "./dist/",
    "sourceMap": true,
    "noImplicitAny": true,
    "module": "esnext",
    "target": "ES2016",
    "jsx": "react",
    "allowJs": true,
    "moduleResolution": "node",
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "importHelpers": true,
    "lib": [
      "dom",
      "webworker",
      "es2015.promise"
    ]
  },
  "include": [
    "**/*.ts",
    "**/*.vue"
  ],
  "exclude": [
    "node_modules"
  ],
  "files": [
    "ts-shim.d.ts"
  ]
}

Note that I’m testing this for plugin.js - a JavaScript file, not a TypeScript file. Not sure if it makes a difference

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error in global call to async function: "await is only valid in ...
The top-level await means that you are trying to use async/await syntax outside async function. The workaround is to create some function ...
Read more >
async function - JavaScript - MDN Web Docs - Mozilla
The async function declaration declares an async function where the await keyword is permitted within the function body. The async and await ...
Read more >
Javascript Async Await with Loops, Error Handling, Promise ...
Today we look at a more complex async-await case with loops. We use a common Promise.all.map technique and we look at how to...
Read more >
Asynchronous programming: futures, async, await - Dart
If the asynchronous operation performed by the function fails for any reason, the future completes with an error. Example: Introducing futures. In the...
Read more >
async/await is the wrong abstraction - LogRocket Blog
What we are trying to do with async/await is to ignore reality and have ... LogRocket records console logs, JavaScript errors, stacktraces, ...
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