Search code examples
androidnode.jsreact-nativenpmbabeljs

SyntaxError: Cannot use import statement outside a module | on android :app:bundleReleaseJsAndAssets


I am trying to upgrade my project from RN v0.61.4 to v0.63.4. I am able to run debug builds on android but release builds are throwing 1 error in gradle task :app:bundleReleaseJsAndAssets

Stacktrace

(node:77926) 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)
/Users/me/Documents/GitHub/MyProject/android/react-native/node_modules/react-native/index.js:13
import typeof AccessibilityInfo from './Libraries/Components/AccessibilityInfo/AccessibilityInfo';
^^^^^^

SyntaxError: Cannot use import statement outside a module
at wrapSafe (internal/modules/cjs/loader.js:1117:16)
at Module._compile (internal/modules/cjs/loader.js:1165:27)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1221:10)
at Module.load (internal/modules/cjs/loader.js:1050:32)
at Function.Module._load (internal/modules/cjs/loader.js:938:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
at internal/main/run_main_module.js:17:47

My project structure is a bit different from usual. As the react native root folder lies inside android root folder. I have made necessary changes in app/build.gradle to handle that.

I even tried creating a fresh project with similar structure as mine and I am still getting this issue.

Attaching minimum reproducible code for the same.

Sample Project

Steps to reproduce

cd react-native
npm i
npx react-native run-android --variant=release

Solution

  • Ok I figured it out. It was the CLI path that was wrong in app/build.gradle replaced

    cliPath : "node_modules/react-native",
    

    with

    cliPath : "node_modules/react-native/cli.js",