Failed building JavaScript bundle error when application starts
Description
When I first create a create-react-native-app project and scan qr on my phone with expo, It threw javascipt bundling error instead of starting.
Expected Behavior
I expect to see my lovely first working react-native application
Observed Behavior


11:21:59: Failed building JavaScript bundle
11:21:59: Error: Expected path `C:\Users\Naim\Documents\projects\nativedeneme\node_modules\metro-bundler\src\Resolver\polyfills\prelude_dev.js` to be relative to one of the project roots
11:21:59: at toLocalPath (C:\Users\Naim\Documents\projects\nativedeneme\node_modules\metro-bundler\src\node-haste\lib\toLocalPath.js:33:9)
11:21:59: at ModuleCache.createPolyfill (C:\Users\Naim\Documents\projects\nativedeneme\node_modules\metro-bundler\src\node-haste\ModuleCache.js:166:18)
11:21:59: at DependencyGraph.createPolyfill (C:\Users\Naim\Documents\projects\nativedeneme\node_modules\metro-bundler\src\node-haste\DependencyGraph.js:312:30)
11:21:59: at map.moduleName (C:\Users\Naim\Documents\projects\nativedeneme\node_modules\metro-bundler\src\Resolver\index.js:150:38)
11:21:59: at Array.map (native)
11:21:59: at Resolver.getModuleSystemDependencies (C:\Users\Naim\Documents\projects\nativedeneme\node_modules\metro-bundler\src\Resolver\index.js:150:5)
11:21:59: at _resolverPromise.then.resolver (C:\Users\Naim\Documents\projects\nativedeneme\node_modules\metro-bundler\src\Bundler\index.js:273:31)
11:21:59: at <anonymous>
11:21:59: at process._tickCallback (internal/process/next_tick.js:188:7)
Environment
npm ls react-native-scripts: [email protected]npm ls react-native: [email protected]npm ls expo: [email protected]node -v: v8.2.1npm -v: 4.6.1yarn --version: 'yarn' is not recognized as an internal or external commandwatchman version: Please specify a target and action
- Operating system: Windows 10 Pro 1703
- Phone/emulator/simulator & version: Galaxy J7 (2016) SM-J710FQ Android 7.0 (Expo 1.20.0) (Nexus_5X_API_25_2 Android 7.1.1 and Nexus_5X_API_23 Android 6.0 emulators in Android Studio gave same error)
Reproducible Demo
npm install -g create-react-native-app
create-react-native-app nativedeneme
cd nativedeneme
npm start
Scan QR code with Expo on the phone
node_modules\metro-bundler\src\node-haste\lib\toLocalPath.js: (this dude is unhappy I think)
'use strict';var _require =
require('path');const relative = _require.relative;
// FIXME: This function has the shortcoming of potentially returning identical
// paths for two files in different roots.
function toLocalPath(
roots,
absolutePath)
{
for (let i = 0; i < roots.length; i++) {
const localPath = relative(roots[i], absolutePath);
if (localPath[0] !== '.') {
return localPath;
}
}
throw new Error(
`Expected path \`${absolutePath}\` to be relative to one of the project roots`);
}
module.exports = toLocalPath;
Installing yarn and using it instead of npm for everything gave same error.
Hi! It looks like this bug in metro-bundler was recently resolved, and we should have support for it soon.
c.f. https://github.com/facebook/metro-bundler/pull/54
@dikaiosune I have seen that commit and tried changing toLocalPath.js file by myself last week. It doesn't quite work for me. Am I missing something? Should I eject first or do another else instead of manually changing lines on node_modules?
Still same error message after importing basename and adding
|| basename(absolutePath) == localPath
to the condition
maybe my problem is related about something else.
//throw new Error( //
Expected path \${absolutePath}` to be relative to one of the project roots`);
Ignoring these lines like this fixed my issue, I think they are not necessary for me right now 😄
edit: DO NOT USE THIS, JUST UPDATE YOUR EXPO APP AND CREATE CREATE-REACT-NATIVE-APP TO C://
i have same problemes how to fix it ?
I am also seeing the same issue. Any quick work around?
@CaReS0107 @jalil I solved it by disabling lines that throw error but I don't think this is the right solution. Maybe it helps.
node_modules\metro-bundler\src\node-haste\lib\toLocalPath.js
delete or turn these lines to comments
throw new Error(
Expected path \${absolutePath}` to be relative to one of the project roots`);
edit: I realized creating create-react-native-app project to the root folder of my computer solved the issue. I also updated expo app on my phone
cd / create-react-native-app deneme cd deneme yarn start
Getting same error, create-react-native-app to C:\Users\<myUsername> solved the issue.
But still, i want to use create-react-native-app on different folder :(
@bondansebastian try checking letters' cases in your project path, they should be proper.
In my situation I accidentally used 'cd projects' instead of 'cd Projects', it works for getting in there but it makes something crash :)
i only stop the bundle with ctrl+c (on windows) and again start the project, npm start and the app works fine without errors
in this case, you have to just install >
npm i metro-bundler
in app direcorty https://www.npmjs.com/package/metro-bundler
Have got 2 similar solutions
- delete the node_modules folder then run npm install or
- copy out your source code with the project structure intact into a new directory without the node_modules folder then run npm install
this should work
Hi everyone! It looks like there are a few different issues reported here, and since the original issue is closed, I'm going to ask that new comments on this issue be reported as new issues if they have a new problem or previous fixes aren't working. Thanks!