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

Error running tests: SyntaxError: Cannot use import statement outside a module #1

Open
eatrocks opened this issue Sep 18, 2023 · 1 comment

Comments

@eatrocks
Copy link

I get an error running the tests...
SyntaxError: Cannot use import statement outside a module

Steps to reproduce:

  • clone the repository
  • cd into the project directory and run npm i
  • run npm run test

I also get an error trying to do the following in Node, which I believe is possibly related...

  • Using Node 18 LTS create a simple project and
  • run "npm i comet-js-sdk"
  • create a file with the "js" extension
  • import the library like this... import comet from "comet-js-sdk"; or import { CometServer } from "comet-js-sdk";
  • run the file with node
    The error is
    Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/path/to/my-project/node_modules/comet-js-sdk/dist/mjs/lib/output' imported from /path/to/my-project/node_modules/comet-js-sdk/dist/mjs/index.js
@mason-cometbackup
Copy link
Contributor

Thanks for raising this issue - both problems are caused by running in script mode instead of module mode.

I can confirm the issue with the Jest tests. To run the tests in module mode, try this (for Node 16 or 18):

NODE_OPTIONS=--experimental-vm-modules npm run test

For your test app, you can either

  • (A) get node.js into Module mode by renaming your .js file to .mjs and running with the --experimental-vm-modules argument; or
  • (B) embrace the classic script mode, and load the SDK with require instead of import, using the provided CommonJS instructions: const { CometServer } = require('comet-js-sdk')

Hope this helps - please let us know if you manage to resolve the issue,

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

No branches or pull requests

2 participants