2
votes

I'm having trouble exporting modules from my project folder (node modules such as 'react', 'react-native' work just fine) to my App.js file. I have created a new project using create-react-native-app and tried the following:

  1. Added test_file.js to my project folder.
  2. Inside test_file I declared variable test and then exported it.
  3. I then imported it to my App.js and ran npm start.

Everything seems to be fine until I try running it through expo. JavaScript bundle building fails and in my phone it gives an error message: The development server returned response error code: 500.

There is also a message: "Unable to resolve module `test_file` from `C:\Users\User\Projects\Test\App.js`: Module `test_file` does not exist in the Haste module map\n\n This might be related to to https://github.com/facebook/react-native/issues/4968\n To resolve try the following:\n 1. Clear watchman watches: `watchman watch-del-all`.\n 2. Delete the `node_modules` folder: `rm -rf node_modules && npm install`.\n 3. Reset Metro Bundler cache: `rm -rf /Test/metro-bundler-cache-` or `npm start -- -- reset-cache`. 4. Remove haste chache: `rm -rf /Test/haste-map-react-native-packager-`.".

So far I have tried all four methods and looked through the provided link, but haven't found a solution yet. I also tried using require instead of import.

What could be causing this problem?

EDIT: Here is the test code:

test_file.js:

const test = 'Hello World.';

export default test;

App.js:

import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
import test from 'test_file';


export default class App extends React.Component {
  render() {
    return (
      <View style={styles.container}>
        <Text>{test}</Text>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    alignItems: 'center',
    justifyContent: 'center',
  },
});
1
Please add the code so it can be examined - user3210641

1 Answers

2
votes

We have to mention the path in the import statement.

If the file is in same Folder:

import test from './test_file';

If the file is in previous Folder or child Folder:

import test from '../test_file';