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

ng serve doesn't work with linked module

Open sichida opened this issue 9 years ago • 8 comments

Bug Report or Feature Request (mark with an x)

- [x] bug report
- [ ] feature request

Versions.

@angular/cli: 1.2.6 node: 6.11.1 os: win32 x64 @angular/animations: 4.3.3 @angular/common: 4.3.3 @angular/compiler: 4.3.3 @angular/core: 4.3.3 @angular/forms: 4.3.3 @angular/http: 4.3.3 @angular/platform-browser: 4.3.3 @angular/platform-browser-dynamic: 4.3.3 @angular/router: 4.3.3 @angular/cli: 1.2.6 @angular/compiler-cli: 4.3.3 @angular/language-service: 4.3.3

Repro steps.

Create a node module locally and link it with npm link. Go to your project generated with @angular/cli and link this module: npm link ngx-my-module. Use your module in your project. For instance:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { MyModule } from 'ngx-my-module';

import { AppComponent } from './app.component';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    AnalyticsModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

Then run ng serve

The log given by the failure.

λ ng serve
Cannot read property 'length' of undefined
TypeError: Cannot read property 'length' of undefined
    at createSourceFile (my-module\node_modules\typescript\lib\typescript.js:15457:109)
    at parseSourceFileWorker (my-module\node_modules\typescript\lib\typescript.js:15389:26)
    at Object.parseSourceFile (my-module\node_modules\typescript\lib\typescript.js:15338:26)
    at Object.createSourceFile (my-module\node_modules\typescript\lib\typescript.js:15192:29)
    at WebpackCompilerHost.getSourceFile (my-module\node_modules\@ngtools\webpack\src\compiler_host.js:239:27)
    at findSourceFile (my-module\node_modules\typescript\lib\typescript.js:67909:29)
    at processSourceFile (my-module\node_modules\typescript\lib\typescript.js:67840:27)
    at my-module\node_modules\typescript\lib\typescript.js:67950:17
    at Object.forEach (my-module\node_modules\typescript\lib\typescript.js:1449:30)
    at processReferencedFiles (my-module\node_modules\typescript\lib\typescript.js:67948:16)
    at findSourceFile (my-module\node_modules\typescript\lib\typescript.js:67933:21)
    at processImportedModules (my-module\node_modules\typescript\lib\typescript.js:68056:25)
    at findSourceFile (my-module\node_modules\typescript\lib\typescript.js:67937:17)
    at processImportedModules (my-module\node_modules\typescript\lib\typescript.js:68056:25)
    at findSourceFile (my-module\node_modules\typescript\lib\typescript.js:67937:17)
    at processImportedModules (my-module\node_modules\typescript\lib\typescript.js:68056:25)

Mention any other details that might be useful.

I used generator-ngx-library to generate my node module project. It uses @angular/cli in a demo app.

sichida avatar Aug 10 '17 09:08 sichida

This have been discussed many times as in https://github.com/angular/angular-cli/issues/6228#issuecomment-301024884. I believe the correct way to do this is to use tsconfig.json paths property, instead of linking it as a module. You can see paths it in @angular/cli tsconfig.json as well.

hieuxlu avatar Aug 11 '17 03:08 hieuxlu

I am having a similar issue since the cli >1.3 version. I did check the story on linked modules and - to my understanding - I am doing what is being asked there.

Picture: I am developing an App for the web and iOS/Android that uses a common set of core modules (mainly NGRX functionality and interfaces). Those core files are compiled into a module that gets linked to the web/ mobile app.

I compile the lib with ngc and link it via yarn link. In my web app's tsconfig.json I have the path section as described in the story, which worked with the CLI until the recent 1.3 release.

I am a little confused as to what is going here. I have also looked at the example repo that @filipesilva created. It seems overkill in my case (until now it worked well without all that).

The core module is here.

Any ideas? I would love to move along with cli and not be pinned to 1.2.4 ;)

ebtc avatar Aug 16 '17 11:08 ebtc

@ebtc Okay. This is another new issue with v1.3.0. You should fix the version to 1.3.0-rc.5 to see new features such as named-chunk (one of my favorite), while avoid the issue with tsconfig paths. For more discussion on this, read https://github.com/angular/angular-cli/issues/7341

hieuxlu avatar Aug 16 '17 11:08 hieuxlu

just to mention the error message:

Uncaught Error: Unexpected value 'MHCoreModule' imported by the module 'AppModule'. Please add a @NgModule annotation.

This appears in the browser itself (console), while the CLI output is fine.

If I don't use commonjs as module for the core module, then al hell breaks lose :)

ebtc avatar Aug 16 '17 11:08 ebtc

Thanks @hieuxlu for your reply. I've tried to remove my dependency from package.json and put it in peerDependencies instead and combine it with a link within my tsconfig.json as you have mentioned but I am still getting the same error. Any ideas? Thanks

sichida avatar Aug 16 '17 13:08 sichida

More information that may be helpful: I am using a definition.d.ts file to add some declaration to Window interface. If I remove this reference it is working fine. I believe the problem does not come from linked module but from the definition file used within the project. Make sense for you?

sichida avatar Aug 17 '17 13:08 sichida

Closing as a duplicate of https://github.com/angular/angular-cli/issues/6228#issuecomment-301024884 and https://github.com/angular/angular-cli/issues/7341, as @hieuxlu mentioned. Please see those issues for the recommended approaches.

filipesilva avatar Aug 21 '17 12:08 filipesilva

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.