Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Router is not a constructor #127

Closed
bezenson opened this issue Sep 27, 2017 · 4 comments
Closed

Router is not a constructor #127

bezenson opened this issue Sep 27, 2017 · 4 comments

Comments

@bezenson
Copy link

Hi! I am using universal router with your react-starter-kit. I've upgraded there universal router to latest version (4.2.0).

import Router from 'universal-router';
import routes from './routes';

console.log(Router);

export default new Router(routes, {
  resolveRoute(context, params) {
    if (typeof context.route.load === 'function') {
      return context.route
        .load()
        .then(action => action.default(context, params));
    }
    if (typeof context.route.action === 'function') {
      return context.route.action(context, params);
    }
    return null;
  },
});

I am getting error that Router is not a constructor. console.log result is /assets/node_modules/universal-router/browser.mjs?05fd4504. It's just a string. Where is the magic? What I am doing wrong?

@frenzzy
Copy link
Member

frenzzy commented Sep 27, 2017

From changelog:

  • If you are using webpack, change rule (loader) for .js files to include .mjs extension, i.e.
    • test: /\.js$/ => test: /\.m?js$/

Also see kriasoft/react-starter-kit#1403 and kriasoft/react-starter-kit#1233

@javiermatos
Copy link

Hi, I am having this issue too when adding universal-router 4 and 5 to my project. I am using create-react-app. It works with version 3. According to create-react-app code, they added mjs extension in rule loader.

To reproduce this issue just create an empty project with create-react-app and change src/index.js to include the following code, that is similar to the one shown by universal-router here:

import UniversalRouter from 'universal-router';

const routes = [
    {
        path: '/',
        action: () => (<h1>Home</h1>),
    },
    {
        path: '/test',
        action: () => (<h1>Test</h1>),
    },
];

const router = new UniversalRouter(routes);


router.resolve('/test').then(html => {
    document.body.innerHTML = html;
});

How can I do to have it working? For version 5 in my project I was able to import from universal-router/browser.js and it worked, but generateUrls function from universal-router/generateUrls/browser.js failed. I tried with node 6 and 8.

@frenzzy
Copy link
Member

frenzzy commented Jan 1, 2018

@javiermatos it because facebook/create-react-app#3537 is not yet released, you can use version from master or wait a little bit.

@javiermatos
Copy link

Thank you @frenzzy, it worked.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants