angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

Module not found: Error: Can't resolve file

Open hassanasad opened this issue 9 years ago • 48 comments

@angular/cli: 1.0.0-beta.32.3 [1.0.0-beta.28.3]
node: 7.5.0
os: darwin x64
@angular/common: 2.4.7
@angular/compiler: 2.4.7
@angular/core: 2.4.7
@angular/forms: 2.4.7
@angular/http: 2.4.7
@angular/platform-browser: 2.4.7
@angular/platform-browser-dynamic: 2.4.7
@angular/router: 3.4.7
@angular/cli: 1.0.0-beta.32.3
@angular/compiler-cli: 2.4.7

After the update to beta.32.3 i started getting these errors in SCSS files:

ERROR in ./src/app/shared/header/header.component.scss
Module not found: Error: Can't resolve './assets/images/icon-search.png' in '/Users/hassan/Code/app/shared/header'
 @ ./src/app/shared/header/header.component.scss 6:1898-1940
 @ ./src/app/shared/header/header.component.ts
 @ ./src/app/shared/shared.module.ts
 @ ./src/app/app.module.ts
 @ ./src/main.ts
 @ multi webpack-dev-server/client?http://localhost:4200/ ./src/main.ts

ERROR in ./src/app/shared/nav/nav.component.scss
Module not found: Error: Can't resolve './assets/fonts/portal.eot?' in '/Users/hassan/Code/src/app/shared/nav'
 @ ./src/app/shared/nav/nav.component.scss 6:295-332
 @ ./src/app/shared/nav/nav.component.ts
 @ ./src/app/shared/shared.module.ts
 @ ./src/app/app.module.ts
 @ ./src/main.ts
 @ multi webpack-dev-server/client?http://localhost:4200/ ./src/main.ts

The error seems to appear in this line:

background-image: url('assets/images/icon-search.png');

P.S: It was working fine on beta.31

hassanasad avatar Feb 17 '17 11:02 hassanasad

Same thing here. Technically, at development time, the path is wrong because the file has not yet been compiled into the location it will be at runtime. So I'm assuming I need to specify the location in the configuration somewhere?

billdwhite avatar Feb 17 '17 11:02 billdwhite

For reference my .angular-cli.json file has:

"assets": [
        "assets",
        "web.config"
      ],

(Which was working fine in beta.31 as mentioned earlier)

hassanasad avatar Feb 17 '17 12:02 hassanasad

I've got the same; still getting the error

billdwhite avatar Feb 17 '17 12:02 billdwhite

me too @font-face { font-family: 'Segoe UI'; src: url('../assets/fonts/SegoeUI.eot'); src: url('../assets/fonts/SegoeUI.eot?#iefix') format('embedded-opentype'), url('../assets/fonts/SegoeUI.woff') format('woff'), url('../assets/fonts/SegoeUI.ttf') format('truetype'); font-weight: normal; font-style: normal; }

ERROR in ./src/app/components/footer/footer.scss Module not found: Error: Can't resolve '../assets/fonts/SegoeUI.eot' in '/home/arman/Projects/AOP/src/app/components/footer' @ ./src/app/components/footer/footer.scss 6:83-121 6:144-182 @ ./src/app/components/footer/footer.ts @ ./src/app/index.ts @ ./src/main.ts @ multi webpack-dev-server/client?http://localhost:4200/ ./src/main.ts

armangevorgyan avatar Feb 17 '17 12:02 armangevorgyan

CSS URL handling was changed. To replicate the behavior prior to beta.32, use a root relative URL (i.e., add a / to the beginning of each URL).

clydin avatar Feb 17 '17 12:02 clydin

I tried that and it did compile with no errors, but then at runtime it was looking for 'assets' in the root of the url rather than in the root of the web context. So none of my images loaded.....

billdwhite avatar Feb 17 '17 12:02 billdwhite

Thanks @clydin - it started working again for me by adding '/' before all relative URLs in the SCSS files.

@billdwhite do you have the 'assets' in your .angular-cli.json file as mentioned above?

hassanasad avatar Feb 17 '17 12:02 hassanasad

Yes I do have that in my files. But I'm running the app from a subcontext (think http://myserver/myapp as opposed to http://myserver) so the path that results from using / before the relative urls results in it looking for http://myserver/assets/myimage.png which is wrong. (It needs to be http://myserver/my app/assets/myimage.png) so I'm guessing I'm missing some other config setting that tells it to adjust for that. Maybe base href or something but that seems to be set correctly for everything else in my app so I'm not sure what I'm missing. Again, setting the / does fix the compile errors but it just doesn't work at runtime.

billdwhite avatar Feb 17 '17 15:02 billdwhite

I have the exact same behaviour as @billdwhite. Adding a slash maybe fixes the compilation error but it's no final solution and wasn't necessary in the past.

kunee avatar Feb 17 '17 19:02 kunee

@filipesilva is it possible this was caused by your commit from 3 days back?

billdwhite avatar Feb 17 '17 20:02 billdwhite

Yes it is likely related. I'll have a look.

filipesilva avatar Feb 17 '17 20:02 filipesilva

url()'s in component stylesheets are now processed. This allows the CLI to hash the assets and supports long-term caching. However, the URL now needs to be build-time discoverable and the path must be relative to the component stylesheet for this to work. This also has the advantage that component based assets can be encapsulated within the component source location, if desired.

If you don't want them processed, a root relative URL will prevent this behavior. Unfortunately, this will currently conflict with custom base hrefs and deployUrls. This is in the process of being corrected.

Also of note is that global stylesheets already function in this manner.

clydin avatar Feb 17 '17 21:02 clydin

First of all, apologies for the breakage. @clydin's answer above explains what happened well. Further clarification: 'root relative URLs' are those that start with /.

As to why this change... it was because component css did not support imports, and in fixing that we inadvertently also fixed url() inside css.

Before it didn't actually work insofar as images/etc where never processed. Now they are (as long as it's a relative url).

This also means that you can just put your images locally, side by side with your css. They will be correctly recognized and placed in the output directory, and even get hashes on prod builds. You don't need to put them in assets/ if you don't want to.

If you do want to put them in assets/, you can use a root relative path, one starting with /.

For instance, from src/app/component.css you can use the relative path url('../assets/image.jpg') or the absolute path url('/assets/image.jpg'.

I just pushed a PR that should fix base-href and deploy-url broken interaction: https://github.com/angular/angular-cli/pull/4803.

filipesilva avatar Feb 17 '17 23:02 filipesilva

@filipesilva Thanks for your clarification. However, I'm still having problem with images not being copied in production builds, see #4806. I don't know if this was fixed in your fix?

arntj avatar Feb 18 '17 08:02 arntj

If url() is now processed, I believe @import should follow the same way.

In my less file I'm having to use two ways to reference a file.

@import "../../../assets/less/bootstrap/_variables.less"; background-image: url ('/assets/img/professional-02.jpg');

assets in the two cases above is the same folder, but I can not use /assets in @import.

romulo1984 avatar Mar 17 '17 03:03 romulo1984

@romulo1984 imports and urls follow different semantics.

filipesilva avatar Mar 18 '17 16:03 filipesilva

Thanks @clydin... :)

Save my time :)

skdhayal avatar Mar 20 '17 07:03 skdhayal

according to base tag specification

The base URL to be used throughout the document for relative URL addresses. If this attribute is specified, this element must come before any other elements with attributes whose values are URLs. Absolute and relative URLs are allowed.

in angular-cli projects the base href is "/" (index.html) so all paths, including urls in css, and including relative and absolute paths, should have / appended to them. Instead, relative paths have './'

The solution of manually adding '/' for absolute paths is not good enough, since different server environments on different builds might differ in the wanted 'base' url.

I assume it's a loader issue. Not sure how it plays with the rest of the configuration, but raw-loader follows this spec perfectly.

doronnac avatar Mar 22 '17 13:03 doronnac

"@angular/cli" :"1.0.0-rc.4", - throws 404 error for urls in '.html' files

GET http://localhost:4200/resources/files.json 404 (Not Found) scheduleTask @ zone.js:2019 ZoneDelegate.scheduleTask @ zone.js:353 onScheduleTask @ zone.js:241 ZoneDelegate.scheduleTask @ zone.js:347 Zone.scheduleTask @ zone.js:196 Zone.scheduleMacroTask @ zone.js:210 (anonymous) @ zone.js:2043 send @ VM8905:3 (anonymous) @ xhr_backend.js:126 Observable._trySubscribe @ Observable.js:57 Observable.subscribe @ Observable.js:45 (anonymous) @ toPromise.js:68 ZoneAwarePromise @ zone.js:729 toPromise @ toPromise.js:66 webpackJsonp.647.NodeService.getFiles @ app.treeservice.ts:13 webpackJsonp.432.AppComponent.ngOnInit @ app.component.ts:61 Wrapper_AppComponent.ngDoCheck @ /AppModule/AppComponent/wrapper.ngfactory.js:22 View_AppComponent_Host0.detectChangesInternal @ /AppModule/AppComponent/host.ngfactory.js:36 AppView.detectChanges @ view.js:425 DebugAppView.detectChanges @ view.js:620 ViewRef_.detectChanges @ view_ref.js:170 (anonymous) @ application_ref.js:625 ApplicationRef_.tick @ application_ref.js:625 ApplicationRef_._loadComponent @ application_ref.js:600 ApplicationRef_.bootstrap @ application_ref.js:588 (anonymous) @ application_ref.js:400 PlatformRef_._moduleDoBootstrap @ application_ref.js:400 (anonymous) @ application_ref.js:352 ZoneDelegate.invoke @ zone.js:334 onInvoke @ ng_zone.js:273 ZoneDelegate.invoke @ zone.js:333 Zone.run @ zone.js:126 (anonymous) @ zone.js:713 ZoneDelegate.invokeTask @ zone.js:367 onInvokeTask @ ng_zone.js:264 ZoneDelegate.invokeTask @ zone.js:366 Zone.runTask @ zone.js:166 drainMicroTaskQueue @ zone.js:546 error_handler.js:54 EXCEPTION: Uncaught (in promise): Response with status: 404 Not Found for URL: http://localhost:4200/resources/files.json ErrorHandler.handleError @ error_handler.js:54 next @ application_ref.js:348 schedulerFn @ async.js:93 SafeSubscriber.__tryOrUnsub @ Subscriber.js:234 SafeSubscriber.next @ Subscriber.js:183 Subscriber._next @ Subscriber.js:125 Subscriber.next @ Subscriber.js:89 Subject.next @ Subject.js:55 EventEmitter.emit @ async.js:79 NgZone.triggerError @ ng_zone.js:333 onHandleError @ ng_zone.js:294 ZoneDelegate.handleError @ zone.js:338 Zone.runGuarded @ zone.js:142 _loop_1 @ zone.js:557 drainMicroTaskQueue @ zone.js:566 ZoneTask.invoke @ zone.js:424 error_handler.js:59 ORIGINAL STACKTRACE: ErrorHandler.handleError @ error_handler.js:59 next @ application_ref.js:348 schedulerFn @ async.js:93 SafeSubscriber.__tryOrUnsub @ Subscriber.js:234 SafeSubscriber.next @ Subscriber.js:183 Subscriber._next @ Subscriber.js:125 Subscriber.next @ Subscriber.js:89 Subject.next @ Subject.js:55 EventEmitter.emit @ async.js:79 NgZone.triggerError @ ng_zone.js:333 onHandleError @ ng_zone.js:294 ZoneDelegate.handleError @ zone.js:338 Zone.runGuarded @ zone.js:142 _loop_1 @ zone.js:557 drainMicroTaskQueue @ zone.js:566 ZoneTask.invoke @ zone.js:424 error_handler.js:60 Error: Uncaught (in promise): Response with status: 404 Not Found for URL: http://localhost:4200/resources/files.json at resolvePromise (zone.js:665) [angular] at resolvePromise (zone.js:636) [angular] at :4200/vendor.bundle.js:109704:17 [angular] at Object.onInvokeTask (ng_zone.js:264) [angular] at ZoneDelegate.invokeTask (zone.js:366) [angular] at Zone.runTask (zone.js:166) [<root> => angular] at drainMicroTaskQueue (zone.js:546) [<root>] at XMLHttpRequest.ZoneTask.invoke (zone.js:424) [<root>] ErrorHandler.handleError @ error_handler.js:60 next @ application_ref.js:348 schedulerFn @ async.js:93 SafeSubscriber.__tryOrUnsub @ Subscriber.js:234 SafeSubscriber.next @ Subscriber.js:183 Subscriber._next @ Subscriber.js:125 Subscriber.next @ Subscriber.js:89 Subject.next @ Subject.js:55 EventEmitter.emit @ async.js:79 NgZone.triggerError @ ng_zone.js:333 onHandleError @ ng_zone.js:294 ZoneDelegate.handleError @ zone.js:338 Zone.runGuarded @ zone.js:142 _loop_1 @ zone.js:557 drainMicroTaskQueue @ zone.js:566 ZoneTask.invoke @ zone.js:424 zone.js:522 Unhandled Promise rejection: Response {_body: "<!DOCTYPE html>↵<html lang="en">↵<head>↵<meta char…e>Cannot GET /resources/files.json</pre>↵</body>↵", status: 404, ok: false, statusText: "Not Found", headers: Headers…} ; Zone: angular ; Task: Promise.then ; Value: Response {_body: "<!DOCTYPE html>↵<html lang="en">↵<head>↵<meta char…e>Cannot GET /resources/files.json</pre>↵</body>↵", status: 404, ok: false, statusText: "Not Found", headers: Headers…} undefined consoleError @ zone.js:522 handleUnhandledRejection @ zone.js:527 _loop_1 @ zone.js:562 drainMicroTaskQueue @ zone.js:566 ZoneTask.invoke @ zone.js:424 zone.js:524 ZoneAwareError {__zone_symbol__error: Error: Uncaught (in promise): Response with status: 404 Not Found for URL: http://localhost:4200/res…, rejection: Response, promise: ZoneAwarePromise, zone: Zone, task: ZoneTask…} consoleError @ zone.js:524 handleUnhandledRejection @ zone.js:527 _loop_1 @ zone.js:562 drainMicroTaskQueue @ zone.js:566 ZoneTask.invoke @ zone.js:424 Mal.png:1 GET http://localhost:4200/assets/Mal.png 404 (Not Found) Molly.png:1 GET http://localhost:4200/assets/Molly.png 404 (Not Found) Sophie.png:1 GET http://localhost:4200/assets/Sophie.png 404 (Not Found) Taz.png:1 GET http://localhost:4200/assets/Taz.png 404 (Not Found) Porter.png:1 GET http://localhost:4200/assets/Porter.png 404 (Not Found) Stephen.png:1 GET http://localhost:4200/assets/Stephen.png 404 (Not Found) Kobe.png:1 GET http://localhost:4200/assets/Kobe.png 404 (Not Found) Cinny.png:1 GET http://localhost:4200/assets/Cinny.png 404 (Not Found) Hermes.png:1 GET http://localhost:4200/assets/Hermes.png 404 (Not Found)

moorthi07 avatar Mar 23 '17 23:03 moorthi07

I fixed it by moving 'resources' folder inside 'assets' - folder as its manageable workaround for me. But , I actually need to access a folder from the root. In angular-cli.json if I set 'resources' folder inside 'assets' : array, its still not working!

"assets": [ "assets", //-------- Folder "resources" //-------Folder ], Whats wrong here, that angular-cli to throw the above errror?

moorthi07 avatar Mar 27 '17 07:03 moorthi07