react-speech-to-text icon indicating copy to clipboard operation
react-speech-to-text copied to clipboard

Cannot use import statement outside a module

Open jamesdigid opened this issue 3 years ago • 9 comments

Looking for any suggestions as to why this keeps failing our pipeline build. We have babel transforms all the other modules work correctly am I missing something basic here?

   SyntaxError: Cannot use import statement outside a module
@ss/extension-ivis:       34 | import { FaMicrophone, FaMicrophoneSlash, FaMinus } from 'react-icons/fa';
@ss/extension-ivis:       35 |
@ss/extension-ivis:     > 36 | import useSpeechToText from 'react-hook-speech-to-text';
@ss/extension-ivis:          | ^
@ss/extension-ivis:       37 | //import { useSpeechToText } from 'react-hook-speech-to-text';
@ss/extension-ivis:       38 | //import * as useSpeechToText from 'react-hook-speech-to-text';
@ss/extension-ivis:       39 | //const useSpeechToText = require('react-hook-speech-to-text');
@ss/extension-ivis:       at ScriptTransformer._transformAndBuildScript (../../node_modules/@jest/transform/build/ScriptTransformer.js:537:17)
@ss/extension-ivis:       at ScriptTransformer.transform (../../node_modules/@jest/transform/build/ScriptTransformer.js:579:25)

jamesdigid avatar Oct 26 '21 20:10 jamesdigid

+1

This is also happening to me

javiermanzano avatar Nov 28 '21 14:11 javiermanzano

@javiermanzano @jamesdigid Is this happening with Next.js, create-react-app or something else?

Riley-Brown avatar Dec 04 '21 19:12 Riley-Brown

+1

This is also happening to me

I eventually found a work around by adding to our jest.config.js:

transformIgnorePatterns: [ "node_modules/(?!react-hook-speech-to-text/.*)"],

@Riley-Brown This was happening inside a react mono repo application built off of OHIF.

jamesdigid avatar Dec 04 '21 20:12 jamesdigid

@Riley-Brown This is also happening to me. (Next.js)

Server Error
SyntaxError: Cannot use import statement outside a module

===================================

external%20%22react-hook-speech-to-text%22 (1:0) @ eval

> 1 | module.exports = require("react-hook-speech-to-text");

スクリーンショット 2021-12-25 15 24 15

I ran into the above error while trying to call useSpeechToText.

    const {
        error,
        interimResult,
        isRecording,
        results,
        startSpeechToText,
        stopSpeechToText,
    } = useSpeechToText({
        continuous: true,
        useLegacyResults: false
    })

k-masashi avatar Dec 25 '21 06:12 k-masashi

This Next.js problem was solved by using next-transpile-modules. https://github.com/Riley-Brown/react-speech-to-text/issues/22#issuecomment-1000980832

next.config.js

const withTM = require('next-transpile-modules')(['react-hook-speech-to-text']);

withTM({.. config ..})

k-masashi avatar Dec 25 '21 07:12 k-masashi

You sure this has been fixed?

There is still a bug with this config

next.config.js

/** @type {import('next').NextConfig} */
const nextConfig = {
  reactStrictMode: true,
}

const withTM = require('next-transpile-modules')(['react-hook-speech-to-text']);

module.exports = withTM({ nextConfig });

Is currently returning:

./node_modules/react-hook-speech-to-text/dist/index.js
Module parse failed: Identifier '_b' has already been declared (696:12)
File was processed with these loaders:
 * ./node_modules/@next/react-refresh-utils/loader.js
 * ./node_modules/next/dist/build/webpack/loaders/next-swc-loader.js
You may need an additional loader to handle the result of these loaders.
| 
| ;
>     var _a, _b;
|     // Legacy CSS implementations will `eval` browser code in a Node.js context
|     // to extract CSS. For backwards compatibility, we need to check we're in a

Any ideas?

28development avatar Jan 23 '22 23:01 28development

For NextJS, don't worry about "next-transpile-modules". Put the code from the example into its own SpeechToText component, then load that component dynamically with SSR turned off:

import dynamic from "next/dynamic";
const SpeechToText = dynamic(() => import("../Library/SpeechToText"), {
  ssr: false,
});

const MyApp = () => {
    return (
         <SpeechToText />
    );
};

export default MyApp;

JonathanGallivan avatar Apr 28 '22 13:04 JonathanGallivan

Still facing this issue with nextjs and nothing seems to work. Can anyone suggest a way forward

abiolapunch avatar Jun 07 '22 13:06 abiolapunch

For NextJS, don't worry about "next-transpile-modules". Put the code from the example into its own SpeechToText component, then load that component dynamically with SSR turned off:

import dynamic from "next/dynamic";
const SpeechToText = dynamic(() => import("../Library/SpeechToText"), {
  ssr: false,
});

const MyApp = () => {
    return (
         <SpeechToText />
    );
};

export default MyApp;

it works for me thank you :)

bob-kim1 avatar Dec 11 '23 13:12 bob-kim1