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

You may need an appropriate loader for dynamic imports after upgrade to Angular 8

Open BrsJsk opened this issue 7 years ago β€’ 35 comments

🐞 bug report

Description

I've updated my Angular 7 app to Angular 8. It used the old Angular way dynamic imports. i've ran ng update @angular/cli @angular/core, everything was updated properly and it was using the new standard for dynamic imports, but app cannot now be opened. It breaks during the compile process. This might be webpack issue?

πŸ”¬ Minimal Reproduction

Updated to Angular 8 and this error is throwing.

πŸ”₯ Exception or Error


ERROR in ./src/app/app-routing.module.ts 8:28
Module parse failed: Unexpected token (8:28)
You may need an appropriate loader to handle this file type.
|     {
|         path: 'auth',
>         loadChildren: () => import('./auth/auth.module').then(m => m.AuthModule)
|     },
|     {

🌍 Your Environment

Angular Version:


Angular CLI: 8.0.0
Node: 10.13.0
OS: darwin x64
Angular: 8.0.0

@angular-devkit/architect          0.800.0
@angular-devkit/build-angular      0.800.0
@angular-devkit/build-optimizer    0.800.0
@angular-devkit/build-webpack      0.800.0
@angular-devkit/core               8.0.0
@angular-devkit/schematics         8.0.0
@angular/cdk                       7.3.1
@angular/material                  7.3.1
@angular/material-moment-adapter   7.3.1
@ngtools/webpack                   8.0.0
@schematics/angular                8.0.0
@schematics/update                 0.800.0
rxjs                               6.5.2
typescript                         3.4.5
webpack                            4.30.0

BrsJsk avatar May 29 '19 16:05 BrsJsk

This issue rings some bells, but I cannot find the original issue in webpack.

I believe this was encountered due to an npm bug and as a workaround users had to

rm -rf node_modules
rm -f package-lock.json
npm install

Can you try to give it a shot and see if this works for you?

alan-agius4 avatar May 29 '19 16:05 alan-agius4

Found it: https://github.com/webpack/webpack/issues/8656

alan-agius4 avatar May 29 '19 16:05 alan-agius4

Tried it. Didn't work. Ran

yarn

instead..and everything after that went fine.

BrsJsk avatar May 29 '19 19:05 BrsJsk

Removing the package-lock.json was the key for me. @alan-agius4 your example has the wrong name for package-lock.json but your answer worked for me. Should be

rm -rf node_modules
rm -f package-lock.json
npm install

jpduckwo avatar May 30 '19 00:05 jpduckwo

Didn't even notice the wrong name.. πŸ—‘

BrsJsk avatar May 30 '19 07:05 BrsJsk

I tried those 3 commands but with no luck, still get the same error. Any ideas? I don't use "yarn" on this project so I cannot try that one :/

Dunos avatar May 30 '19 15:05 Dunos

I upgraded from ang7 to ang8 both manually and with ng update. Lazy routes are updated to use import. tsconfig has module set to "esnext" but am still getting "Module parse failed: Unexpected token (52:28) You may need an appropriate loader to handle this file type." loadChildren: () => import(...

Removing node_modules and package-json did not help.

Update: Using yarn fixed the problem (both dev and aot builds). What did yarn do that npm did not? Anyone know?

greglarson avatar May 30 '19 17:05 greglarson

@jpduckwo, glad that it worked out. I updated my answer to include the extension for the lock file so future users don’t get confused.

This bug is only present in npm hoisting when already having modules installed.

Npm issue: https://npm.community/t/packages-with-peerdependencies-are-incorrectly-hoisted/4794

alan-agius4 avatar May 31 '19 05:05 alan-agius4

Upgraded from 7 - 8 today. Had the same issue as this thread. Yarn to the rescue. Thank you!

bjornharvold avatar May 31 '19 11:05 bjornharvold

Same issue, nothing worked. Is there a fix for this that doesn't rely on switching to yarn?

TomaszCz avatar May 31 '19 13:05 TomaszCz

I just tried to upgrade one of my projects (7 > 8) today and I had the same problem using npm.

gabucito avatar May 31 '19 15:05 gabucito

after upgrading from 7-> 8, i now receive the following error within the cli. i'm using require to add the contents of a html page that im rendering within a component. this.htmlContent = require('./content/' + 'pp-en.html');

WARNING in ./src/app/components/modals/privacy-policy/content/pp-dk.html 2:0 Module parse failed: Unexpected token (2:0) You may need an appropriate loader to handle this file type.

i've tried multiple updates to dependencies and yarn install does resolve this.

gpsimmo avatar Jun 03 '19 10:06 gpsimmo

Update:

acorn fixed the issue in 6.2.0 via https://github.com/acornjs/acorn/pull/834 webpack updated to acorn to 6.2 in webpack 4.35.3

So as soon as you are on webpack >= 4.35.3, this error will go away.

Old post:

The issue is, npm builds a directory structure for webpack 3.30+ like this:

β”œβ”€β”¬ node_modules/
β”‚ └─── acorn/
β”‚ └─── acorn-dynamic-import/
β”‚ └──┬ webpack/
β”‚    └─┬ node_modules
β”‚      └─── acorn/

so webpack has it's own acorn. But uses acorn-dynamic-import with uses the global acorn. This breaks instance of checks.

The fix in npm is, to do

npm install [email protected]

You can verify if it worked by doing npm ls acorn and see, if it says deduped behind webpacks acorn.

image

sod avatar Jun 03 '19 11:06 sod

@sod thanks man, your fix solved my build problem. Greatly appreciated, thank you for taking the time to share!

rolandzwaga avatar Jun 03 '19 11:06 rolandzwaga

I tried everything mentioned here but only installing with yarn fixed the problem for me. Hoping that using npm will be an option soon since I don't normally use yarn.

Update: I just tried it again but with npm install [email protected] first as @jcox68 mentions below. I didn't see a dedupe message either, but it seems to build and run now.

DanWahlin avatar Jun 03 '19 21:06 DanWahlin

@sod fixed my issue, although it did not dedupe my acorn package.

I had to run npm install [email protected] and then run

rm -rf node_modules
rm -f package-lock.json
npm install

for npm to get it working correctly.

In addition to the above, I had to update my tsconfig.json -> compilerOptions -> module property to esnext. More details here

I was left with the acorn reference in my package.json but everything still worked after removing it.

jcox86 avatar Jun 03 '19 22:06 jcox86

after upgrading from 7-> 8, i now receive the following error within the cli. i'm using require to add the contents of a html page that im rendering within a component. this.htmlContent = require('./content/' + 'pp-en.html');

WARNING in ./src/app/components/modals/privacy-policy/content/pp-dk.html 2:0 Module parse failed: Unexpected token (2:0) You may need an appropriate loader to handle this file type.

i've tried multiple updates to dependencies and yarn install does resolve this.

@gpsimmo try below line

this.htmlContent = require('html-loader!./content/' + 'pp-en.html');

or this.htmlContent = require('raw-loader!./content/' + 'pp-en.html');

We need to use html-loader! prefix as per latest webpack.

lokeshdaiya avatar Jun 04 '19 13:06 lokeshdaiya

@jpduckwo's suggestion fixed my issue

ptrzyna avatar Jun 05 '19 18:06 ptrzyna

@alan-agius4 unfortunately i can't fix the problem

splincode avatar Jun 06 '19 12:06 splincode

To complete @lokeshdaiya, after upgrading to Angular 8 all html files imports not working anymore (I guess that due to esnext module that need now a module definition for each import statement ?)

A workaround to fix this is to install webpack html-loader.

Install

yarn add html-loader --dev

Replace

const content = require(`./content1.html`);

By

const content = require(`html-loader!./content1.html`);

Normally this will fix this kind of error : image

I hope that will help someone

alexis-landrieu-avanade avatar Jun 13 '19 11:06 alexis-landrieu-avanade