Search code examples
angularangular-cli

Angular CLI looking for router module in wrong place? "router_module not found"


I have an issue:

Based on the trace, my code is looking for the router module in the wrong place, it should be looking in my node modules\@angular folder but its looking in my src\app\ folder, how can i fix this?

How I got here

I've installed angular 2 using angular cli, when I go to use the router module to create my routes, I import it like so:

import { RouterModule } '@angular/router/src/router_module';

and then use it passing in my routes like so:

export const routing = RouterModule.forRoot(APP_ROUTES, {enableTracing: true});

now when it goes to compile i get the following error:

Failed to compile.

./src/app/app.routing.ts Module not found: Error: Can't resolve

'@angular/router/src/router_module' in 'C:\path\to\src\app' <== important part ?

@ ./src/app/app.routing.ts 3:0-84

@ ./src/app/app.module.ts

@ ./src/main.ts

@ multi webpack-dev-server/client?http://localhost:4200 ./src/main.ts

I've checked that node module/@angular/router/src/router_module exists, and it does... I'm really stumped

angluar cli v1.0.1

angular v 4.4.6


Solution

  • Use this as your import:

    import { RouterModule } from '@angular/router';
    

    Get rid of the /src/router_module on the end of the path, and don't forgot the from.

    I don't know what IDE you are using, but I use VS Code. Sometimes, when I use the Ctrl+. (control period) to get the IDE to add a reference for me, I get some extra stuff in the path like what you had.

    Something to watch out for!