ellen
ellen

Reputation: 704

React cannot find exported renderRoutes function

I am working on my first meteor/react application and I am trying to use react-router. However, when I try to run the app, I can't seem to import the renderRoutes function.

I am trying to follow along with the router part here:

https://guide.meteor.com/react.html

The router lives in myapp/client/lib/router.js. Here is my router code:

import React from 'react';
import { Router, Route, Switch } from 'react-router';
import createBrowserHistory from 'history/createBrowserHistory';

import {Home} from '../home/home.js';
import {Login} from '../login/login.js';
import {Connect} from '../connect/connect.js';

const browserHistory = createBrowserHistory();

export const renderRoutes = () => (
  <Router history={browserHistory}>
    <Switch>
      <Route exact path="/" component={Home}/>
      <Route exact path="/login" component={Login}/>
      <Route exact path="/connect" component={Connect}/>
    </Switch>
  </Router>
);

I have a meteor.startup() function in myapp/server/main.js, this is all I have in there:

import { Meteor } from 'meteor/meteor';
import { render } from 'react-dom';
import { renderRoutes } from '../client/lib/router.js';

Meteor.startup(() => {
  render(renderRoutes(), document.getElementById('App'));
});

When I try to run meteor run, here's what I see:

Error: Cannot find module '../client/lib/router.js'

Why? Any help is appreciated!

Upvotes: 0

Views: 123

Answers (1)

Jankapunkt
Jankapunkt

Reputation: 8423

It is because your router is located in

myapp/client/lib/router.js

whereas your main file is located in

myapp/server/main.js

So unless you intend to implement server-side-rendering, you may move the second code to

myapp/client/main.js

This is due to Meteor's project-structure awareness. If a file is contained in a folder, named client or server it will only be available in this environment.

More to read in the guide: https://guide.meteor.com/structure.html#special-directories

Upvotes: 1

Related Questions