awesome-cordova-plugins icon indicating copy to clipboard operation
awesome-cordova-plugins copied to clipboard

Cannot use the barcode-scanner plugin in an ESM project: Cannot use import statement outside a module

Open TheOnlyTails opened this issue 3 years ago • 1 comments

I'm submitting a ... (check one with "x")

  • [x] bug report
  • [ ] feature request
  • [ ] support request => Please do not submit support requests here, use one of these channels: https://forum.ionicframework.com/ or https://ionicworldwide.herokuapp.com/

Current behavior: I have a Svelte + Capacitor project, and I've installed the barcode-scanner plugin. I made a utility function to scan a QR code and return the data, but when I build the project, I get the following error:

(node:9589) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
/home/theonlytails/IdeaProjects/archery-scorekeeper/node_modules/.pnpm/@[email protected]_zuvf63os36jyc5em5biagvpyka/node_modules/@awesome-cordova-plugins/barcode-scanner/index.js:14
import { AwesomeCordovaNativePlugin, cordova } from '@awesome-cordova-plugins/core';
^^^^^^

SyntaxError: Cannot use import statement outside a module
<stacktrace>

Expected behavior:

It should work without issues.

Steps to reproduce:

  1. Install the barcode-scanner plugin.
  2. Import it into an ESM project.
  3. Get an error

Related code:

import { BarcodeScanner } from "@awesome-cordova-plugins/barcode-scanner"

export const startScan = async () => await BarcodeScanner.scan().then(r => r.text)

Other information:

Ionic info::

Ionic:

   Ionic CLI : 6.20.1

Utility:

   cordova-res : not installed globally
   native-run  : not installed globally

System:

   NodeJS : v18.1.0
   npm    : 8.15.1
   OS     : Linux 5.18

TheOnlyTails avatar Aug 17 '22 12:08 TheOnlyTails

I have the exact same issue with the local-notifications plugin, is there a workaround at least ?

@awesome-cordova-plugins/local-notifications
import { AwesomeCordovaNativePlugin, cordova } from '@awesome-cordova-plugins/core';
    ^^^^^^

    SyntaxError: Cannot use import statement outside a module

devicenul1 avatar Sep 06 '22 20:09 devicenul1

same for purchases. very annoying. you can add "type":"module" to the package.json of the plugin, that should help in the meantime (of course gets overwritten once you install the module fresh)

eikaramba avatar Oct 05 '22 19:10 eikaramba

There has been no recent activity and this issue has been marked inactive.

github-actions[bot] avatar Dec 05 '22 00:12 github-actions[bot]

This problem came up for me in a stack using Next.js. Ultimate solution was to dynamically import this external package using await and import, as shown in their docs' example here.

mepc36 avatar Dec 12 '22 16:12 mepc36

There has been no recent activity and this issue has been marked inactive.

github-actions[bot] avatar Feb 12 '23 00:02 github-actions[bot]

Still happens for me as well:

mx code:

import {
    InAppBrowser,
    InAppBrowserEvent,
    InAppBrowserObject,
} from '@awesome-cordova-plugins/in-app-browser';

compiler error:

Test suite failed to run
    Jest encountered an unexpected token
    Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.
    Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.
    By default "node_modules" folder is ignored by transformers.
    Here's what you can do:
     • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
     • If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
     • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
     • If you need a custom transformation specify a "transform" option in your config.
     • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.
    You'll find more details and examples of these config options in the docs:
    https://jestjs.io/docs/configuration
    For information about custom transformations, see:
    https://jestjs.io/docs/code-transformation
    Details:
    /builds/my-project/node_modules/@awesome-cordova-plugins/in-app-browser/index.js:21
    import { AwesomeCordovaNativePlugin, instanceAvailability, cordovaInstance } from '@awesome-cordova-plugins/core';
    ^^^^^^
    SyntaxError: Cannot use import statement outside a module
      1 | import { Environment } from '../environment.global';
    > 2 | import {
        | ^
      3 |     InAppBrowser,
      4 |     InAppBrowserEvent,
      5 |     InAppBrowserObject,
      at Runtime.createScriptFromCode (../../node_modules/jest-runner/node_modules/jest-runtime/build/index.js:1449:14)

ErikBrendel avatar Apr 05 '23 09:04 ErikBrendel

Any updates on this? having the same issue with

import { InAppPurchase2 as iap, IAPProduct } from '@awesome-cordova-plugins/in-app-purchase-2';

adamcohenhillel avatar Jun 24 '23 14:06 adamcohenhillel