ng serve - npm linked library changes(updates) are not reflected in UI
Bug Report or Feature Request (mark with an x)
- [x] bug report -> please search issues before submitting
- [ ] feature request
Versions.
@angular/cli: 1.0.1 node: 6.10.1 os: darwin x64 @angular/common: 4.1.1 @angular/core: 4.1.1 @angular/forms: 4.1.1 @angular/http: 4.1.1 @angular/platform-browser: 4.1.1 @angular/platform-browser-dynamic: 4.1.1 @angular/router: 4.1.1 @angular/cli: 1.0.1 @angular/compiler: 4.1.1 @angular/compiler-cli: 4.1.1
macOs Sierra 10.12.4 Node: v6.10.1 Npm: 3.10.10
Repro steps.
- Generate 'myProject' with angular-cli
- Add a private npm package: npm install @private/etcPackage --save (it's a angular4 library)
- Go to the private package local path and 'npm link'
- Go to 'myProject' and run: npm link @private/etcPackage
- Delete 'node_modules' from 'myProject/node_modules/@private/etcPackage'
- start serve: ng serve --aot
- on library project, '/localpath/etcPackage' run gulp (this will trigger watch and build on change)
Since this dist folder it's linked with node_modules one in my angular project 'myProject' for every build the package is update.
The log given by the failure.
Doesn't update UI on changes.
Desired functionality.
When the library (etc - in our case) it's compiled along with it's resources, the angular-cli project should see the changes and update the UI. Eg: external html templates and css - not updated.
Mention any other details that might be useful.
- I'm using templateUrl: 'etc.html' and not have any template inline;
- If any change is made to componentName.ts file - this is reflected on screen (eg: variables);
- Changes exist after library it gets build in 'myProject/node_modules/@private/etcPackage' folder, so link it's working;
- Even if i refresh page, and I check main.bundle.js - changes are there compiled, but still are not visible on screen unless I stop serve and start it again.
--
Question is, anything can be done or need create a separate watch script to support both the library and myProject?
Same issue here, using npm-linked module in an angular app. The module is a plain typescript module with no angular dependencies. It uses tsc to produce a dist folder containing index.js and index.d.ts. The package.json specifies these as main and types.
Repro steps:
- create new npm package
my-packagewhich exportsTest - create new angular app
my-app - use
npm linkto link package into the app - in app,
import { Testing } from 'my-package' - run
ng serve(fails: export is Test not Testing) - change
my-packageexportTesttoTesting - angular app rebuild is triggered, but has error:
ERROR in [...]/linked-lib-test/src/app/app.component.ts (2,10):
Module '"[...]/linked-lib-test/node_modules/linked-lib/dist/index"' has no exported member 'Testing'.
File under node_modules/linked-lib/dist/index.js clearly does have an exported member Testing.
Stopping ng serve and then running it again results in a successful build.
hi @tanyagray, unfortunately the issue still remains. angular cli is utilizing webpack to resolve module dependencies and build bundles. i believe the problem is somewhere in there.
In addition, the title of this issue is kind of misleading. this problem isn't limited to ui changes.
looks like some kind of caching issue during module resolution.
For those of you who are using generator-angular2-library and are suffering from broken links with a linked library (using npm link) during development, could you please try the following:
- open gulpfile.js in the root of your library directory
- replace the
clean:disttask with:
gulp.task('clean:dist', function () {
// Delete contents but not dist folder to avoid broken npm links
// when dist directory is removed while npm link references it.
return deleteFolders([
distFolder + '/**',
'!' + distFolder,
'!' + distFolder + '/*.umd.js'
]);
});
- run
gulpin your library directory - consume your library in an Angular CLI app (instructions)
- run
ng servein your Angular CLI app - make a change to your library and see if the consuming application picks up the changes properly
- post a comment in this issue to let us know if it works for you or not
It would be great to get feedback on whether this prevents the issue in different environments.
Thanks in advance! 👍
hi @jvandemo,
thanks for you effort! definitely fixes this issue where gulp and webpack deleting and reading the dist-folder simultaneously.
But still, try to rename a public method of a service provided by a linked dependency, it won't work, until you stop and execute ng serve again.
@synctom — I think the issue you are referring to is an underlying Webpack issue.
Did the change in the gulpfile.js resolve the issue with refreshing the browser during library rebuilds (where Webpack complains about missing files when gulp is rebuilding the library)?
Thanks again for testing, much appreciated! 👍
yes indeed, webpack doesn't complain about missing files anymore after implementing your fix.
@synctom — Excellent, thank you for the confirmation!
is there any workaround for assets folder as well in dist folder as well?
Hi,
I do have a problem with the update/refresh behavior of ng serve in an other way. TBH it does depend more on a different issue with NgModule annotation
Uncaught Error: Unexpected value '[object Object]' imported by the module 'AppModule'. Please add a @NgModule annotation.
and how to resolve it when using npm link.
Dev Environment
Windows 7 npm: 3.10.10 node: 6.11.1 Angular CLI: 1.3.0 Yo: 2.0.0 generator-angular2-library: 11.4.0 VS Code: 1.16.1
Projects
App created with the Angular CLI https://github.com/Wampe/Angular-CLI-App-6195
Library created with the generator-angular2-library based on the yeoman generator
https://github.com/Wampe/Angular-Lib-6195
(Gulp file modifications for clean:dist mentioned by @jvandemo considered)
Steps to reproduce
-
Load repositories
-
Angular-Lib-6195 (
@wampe/lib-6195)
- [ ] npm install
- [ ] npm run build (or gulp build)
- [ ] cd dist
- [ ] npm link
- Angular-CLI-App-6195
- [ ] npm link
@wampe/lib-6195 - [ ] npm install
- [ ] npm start (or ng serve)
Now when you open the App in Browser you should get the NgModule annotation error in console.
- Back to Angular-Lib-6195
- [ ] npm run build:watch
- [ ] Change anything you want and save it.
Now comes the fun: the update/refresh of ng serve works fine itself now (until this step). But just with the problem that with every update the NgModule annotation error appears again and again...
- Back to Angular-CLI-App-6195
- [ ] Stop current process of ng serve
- [ ] ng serve --preserve-symlinks (how described at the bottom of Consuming your library in a local application during development)
- [ ] open the App in Browser again
Now the App works. The default implementation of the Angular CLI template gets showen. But now the update/refresh behavior of ng serve doesn´t work anymore. Interestingly, update/refresh while running ng build --watch in the App project works (how already mentioned in this thread).
tl;dr Is it the expected behavior of ng serve to "ignore" updates in a source (via npm link) when using --preserve-symlinks or how can I get the update/refresh when using it?
I am experiencing the same type of issue. I have a main project and an external module (npm linked). I serve the application using ng serve.
As @synctom said, if I update existing methods it works fine but if I add new content (like a new method on a service) inside the external module and try using it in my main project, ng serve outputs error like:
ERROR in app/main/components/login/login.component.ts (36,18): Property 'hello' does not exist on type 'LoginFlow'.
ng serve success, only when I restart. Also, running ng build (main project) on a new terminal while ng serve is running works without any error. So I think there is some caching issue (ng serve, webpack or whatever, ...). I've inspected the /gendir of the external module and the newly added method is present and correct.
I've tried with the last stable release of cli (1.4.3) & adding --poll option on ng serve & adding more watchers (inotify). Also I build my external module with ngc
Here is my setup:
@angular/cli: 1.2.7
node: 6.11.0
os: linux x64
@angular/animations: 4.3.6
@angular/common: 4.3.6
@angular/compiler: 4.3.6
@angular/core: 4.3.6
@angular/forms: 4.3.6
@angular/http: 4.3.6
@angular/platform-browser: 4.3.6
@angular/platform-browser-dynamic: 4.3.6
@angular/router: 4.3.6
@angular/cli: 1.2.7
@angular/compiler-cli: 4.3.6
editor: VScode
Good news guys, i found the issue.
As I already suspected it's a caching issue located in a Webpack plugin that compiles your angular code.
Check @ngtools/webpack/src/plugin.js in the node_modules folder of your cli application.
Just remove or uncomment:
this._compilerHost.enableCaching();
Can anyone confirm?
@synctom Your solution works perfectly :)
I tried once with ng serve which built successfuly without error this time (after making change to my external module & updated my main app). I'm reinstalling all my dependencies (npm) for further tests but npm seems down with scoped packages right now.
@synctom In my case it doesn´t help.
By the way (a stupid question):
Check @ngtools/webpack/src/plugin.js in your node_modules folder.
Global? Local (App) or local (library)? I did try each case.
@Wampe ./node_modules/@ngtools/webpack/src/plugin.js in your angular-cli application. updated my answer, thanks.
I'm getting the exact same thing @Wampe is describing (also using the gulp fix from @jvandemo ), and have tried the /@ngtools/webpack/src/plugin.js fix also mentioned by @synctom and nothing has fixed the issue completely.
When doing ng serve, I'm getting the same type of Uncaught Error at runtime (mine giving me Uncaught Error: Unexpected value 'MyLibraryModule' imported by the module 'AppModule'. Please add a @NgModule annotation.). When doing ng serve --preserve-symlinks, there's no recompilation triggered by the Angular CLI application.
hi @deckerbd, hi @Wampe
if you want to store your angular components / pipes / modules / etc. in a external package (and probably link it to your app while developing) make sure you ...
- list all @angular/* dependencies as
peerDependencies(package.json of your external lib) - list all @angular/* dependencies as
devDependencies(package.json of your external lib) - remove all @angular/* dependencies from
dependencies(package.json of your external lib) - modify /src/tsconfig.app.json within your cli app like this
{
"compilerOptions": {
// ...
// Note: these paths are relative to `baseUrl` path.
"paths": {
"@angular/*": [
"../node_modules/@angular/*"
]
}
}
}
This ensures that typescript will always load the local copies of the angular-modules your library asks for.
learned from angular-cli wiki
Hey @synctom
Double-checked and I have all the steps you mentioned already done (peerDependencies, devDependencies correct and match, no dependencies listed, tsconfig.app.json in app modified), still the same thing. @Wampe looks to have this in their repo as well if I'm not mistaken.
node: 8.2.1
npm: 5.3.0
os: win32 x64 (Windows 10)
@angular/common: 4.4.3
@angular/compiler: 4.4.3
@angular/compiler-cli: 4.4.3
@angular/core: 4.4.3
@angular/forms: 4.4.3
@angular/platform-browser: 4.4.3
@angular/platform-browser-dynamic: 4.4.3
@angular/router: 4.4.3
typescript: 2.2.2
yo: 2.0.0
generate-angular2-library: 11.4.0
With my new dev environment
Windows 10 npm: 3.10.10 node: 6.11.4 Angular CLI: 1.4.4 Yo: 2.0.0 generator-angular2-library: 11.4.0 VS Code: 1.16.1 and the newest npm package versions
I do still have the same issue like written in (comment) So it seems in my case it is not an issue with a specific version (of any dev resource or OS) or it isn not still fixed.
"paths": { "@angular/*": [ "../node_modules/@angular/*" ] }
@synctom I tried it with and without the path declaration. Meanwhile if I understand it right it is obsolete? comment 1 | comment 2 Maybe @filipesilva can say more.
@Wampe - While developing a library, you'll need to have all of your peer dependencies also installed via devDependencies - otherwise you could not compile.
And if you do so, you definitely need to add paths to your typescript-configuration.
BUT if you are using angular2-library its a litte bit different. ;) a angular2-library project works like a factory that produces a 'npm-linkable' output (see dist folder). and this output has its own package.json without any devDependencies. so in this case, no paths is required.
But only because you link the dist folder to you cli-app, not the library itself.
@Wampe - could you please try this:
-
npm unlink lib-6195 with:
npm unlink @wampe/lib-6195(in app) andnpm unlink(in lib) -
rebuild your lib with 'npm run build'
-
then copy the dist-folder to a different location, out of your lib-folder, thats important.
-
then link the dist folder again
-
try to modify your code in dist folder while cli app is running
Any different behavior?