flatiron / director

a tiny and isomorphic URL router for JavaScript
http://github.com/flatiron/director
MIT License
5.6k stars 486 forks source link

Cannot initialize router when used via CommonJS #332

Open timwis opened 8 years ago

timwis commented 8 years ago

Hey folks, I've been trying to debug this for the past hour but have decided it may not be just me.

const Router = require('director').Router

const router = new Router({
  '/': () => console.log('root'),
  '/foo': () => console.log('foo')
})
router.init()

Yields the error router.init is not a function on index.js:19.

The documentation isn't quite clear on whether init() is necessary, so I tried without that line and the error goes away but the router doesn't appear to have any effect (nothing is logged). I also tried without new, but it tells me this.configure is not a function.

I'm trying to use this within an electron app, which has native support for require(), though I can't imagine it's any different than using a bundler like browserify/webpack. Any advice on how to use this lib?

PaulMaly commented 8 years ago

Join the question.

teawithfruit commented 8 years ago

I do it this way, and it works.

import {Router} from 'director'

class Routing {
  constructor() {
    let router = Router({
      '/': this.index,
      '/route/': this.route
    });
    router.init();
  }

  index() {
    console.log('index')
  }

  route() {
    console.log('route')
  }
}
rnons commented 7 years ago

Try import { Router } from 'director/build/director';

Look into package.json, you can see "main": "./lib/director", there is actually no init method in lib/director.

bibiuc commented 7 years ago

+1.查看代码后才知道是这么玩

timwis commented 7 years ago

I just ended up using sheet-router on the client and server-router on the server. Quite simple.

cweekly commented 5 years ago

import { Router } from 'director';
// -> "TypeError: (intermediate value).configure(...).init is not a function"

import { Router } from 'director/build/director';
// No problems.