ava icon indicating copy to clipboard operation
ava copied to clipboard

Project fails running: uncaught exception, exited with a non-zero exit code

Open Tyrben opened this issue 1 year ago • 1 comments

Please provide details about:

  • What you're trying to do Running the tutorial project from the README to test if my set up is OK.

  • Reproduction:

    • npm init ava
    • project.json like in the example
    • test.js like in the example
    • npm test
  • What happened

npm WARN config global --global, --local are deprecated. Use --location=global instead.

> test > ava

Uncaught exception in test.js

SyntaxError: Cannot use import statement outside a module × test.js exited with a non-zero exit code: 1 ─

1 uncaught exception
  • What you expected to happen Tests run and succeed.

This is really trivial. After setting ava in my own project and having the same error, I tried on a blanck projet, just following the example from the README.

Running on Windows, with NodeJS 16.16.0 under a cmd console.

Please share relevant sample code. Or better yet, provide a link to a minimal reproducible example.

We'll also need your AVA configuration (in package.json or ava.config.* configuration files) and how you're invoking AVA. Share the installed AVA version (get it by running npx ava --version).

Tyrben avatar Jul 12 '22 14:07 Tyrben

The example uses ESM syntax, but it does not tell you to configure Node.js to use this.

We could change the file format to test.mjs and it'll work out of the box, or give both ESM and CJS examples, at least in the README. What would have helped you the most @Tyrben?

novemberborn avatar Jul 14 '22 08:07 novemberborn

This is the same issue that I just ran into. Part of the problem is that NodeJS 18 documentation says that every file is treated as a module by NodeJS. I have not looked for NodeJS 16 documentation. Windows Subsystem for Linux (WSL) does not (yet?) support NodeJS 18 (WSL does not have GLIBC_2.28 and so produces an error while trying to install NodeJS 18). In any case, NodeJS is clearly viewing the file the docs say to make (test.js) as NOT a module, and therefore disallowing the import statement.

Renaming test.js to test.mjs solves the problem. I don't personally know what the implications of using .mjs are aside from forcing NodeJS 16 to process the file as a module. If anyone has a link to the ins and outs of NodeJS and overall Javascript handling of files, I'd appreciate it being posted.

dscotese avatar Oct 19 '22 19:10 dscotese