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

Hi, I use cli version 1.0.0. I'm deploying to non-root path within a domain and my build script executes the following command:

ng build \
--target=production --output-path=dist/de \
--deploy-url=/site/v2/de/ \
--base-href=/site/v2/de/ \
--i18n-file=src/i18n/messages.de.xlf --locale=de

One of my components has the scss style:

background-image: url('/assets/images/logo-de.svg');

After the build the style is transformed to:

background-image:url(/site/v2/de/site/v2/de/assets/images/logo-de.svg

It seems like it concatenates deploy-url and base-href which is a wrong behavior I guess. However everything is working while serving it in development mode with ng serve.

PS. If I place the images within the component folder and use relative url like this background-image: url('./images/logo-de.svg') everything works perfect.

Update. Looks like the logic is here https://github.com/angular/angular-cli/blob/master/packages/%40angular/cli/models/webpack-configs/styles.ts#L66 but still can't understand why it's not possible to have both deploy-url and base-href without scheme.

sqrter avatar Mar 28 '17 21:03 sqrter

It was an empty background-image: url('') in my case.

jonathanargentiero avatar Apr 20 '17 13:04 jonathanargentiero

when is set url like this , it has error: background-image: url('assets/img/1.jpg');

if you used angular2-webpack-starter you can try like this, it works for me ! background-image: url('~assets/img/1.jpg');

gaojinbo010 avatar Apr 24 '17 07:04 gaojinbo010

Hi, one solution for me is, put some styles like this in:

styles.css /* You can add global styles to this file, and also import other style files */

spark-studio avatar May 20 '17 20:05 spark-studio

I am facing a similar problem with @angular/cli version 1.0.0. I am using the path in sass as background-image: url('/assets/images/account.png') And this works fine with ng serve and I can see images. ng build also does not give any error. But after I build it, the main bundle has wrong path. It has

background-image: url('/./assets/images/account.png')

Note that I am using "ng build --env prod --base-href ." to build the project.

Does anyone know which version has the fix for this?

nputhiyadath avatar May 31 '17 20:05 nputhiyadath

Solved that by disabling of url(...) processing in css-loader plugin (I using external webpack config from angular2-webpack-starter). Described here https://github.com/webpack-contrib/extract-text-webpack-plugin/issues/246#issuecomment-308438193

bobrosoft avatar Jun 14 '17 14:06 bobrosoft

@billdwhite hey did you ever end up solving your issue? I'm facing the same scenario.

aliyeysides avatar Sep 20 '17 20:09 aliyeysides

@aliyeysides that was so many months back that I'm not sure how I solved it. Regardless, I have been using ~ in my asset paths to like this:

@import '~assets/styles/variables.scss';

so maybe that will work for you to specify an assets folder in the root of the project that is running from a non-server root endpoint.

billdwhite avatar Sep 20 '17 21:09 billdwhite

[](ERROR in multi script-loader!./~/jquery/dist/jquery.slim.js script-loader!./~/popper.js/dist/umd/popper.min.js script-loader!./~/bootstrap/dist/js/bootstrap.min.js Module not found: Error: Can't resolve 'script-loader' in 'C:\rahul\angular\test\abc' @ multi script-loader!./~/jquery/dist/jquery.slim.js script-loader!./~/popper.js/dist/umd/popper.min.js script-loader!./~/bootstrap/dist/js/bootstrap.min.js

ERROR in multi script-loader!./~/jquery/dist/jquery.slim.js script-loader!./~/popper.js/dist/umd/popper.min.js script-loader!./~/bootstrap/dist/js/bootstrap.min.js Module not found: Error: Can't resolve 'script-loader' in 'C:\rahul\angular\test\abc' @ multi script-loader!./~/jquery/dist/jquery.slim.js script-loader!./~/popper.js/dist/umd/popper.min.js script-loader!./~/bootstrap/dist/js/bootstrap.min.js

ERROR in multi script-loader!./~/jquery/dist/jquery.slim.js script-loader!./~/popper.js/dist/umd/popper.min.js script-loader!./~/bootstrap/dist/js/bootstrap.min.js Module not found: Error: Can't resolve 'script-loader' in 'C:\rahul\angular\test\abc' @ multi script-loader!./~/jquery/dist/jquery.slim.js script-loader!./~/popper.js/dist/umd/popper.min.js script-loader!./~/bootstrap/dist/js/bootstrap.min.js

ERROR in multi ./~/bootstrap/dist/css/bootstrap.min.css ./src/styles.css Module not found: Error: Can't resolve 'postcss-loader' in 'C:\rahul\angular\test\abc' @ multi ./~/bootstrap/dist/css/bootstrap.min.css ./src/styles.css

ERROR in multi ./~/bootstrap/dist/css/bootstrap.min.css ./src/styles.css Module not found: Error: Can't resolve 'postcss-loader' in 'C:\rahul\angular\test\abc' @ multi ./~/bootstrap/dist/css/bootstrap.min.css ./src/styles.css

ERROR in multi ./src/polyfills.ts Module not found: Error: Can't resolve '@ngtools/webpack' in 'C:\rahul\angular\test\abc' @ multi ./src/polyfills.ts

ERROR in multi webpack-dev-server/client?http://localhost:4200 ./src/main.ts Module not found: Error: Can't resolve '@ngtools/webpack' in 'C:\rahul\angular\test\abc' @ multi webpack-dev-server/client?http://localhost:4200 ./src/main.ts

ERROR in Error: Child compilation failed: Entry module not found: Error: Can't resolve 'raw-loader' in 'C:\rahul\angular \test\abc': Error: Can't resolve 'raw-loader' in 'C:\rahul\angular\test\abc'

  • compiler.js:76 [abc]/[cli]/[html-webpack-plugin]/lib/compiler.js:76:16

  • Compiler.js:280 Compiler. [abc]/[cli]/[webpack]/lib/Compiler.js:280:10

  • Compiler.js:480 [abc]/[cli]/[webpack]/lib/Compiler.js:480:13

  • Tapable.js:202 next [abc]/[cli]/[tapable]/lib/Tapable.js:202:11

  • CachePlugin.js:62 Compiler. [abc]/[cli]/[webpack]/lib/CachePlugin.js:62:5

  • Tapable.js:206 Compiler.applyPluginsAsyncSeries [abc]/[cli]/[tapable]/lib/Tapable.js:206:13

  • Compiler.js:477 [abc]/[cli]/[webpack]/lib/Compiler.js:477:10

  • Tapable.js:195 Compilation.applyPluginsAsyncSeries [abc]/[cli]/[tapable]/lib/Tapable.js:195:46

  • Compilation.js:640 self.applyPluginsAsync.err [abc]/[cli]/[webpack]/lib/Compilation.js:640:19

  • Tapable.js:195 Compilation.applyPluginsAsyncSeries [abc]/[cli]/[tapable]/lib/Tapable.js:195:46

  • Compilation.js:631 self.applyPluginsAsync.err [abc]/[cli]/[webpack]/lib/Compilation.js:631:11

  • Tapable.js:195 Compilation.applyPluginsAsyncSeries [abc]/[cli]/[tapable]/lib/Tapable.js:195:46

  • Compilation.js:626 self.applyPluginsAsync.err [abc]/[cli]/[webpack]/lib/Compilation.js:626:10

  • Tapable.js:195 Compilation.applyPluginsAsyncSeries [abc]/[cli]/[tapable]/lib/Tapable.js:195:46

  • Compilation.js:622 sealPart2 [abc]/[cli]/[webpack]/lib/Compilation.js:622:9

  • Tapable.js:195 Compilation.applyPluginsAsyncSeries [abc]/[cli]/[tapable]/lib/Tapable.js:195:46

ERROR in multi webpack-dev-server/client?http://localhost:4200 ./src/main.ts Module not found: Error: Can't resolve 'webpack-dev-server/client?http://localhost:4200' in 'C:\rahul\angular\test\abc' @ multi webpack-dev-server/client?http://localhost:4200 ./src/main.ts ) Please help me on this

sunarc-rahul avatar Nov 13 '17 13:11 sunarc-rahul

Please help me ...

npm run dev @ dev /var/www/html/screp npm run development @ development /var/www/html/screp cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js 95% emitting

ERROR Failed to compile with 1 errors 00:33:59

This dependency was not found:

/var/www/html/screp/resources/assets/sass/app.scss in multi ./resources/assets/js/app.js ./resources/assets/sass/app.scss To install it, you can run: npm install --save /var/www/html/screp/resources/assets/sass/app.scss Asset Size Chunks Chunk Names /js/app.js 4.12 kB 0 [emitted] /js/app

ERROR in ./resources/assets/js/app.js Module build failed: ReferenceError: Unknown plugin "transform-object-rest-spread" specified in "base"t > 0, attempted to resolve relative to "/var/www/html/screp/resources/assets/js" at /var/www/html/screp/node_modules/babel-core/lib/transformation/file/options/option-manager.js:180:17

at Array.map () at Function.normalisePlugins (/var/www/html/screp/node_modules/babel-core/lib/transformation/file/options/option-manager.js:158:20) at OptionManager.mergeOptions (/var/www/html/screp/node_modules/babel-core/lib/transformation/file/options/option-manager.js:234:36) at OptionManager.init (/var/www/html/screp/node_modules/babel-core/lib/transformation/file/options/option-manager.js:368:12) at File.initOptions (/var/www/html/screp/node_modules/babel-core/lib/transformation/file/index.js:212:65) at new File (/var/www/html/screp/node_modules/babel-core/lib/transformation/file/index.js:135:24) at Pipeline.transform (/var/www/html/screp/node_modules/babel-core/lib/transformation/pipeline.js:46:16) at transpile (/var/www/html/screp/node_modules/babel-loader/lib/index.js:50:20) at /var/www/html/screp/node_modules/babel-loader/lib/fs-cache.js:118:18 at ReadFileContext.callback (/var/www/html/screp/node_modules/babel-loader/lib/fs-cache.js:31:21) at FSReqWrap.readFileAfterOpen [as oncomplete] (fs.js:437:13) @ multi ./resources/assets/js/app.js ./resources/assets/sass/app.scss ERROR in multi ./resources/assets/js/app.js ./resources/assets/sass/app.scss> Module not found: Error: Can't resolve 'css-loader' in '/var/www/html/screp' @ multi ./resources/assets/js/app.js ./resources/assets/sass/app.scss

ululajib avatar Dec 16 '17 17:12 ululajib

@filipesilva @romulo1984 Hi. In reference to @romulo1984 comment about

@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.

How would you add a root reference in @import?

For example: Convert: @import "../../../assets/less/bootstrap/_variables.less"; To point to a common folder: @import "common/less/bootstrap/_variables.less";

Because in my current implementation this currently creates an error in saying it cannot resolve the path location. Error:

Module build failed:
@import "common/variables";
      File to import not found or unreadable: common/variables.

I have defined the "common" path mapping in tsconfig.json:

"paths": {
        "common/*": [
            "app/common/styles*"
        ]
    }

and yes, "variables.scss" does exist in the "app/common/styles" folder. Also, using the "~" infront of it does not work either.

xtgrant avatar Jan 10 '18 21:01 xtgrant

background-image: url('/assets/images/icon-search.png'); works on mine, add / to assests... so /assets... and also don't do background-image: url(""); empty source with double"" or single'' quotes, it gave me error saying ERROR in ./src/styles.css Module build failed: ModuleNotFoundError: Module not found: Error: Can't resolve './.' in ......

mseltene avatar Feb 15 '18 15:02 mseltene

Is this broken again in 1.7.0?

dcatoday avatar Feb 16 '18 20:02 dcatoday

@dcatoday having the same problem as you in 1.7.0, I think it's broken again

rodrijuarez avatar Feb 20 '18 15:02 rodrijuarez

I still don't see a clean way, how to reference urls in sass with a relative path that works both in dev and prod (app not running in root directory).

If I reference my image and font urls with 'root relative urls' like url('/assets/some-font.ttf') everything works fine in dev but I can not run my app in a subfolder on the server.

If I use relative urls starting from my sass file like url('../fonts/some-font.ttf'), my dev environment works fine as well but all assets referenced in sass get packaged into the root of my app instead of the assets folder.

For reasons of our deployment process and prod environment I am not using base href, but HashLocationStrategy.

tkohr avatar Feb 21 '18 14:02 tkohr

Guys, Google Mobile Friendly Test failed (https://search.google.com/test/mobile-friendly), because it was not able to find the background-image. This is how I started to search for a solution. Actually starting with '/' didn't help me at all... but starting with './' was the solution. (Angular 6)

image

woteska avatar Jul 21 '18 17:07 woteska

I'm running Angular CLI version 6.2.4 and I still have the same issue reported before by @billdwhite:

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.

this happens only in production and only when the app runs in a subdomain.

FrancescoBorzi avatar Oct 05 '18 12:10 FrancescoBorzi

Just want to add another vote.. because paths in my scss is also not working for me. If I do not absolute it works when running with ng serve but not in prduction and the other way around when I go relative. This the line in one of my components scss files: background-image: url("assets/bar.png");

EDIT: actually I just found I had another bug in my scss, which was the real cause for it not working in production. I had:

background: cover #da4877;
background-image: url("/assets/bar.png");
background-size: cover;

after I changed the first line to background: #da4877; it works.

So please Ignore my vote on this ;)

matulkum avatar Oct 07 '18 20:10 matulkum

I'm running Angular CLI version 6.2.4 and I still have the same issue reported before by @billdwhite:

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.

this happens only in production and only when the app runs in a subdomain.

Hey, did you find any solution for this issue? I'm facing the same issue.

isahilpahuja avatar Nov 03 '18 15:11 isahilpahuja

@isahilpahuja , the workaround (not a proper solution) I've found is to move your image reference from the styles to the template. It feels icky but it "works."

So instead of:

// article.scss
.article__photo {
  background-image: url(assets/bacteria.svg);
}

do this instead:

<!-- article.html -->
<div class="article__photo" style="background-image: url(assets/bacteria.svg);"></div>

Though this workaround may not be reasonable if you're wanting to apply the background image to many elements.

Another possible workaround is to eject the webpack config and fiddle with the CSS file-loading, though I'm no webpack expert but I assume it could likely be done... Of course, at the expense of the ejected webpack config which I personally find intimidating...

Empereol avatar Nov 07 '18 21:11 Empereol