Getting the error "Unable find module with ID" with Aurelia Router
Asked Answered
S

1

7

I'm trying to configure routes in Aurelia using aurelia-router, but it throws an error that it can't find the moduleId(route to file) of my route.

I configure my routes in two files router.js and app.js, router file only contains an array with all the routes. app file is the main file of my project.

Project Structure:

Project structure

Router.js:

export default [
  { route: ['', '/', 'home'], name: 'home', title: 'Inicio', layoutView: 'components/common/layout/layout.html', moduleId: 'components/home/home' }
]

App.js

import routes from 'router'
import {RouterConfiguration, Router} from 'aurelia-router';

export class App {

  configureRouter(config, router){
    config.options.root = '/';
    config.title = 'La Tatuadora';
    this.router = router;
    config.map(routes);
  }
}

Error

enter image description here

Science answered 7/9, 2017 at 5:15 Comment(4)
What are the contents of home.html and home.js?Phoenix
I already found the solution, I'm using Webpack as bundler, webpack need a resolver to find modules, Aurelia provides PLATFORM.moduleName("moduleId") to resolve modules, it comes with the aurelia-pal package. In use it looks like this: moduleId: PLATFORM.moduleName("components/home/home").Science
I regularly miss this one too. It would be good to reply to your own question and mention PLATFORM.moduleName()Extravagant
@Science Ahh nice spot. Could you self answer this question to help others?Phoenix
S
15

Aurelia provides PLATFORM.moduleName("moduleId") to resolve modules, it comes with the aurelia-pal package.

In use it looks like this:

{ moduleId: PLATFORM.moduleName("components/home/home") }
Science answered 11/9, 2017 at 6:25 Comment(1)
Very useful answer, saved me a lot of headache. Thanks.Linnealinnean

© 2022 - 2024 — McMap. All rights reserved.