Skip to content

ng serve / ng test - import resolving difference ? #3533

Closed

Description

@vfedoriv

Please provide us with the following information:

OS?

Windows 10

Versions.

angular-cli: 1.0.0-beta.22-1
node: 7.2.0
os: win32 x64

Repro steps.

This import work with ng serve and not work with ng test:

import {environment} from 'environments/environment';

@Injectable()
export class OwnerService {

  private entity_url = environment.REST_API_URL + 'owners';

When start ng test :

ERROR in ./src/app/owners/owner.service.ts
Module not found: Error: Can't resolve 'environments/environment' in 'D:\eclipse\workspace\spring-petclinic-angular2\src\app\owners'
 @ ./src/app/owners/owner.service.ts 15:20-55
 @ ./src/app/app.component.ts
 @ ./src/app/app.component.spec.ts
 @ ./src \.spec\.ts
 @ ./src/test.ts

When I change import as

import {environment} from './../../environments/environment'

work ok for both (but it's lo-o-ong :) )

File environment.ts in src/environments/ directory.

Webpack config difference or something relate to ##2470 ?

Activity

  1. Juansasa commented on Dec 14, 2016

    @Juansasa

    I'm having the same problem, hopes it will be addressed soon

  2. Igonato commented on Dec 14, 2016

    @Igonato

    Same Issue. Brakes app/* imports in tests

  3. hansl commented on Dec 15, 2016

    @hansl
    Contributor

    Do you have a baseUrl key in your tsconfig.json?

  4. vfedoriv commented on Dec 15, 2016

    @vfedoriv
    Author

    tsconfig as default generated, "baseUrl": ""

  5. hansl commented on Dec 15, 2016

    @hansl
    Contributor

    Could you provide a project that we can test against? I haven't seen that in beta 22-1 and/or Windows.

  6. added
    needs: repro stepsWe cannot reproduce the issue with the information given
    on Dec 15, 2016
  7. Igonato commented on Dec 16, 2016

    @Igonato

    @hansl I was on beta 22 updated to beta 22-1 and it went away

  8. vfedoriv commented on Dec 16, 2016

    @vfedoriv
    Author
  9. hansl commented on Dec 16, 2016

    @hansl
    Contributor

    Thanks. We'll investigate and come back.

  10. hansl commented on Dec 16, 2016

    @hansl
    Contributor

    I found the problem with your

    Using a local fix that I have to report errors more efficiently, this is what I see:

    # ng serve --aot
    ** NG Live Development Server is running on http://localhost:4200. **
    Hash: 1951a494f4f6edf25474
    Time: 5870ms
    chunk    {0} main.bundle.js, main.bundle.map (main) 59.9 kB {2} [initial] [rendered]
    chunk    {1} styles.bundle.css, styles.bundle.map, styles.bundle.map (styles) 69 bytes {3} [initial] [rendered]
    chunk    {2} vendor.bundle.js, vendor.bundle.map (vendor) 1.22 MB [initial] [rendered]
    chunk    {3} inline.bundle.js, inline.bundle.map (inline) 0 bytes [entry] [rendered]
    
    ERROR in Cannot determine the module for class PageNotFoundComponent in /Users/hansl/Sources/_Temp/test1/src/app/common/page-not-found/page-not-found.component.ts!
    
    ERROR in ./src/main.ts
    Module not found: Error: Can't resolve './app/app.module.ngfactory' in '/Users/hansl/Sources/_Temp/test1/src'
     @ ./src/main.ts 4:0-64
     @ multi main
    
    ERROR in ./~/@angular/core/src/linker/system_js_ng_module_factory_loader.js
    Module not found: TypeError: Cannot convert undefined or null to object
     @ ./~/@angular/core/src/linker/system_js_ng_module_factory_loader.js 69:15-36 85:15-102
     @ ./~/@angular/core/src/linker.js
     @ ./~/@angular/core/src/core.js
     @ ./~/@angular/core/index.js
     @ ./src/main.ts
     @ multi main
    webpack: bundle is now VALID.
    

    The error is "Cannot determine the module for class PageNotFoundComponent". Every components built need to be declared in an NgModule, so this is an error.

    Two things you can do to fix:

    1. add the component to a module's declaration (which seems like what you'd like to do)
    2. change the tsconfig to only compile the main.ts (adding "files": ["main"]).

    Point 2 we can actually improve on by having main.ts in our tsconfig by default when creating new apps, so we'll do that. In the meantime you have fixes for this problem and I'll close this issue as fixed.

    Cheers!

  11. vfedoriv commented on Dec 17, 2016

    @vfedoriv
    Author

    Well, i generate clean project w/o other logic and just add one import.
    Same error with 'ng test'
    Try this: https://github.com/vfedoriv/test3.git

  12. angular-automatic-lock-bot commented on Sep 6, 2019

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  13. locked and limited conversation to collaborators on Sep 6, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs: repro stepsWe cannot reproduce the issue with the information given

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions