Error building Angular CLI

2019-07-23 06:54发布

问题:

I am having an issue when trying to build my angular 2 project. When I call ng build, the builder is validating a route path as a module, and says it doesnt exist:

ERROR in Could not resolve "app/membros/membro.module" from "C:/Users/bruno.martins/git/disciples-ui/src/app/app.module.ts".

C:\Users\bruno.martins\git\disciples-ui>npm install

> disciples-ui@0.0.0 postinstall C:\Users\bruno.martins\git\disciples-ui
> ng build --prod

As a forewarning, we are moving the CLI npm package to "@angular/cli" with the next release,
which will only support Node 6.9 and greater. This package will be officially deprecated
shortly after.

To disable this warning use "ng set --global warnings.packageDeprecation=false".

fallbackLoader option has been deprecated - replace with "fallback"
loader option has been deprecated - replace with "use"
fallbackLoader option has been deprecated - replace with "fallback"
loader option has been deprecated - replace with "use"
fallbackLoader option has been deprecated - replace with "fallback"
loader option has been deprecated - replace with "use"
fallbackLoader option has been deprecated - replace with "fallback"
loader option has been deprecated - replace with "use"
Hash: d8f6b3f0c56af167dadf
Time: 16957ms
chunk    {0} polyfills.97e9eeda4d76d39a40b8.bundle.js (polyfills) 239 kB {4} [initial] [rendered]
chunk    {1} main.c2b4bf4bdc30f5037cb6.bundle.js (main) 13.6 kB {3} [initial] [rendered]
chunk    {2} styles.d41d8cd98f00b204e980.bundle.css (styles) 69 bytes {4} [initial] [rendered]
chunk    {3} vendor.45864682c5b5d47e6aa5.bundle.js (vendor) 2.35 MB [initial] [rendered]
chunk    {4} inline.30690479fd56d04d5cd5.bundle.js (inline) 0 bytes [entry] [rendered]

ERROR in Could not resolve "app/membros/membro.module" from "C:/Users/bruno.martins/git/disciples-ui/src/app/app.module.ts".

The problem is here:

RouterModule.forRoot([
            {
                path: 'membro',
                loadChildren: 'app/membros/membro.module#MembroModule'
            },
            {
                path : 'testador',
                loadChildren: 'app/testador/testador.module#TestadorModule'
            }
        ])

If I remove this 'loadChildren', the build runs fine! Actually, this is correct, because the application start with no problem and works. I am deploying this application at heroku, so I must use the ng build at Heroku (This command gives error locally too); locally, I use just npm start (The application runs fine using this).

Here is my package.json too:

{
  "name": "disciples-ui",
  "version": "0.0.0",
  "license": "",
  "angular-cli": {},
  "scripts": {
    "build": "tsc -p src/",
    "build:watch": "tsc -p src/ -w",
    "build:e2e": "tsc -p e2e/",
    "serve": "lite-server -c=bs-config.json",
    "serve:e2e": "lite-server -c=bs-config.e2e.json",
    "prestart": "npm run build",
    "start": "concurrently \"npm run build:watch\" \"npm run serve\"",
    "pree2e": "npm run build:e2e",
    "e2e": "concurrently \"npm run serve:e2e\" \"npm run protractor\" --kill-others --success first",
    "preprotractor": "webdriver-manager update",
    "protractor": "protractor protractor.config.js",
    "pretest": "npm run build",
    "test": "concurrently \"npm run build:watch\" \"karma start karma.conf.js\"",
    "pretest:once": "npm run build",
    "test:once": "karma start karma.conf.js --single-run",
    "lint": "tslint ./src/**/*.ts -t verbose",
    "ng": "ng",
    "postinstall": "ng build --prod",
    "start-heroku": "node server.js"
  },
  "private": true,
  "dependencies": {
    "@angular/common": "~4.0.0",
    "@angular/compiler": "~4.0.0",
    "@angular/core": "~4.0.0",
    "@angular/forms": "~4.0.0",
    "@angular/http": "~4.0.0",
    "@angular/platform-browser": "~4.0.0",
    "@angular/platform-browser-dynamic": "~4.0.0",
    "@angular/router": "~4.0.0",
    "angular-in-memory-web-api": "~0.3.0",
    "systemjs": "0.19.40",
    "core-js": "^2.4.1",
    "rxjs": "5.0.1",
    "zone.js": "^0.8.4",
    "ng": "0.0.0-rc6",
    "angular-cli": "1.0.0-beta.28.3",
    "@angular/compiler-cli": "~4.0.0"
  },
  "devDependencies": {
    "@types/jasmine": "2.5.36",
    "@types/node": "^6.0.46",
    "canonical-path": "0.0.2",
    "concurrently": "^3.2.0",
    "core-js": "^2.4.1",
    "express": "^4.15.2",
    "jasmine-core": "~2.4.1",
    "karma": "^1.3.0",
    "karma-chrome-launcher": "^2.0.0",
    "karma-cli": "^1.0.1",
    "karma-jasmine": "^1.0.2",
    "karma-jasmine-html-reporter": "^0.2.2",
    "lite-server": "^2.2.2",
    "lodash": "^4.16.4",
    "protractor": "~4.0.14",
    "rimraf": "^2.5.4",
    "ts-helpers": "^1.1.1",
    "tslint": "^3.15.1",
    "typescript": "~2.2.1"
  }
}

Could you help me guys?

回答1:

There are two possible solutions for your problem:

a) Use correct relative paths

This is described here: https://stackoverflow.com/a/48028980/5816097

Use ./membros/membro.module#MembroModule instead of app/membros/membro.module#MembroModule.

b) Provide baseUrl in your compilerOptions

In your src/tsconfig.app.json add baseUrl: "./" under compilerOptions:

{
  ...,
  "compilerOptions": {
    ...,
    baseUrl: "./"
  }
}