Skip to content

Component library local development with npm link error: "Please add a @NgModule annotation" #6505

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ernestbofill opened this issue May 30, 2017 · 15 comments
Labels
needs: investigation Requires some digging to determine if action is needed type: bug/fix

Comments

@ernestbofill
Copy link

Bug Report or Feature Request (mark with an x)

- [x] bug report -> please search issues before submitting
- [ ] feature request

Versions.

@angular/cli: 1.0.6
node: 6.9.5
os: win32 x64
@angular/common: 4.1.3
@angular/compiler: 4.1.3
@angular/core: 4.1.3
@angular/forms: 4.1.3
@angular/http: 4.1.3
@angular/platform-browser: 4.1.3
@angular/platform-browser-dynamic: 4.1.3
@angular/router: 4.1.3
@angular/cli: 1.0.6
@angular/compiler-cli: 4.1.3

Repro steps.

  1. Create an empty angular-cli project
  2. Create a new Angular component library with at least one module and one component.
  3. Build the component library with ngc -p ./src/tsconfig.lib.json
  4. Register the component library running npm link at the /dist folder of the library.
  5. From the angular-cli project link to the component library: npm link component-library-package-name
  6. Use the component library (import a module and use a component).
  7. Start the angular-cli project with ng serve.
  8. It compiles but it will fail in the browser with the following error: Unexpected value 'MyModule' imported by the module 'AppModule'. Please add a @NgModule annotation.

The log given by the failure.

Chrome error log:

compiler.es5.js:1540 Uncaught Error: Unexpected value 'MyModule' imported by the module 'AppModule'. Please add a @NgModule annotation.
    at syntaxError (http://localhost:4200/vendor.bundle.js:29720:34)
    at http://localhost:4200/vendor.bundle.js:42674:44
    at Array.forEach (native)
    at CompileMetadataResolver.getNgModuleMetadata (http://localhost:4200/vendor.bundle.js:42657:49)
    at JitCompiler._loadModules (http://localhost:4200/vendor.bundle.js:53810:66)
    at JitCompiler._compileModuleAndComponents (http://localhost:4200/vendor.bundle.js:53769:52)
    at JitCompiler.compileModuleAsync (http://localhost:4200/vendor.bundle.js:53731:23)
    at PlatformRef_._bootstrapModuleWithZone (http://localhost:4200/vendor.bundle.js:5731:25)
    at PlatformRef_.bootstrapModule (http://localhost:4200/vendor.bundle.js:5717:21)
    at Object.122 (http://localhost:4200/main.bundle.js:303:124)

Desired functionality.

Mention any other details that might be useful.

@clydin
Copy link
Member

clydin commented May 30, 2017

You can find information related to using linked libraries here: https://github.com/angular/angular-cli/wiki/stories-linked-library

If you are still having issues, please let us know and if possible provide a sample library or other repro.

@ernestbofill
Copy link
Author

@clydin thanks, I still have issues. I believe my component library follows the guidelines from the link you provided.

I attach a zip file with a sample library and an angular-cli application using it which can be used to reproduce the issue:

npmlinktest.zip

@sumitarora sumitarora self-assigned this May 30, 2017
@sumitarora sumitarora added needs: investigation Requires some digging to determine if action is needed type: bug/fix labels May 30, 2017
@clydin
Copy link
Member

clydin commented May 30, 2017

Your component library setup looks fine.
The app needs some slight adjustment to ensure that the application's Angular libraries are used and not the library's development dependencies. This change will essentially mimic the standard behavior of the Angular peer dependencies in the context of a linked library.
In src/tsconfig.app.json, add the paths setting as follows:

{
  "compilerOptions": {
    // ...
    "paths": {
      "@angular/*": [
        "../node_modules/@angular/*"
      ]
    }
  }
}

@ernestbofill
Copy link
Author

Great. That fixes it. I can see now that it was also explained in the guide you provided earlier but I didn't understand it there.

@matthewhegarty
Copy link

matthewhegarty commented Sep 15, 2017

Adding paths didn't work for me. I removed linked library's node_modules and that is a pain but fixes the issue (actually I just needed to remove node_modules/@angular/core). See this thread.

I've followed all of the steps in the guide carefully, but I must be missing something because I always get the error.

@DooMMasteR
Copy link

I have the same issue, but only in development, the result of the production compile run works just fine.
"paths": { "@angular/*": ["../node_modules/@angular/*"] } is already set up in my tsconfig.app.json but in development I still get an @NgModule annotation error.

@angular/cli: 1.4.3
node: 6.11.3
os: linux x64
@angular/animations: 4.4.3
@angular/common: 4.4.3
@angular/compiler: 4.4.3
@angular/core: 4.4.3
@angular/forms: 4.4.3
@angular/http: 4.4.3
@angular/platform-browser: 4.4.3
@angular/platform-browser-dynamic: 4.4.3
@angular/router: 4.4.3
@angular/cli: 1.4.3
@angular/compiler-cli: 4.4.3
@ngtools/webpack: 1.7.1
typescript: 2.3.4

@kalyan-anna
Copy link

deleting the node_modules folder from linking library solved the error for me

@endy21osu
Copy link

endy21osu commented Oct 19, 2017

I am having the same issue when i try to link a module after running ngc.

The issue goes away if i use "@angular/cli": "1.2.7" but if i use 1.3.0 and up to 1.4.7 i have the issue where i am asked to "Please add a @NgModule annotation." in the console of the browser. any ideas?

node -v
v6.11.3

    "@angular/cli": "1.3.0",
    "@angular/compiler-cli": "^4.3.0",
    "@angular/language-service": "^4.3.0",
    "@types/jasmine": "~2.5.53",
    "@types/jasminewd2": "~2.0.2",
    "@types/node": "~6.0.60",
    "@types/lodash": "^4.14.68",
    "codelyzer": "~3.1.1",
    "jasmine-core": "~2.6.2",
    "jasmine-spec-reporter": "~4.1.0",
    "karma": "~1.7.0",
    "karma-chrome-launcher": "~2.1.1",
    "karma-cli": "~1.0.1",
    "karma-coverage-istanbul-reporter": "^1.2.1",
    "karma-jasmine": "~1.1.0",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.1.2",
    "ts-node": "~3.2.0",
    "tslint": "~5.3.2",
    "typescript": "~2.3.3"

@matthewhegarty
Copy link

@endy21osu Hi Jeff - you can try removing the linked library's node_modules directory (or just @angular/node_modules) - see above.

See also this issue

@endy21osu
Copy link

Hi Matt,

Thanks for the response. If you are talking about the node_modules folder that might be in the dist folder after running ngc then yes I did delete that. Is there a different one i should be deleting?

@matthewhegarty
Copy link

It will be the node_modules folder in whatever is the target of your npm link

@endy21osu
Copy link

yes i did delete it. here is what it looks like
image

@magemello
Copy link

@endy21osu, I explained how I fix it here: angular/angular#15763 (comment), I hope it can help you...

@mprinc
Copy link
Contributor

mprinc commented Nov 9, 2017

Here i tried to elaborate more on the problem: #2154 (comment)

@angular-automatic-lock-bot
Copy link

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.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 7, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
needs: investigation Requires some digging to determine if action is needed type: bug/fix
Projects
None yet
Development

No branches or pull requests

9 participants