React-native: main.jsbundle does not exist. This must be a bug with React Native. metro/src/lib/polyfills/require.js: Unexpected token, expected ","

Created on 25 Jun 2019  Â·  8Comments  Â·  Source: facebook/react-native

Archive in XCode lead to the error:
main.jsbundle does not exist. This must be a bug with React Native. metro/src/lib/polyfills/require.js: Unexpected token, expected ","

React Native version:

info 
  React Native Environment Info:
    System:
      OS: macOS 10.14.5
      CPU: x64 Intel(R) Core(TM) i7-6820HQ CPU @ 2.70GHz
      Memory: 257.27 MB / 16.00 GB
      Shell: 5.3 - /bin/zsh
    Binaries:
      Node: 10.14.1 - ~/.nvm/versions/node/v10.14.1/bin/node
      Yarn: 1.16.0 - ~/.yarn/bin/yarn
      npm: 6.4.1 - ~/.nvm/versions/node/v10.14.1/bin/npm
      Watchman: 4.9.0 - /usr/local/bin/watchman
    SDKs:
      iOS SDK:
        Platforms: iOS 12.2, macOS 10.14, tvOS 12.2, watchOS 5.2
    IDEs:
      Android Studio: 3.3 AI-182.5107.16.33.5264788
      Xcode: 10.2.1/10E1001 - /usr/bin/xcodebuild
    npmPackages:
      @storybook/react: ^5.0.11 => 5.0.11 
      @storybook/react-native: ^4.1.18 => 4.1.18 
      react: 16.8.3 => 16.8.3 
      react-native: 0.59.8 => 0.59.8 
    npmGlobalPackages:
      react-native-cli: 2.0.1

Steps To Reproduce

  1. XCode 10.2.1 (10E1001) > Product > Archive
  2. Got error:
+ BUNDLE_FILE=/Users/<mac-user-name>/Library/Developer/Xcode/DerivedData/********-dnnaonnrphjpphhhgmjunutlpwgi/Build/Intermediates.noindex/ArchiveIntermediates/********/BuildProductsPath/Release-iphoneos/********.app/main.jsbundle
+ node /opt/local/var/www/********/********-app/node_modules/react-native/cli.js bundle --entry-file index.js --platform ios --dev false --reset-cache --bundle-output /Users/<mac-user-name>/Library/Developer/Xcode/DerivedData/********-dnnaonnrphjpphhhgmjunutlpwgi/Build/Intermediates.noindex/ArchiveIntermediates/********/BuildProductsPath/Release-iphoneos/********.app/main.jsbundle --assets-dest /Users/<mac-user-name>/Library/Developer/Xcode/DerivedData/********-dnnaonnrphjpphhhgmjunutlpwgi/Build/Intermediates.noindex/ArchiveIntermediates/********/BuildProductsPath/Release-iphoneos/********.app
warning: the transform cache was reset.
Loading dependency graph, done.
error SyntaxError: /opt/local/var/www/********/********-app/node_modules/metro/src/lib/polyfills/require.js: Unexpected token, expected "," (341:5)

  339 |       _moduleObject.exports,
  340 |       dependencyMap
> 341 |     ); // avoid removing factory in DEV mode as it breaks HMR
      |      ^
  342 | 
  343 |     if (!__DEV__) {
  344 |       // $FlowFixMe: This is only sound because we never access `factory` again. Run CLI with --verbose flag for more details.
+ [[ false != true ]]
+ [[ ! -f /Users/<mac-user-name>/Library/Developer/Xcode/DerivedData/********-dnnaonnrphjpphhhgmjunutlpwgi/Build/Intermediates.noindex/ArchiveIntermediates/********/BuildProductsPath/Release-iphoneos/********.app/main.jsbundle ]]
+ echo 'error: File /Users/<mac-user-name>/Library/Developer/Xcode/DerivedData/********-dnnaonnrphjpphhhgmjunutlpwgi/Build/Intermediates.noindex/ArchiveIntermediates/********/BuildProductsPath/Release-iphoneos/********.app/main.jsbundle does not exist. This must be a bug with'
error: File /Users/<mac-user-name>/Library/Developer/Xcode/DerivedData/********-dnnaonnrphjpphhhgmjunutlpwgi/Build/Intermediates.noindex/ArchiveIntermediates/********/BuildProductsPath/Release-iphoneos/********.app/main.jsbundle does not exist. This must be a bug with
+ echo 'React Native, please report it here: https://github.com/facebook/react-native/issues'
React Native, please report it here: https://github.com/facebook/react-native/issues
+ exit 2

node_modules/metro/package.json

  "version": "0.51.1",
  "name": "metro",
  ...
  "repository": {
    "type": "git",
    "url": "[email protected]:facebook/metro.git"
  },

node_modules/metro/src/lib/polyfills/require.js

    if (hooks.length > 0) {
      for (var i = 0; i < hooks.length; ++i) {
        hooks[i].cb(moduleId, _moduleObject);
      }
    } // keep args in sync with with defineModuleCode in
    // metro/src/Resolver/index.js
    // and metro/src/ModuleGraph/worker.js

    factory(
      global,
      metroRequire,
      metroImportDefault,
      metroImportAll,
      _moduleObject,
      _moduleObject.exports,
      dependencyMap
    ); // avoid removing factory in DEV mode as it breaks HMR

    if (!__DEV__) {
      // $FlowFixMe: This is only sound because we never access `factory` again
      module.factory = undefined;
      module.dependencyMap = undefined;
    }

meanwhile react-native run-ios works without any errors.

Related issues: #15432

Bug Locked 📦Bundler

Most helpful comment

Fixed it with @nishiltamboli and @nadbm comments from #15432

I had a similar problem with node version. What I did was

Open console and type the command > which node
Copied the node path (in my case it came out to be /Users//.nvm/versions/node/v10.14.1/bin/node)
Open Xcode project
Open Project > Build Phases > Bundle React Native code and images
Change
export NODE_BINARY=node
../node_modules/react-native/scripts/react-native-xcode.sh

to

export NODE_BINARY=
../node_modules/react-native/scripts/react-native-xcode.sh

So for me, I changed it to

export NODE_BINARY=/Users//.nvm/versions/node/v10.14.1/bin/node
../node_modules/react-native/scripts/react-native-xcode.sh

Thats it! Hope it helps someone.

All 8 comments

Fixed it with @nishiltamboli and @nadbm comments from #15432

I had a similar problem with node version. What I did was

Open console and type the command > which node
Copied the node path (in my case it came out to be /Users//.nvm/versions/node/v10.14.1/bin/node)
Open Xcode project
Open Project > Build Phases > Bundle React Native code and images
Change
export NODE_BINARY=node
../node_modules/react-native/scripts/react-native-xcode.sh

to

export NODE_BINARY=
../node_modules/react-native/scripts/react-native-xcode.sh

So for me, I changed it to

export NODE_BINARY=/Users//.nvm/versions/node/v10.14.1/bin/node
../node_modules/react-native/scripts/react-native-xcode.sh

Thats it! Hope it helps someone.

In my case, the metro bundler's project path that was running in background is not belonged to the project that I was trying to build.

In my case, the metro bundler's project path that was running in background is not belonged to the project that I was trying to build.

That's the true issue, when switching project… Good catch

Other case. I had the next line of code:

__DEV__ = false

(don't judge me) which doesn't break anything during the development, but breaks the build during release with original error message in the topic.
Any developer can have similar issue which breaks babel step, but you will find it as soon as you'll run bundle command

react-native bundle --minify --entry-file index.js --platform ios --dev false --bundle-output main.jsbundle

In my case the response was pretty clear:
error path/to/the/file.js: Property left of AssignmentExpression expected node to be of a type ["LVal"] but instead got "BooleanLiteral". Run CLI with --verbose flag for more details. TypeError: Property left of AssignmentExpression expected node to be of a type ["LVal"] but instead got "BooleanLiteral"

@2j2e

react-native bundle --minify --entry-file index.js --platform ios --dev false --bundle-output main.jsbundle

I love you, you save my life. <3

In my case, this error caused because of monorepo setup and React Native Cli project root.

react-native-xcode.sh refers $REACT_NATIVE_DIR/cli.js, and the script looks at monorepo root instead of React Native project root under the packages dir.

.
|-- package.json
|-- packages
|   `-- app
|       |- App.tsx                     # react native app
|       `- ios                         # native modules
|-- node_modules
|   `-- react-native
|       `- scripts
|          `- react-native-xcode.sh    # which will be invoked by Xcode
`-- cli.js                             # My custom cli

My Custom cli.js is looks like this to build the monorepo package. (ref: https://github.com/facebook/react-native/issues/25822#issuecomment-531009417)

process.chdir('./packages/app')

var cli = require('@react-native-community/cli')
cli.run()

Thus, I've changed Bundle React Native code and images in build phases so that react-native-xcode.sh invoke my custom react-native cli:

export NODE_BINARY=node
export CLI_PATH=./cli.js           // <-- Add this line
../../../node_modules/react-native/scripts/react-native-xcode.sh

Hope this helps someone like me.

Don't forget close all metro bundler's (include current app develop bundler) before start build. It solved problem for me.

this article saved my day
https://bit.ly/3cQIIyX

Was this page helpful?
0 / 5 - 0 ratings