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?
@julien-arhs
- in your-library place a
package.jsonundersrc/but without entries independencies - in your-library goto
src/and typenpm link - goto your project and type
npm link your-library
Thats it.
Nice ! I didn't get the trick to create a package.json at src level for linking. Does the content of the file matter ? Or is the purpose only to change the npm linking scope ?
@julien-arhs the src/package.json will allow you to indicate the proper entry point:
"main": "./index.ts",
It works without any main value, index.ts must be a kind of default. But for understanding it is probably better to write it.
I have tried the solution of @sunew exactly step by step (on Windows, with mklink instead of ln) but the update/refresh behavior (while ng serve) does not work.
@julien-arhs @ebrehault Do you also use Mac like @sebastian-man ? Is it just a Windows thing?
I use a Mac os 10.12.6.
Did you also try to link you src folder using npm link as explained by @sebastian-man ?
And don't forget to use the ng serve --preserve-symlinks or set it as default in angular-cli.json
"defaults": {
"styleExt": "scss",
"component": {},
"build": {
"preserveSymlinks": true
}
}
For me, it works on MacOSX and Ubuntu.
@julien-arhs I have defined --preserve-symlinks in the angular-cli.json. In this case I did not use npm link. The using of npm link as described in my inital post here would bring the same problem and in the end to set own soft links manually seems to be the same as npm link does. Or do you mean I shall use the manual softlink declaration AND npm link the same time?
@ebrehault Thanks.
Both methods worked for me. But the npm link method to link the library in the /node-modules folder of the project is the better way to do it imo.
As said by @sebastian-man :
in your-library place a package.json under src/ but without entries in dependencies in your-library goto src/ and type npm link goto your project and type npm link your-library
@julien-arhs What does "worked for me" means in your case? Just to consume the library in the CLI App? Or even more the code refresh when library code changes when running the CLI app with ng serve?
Also the code refresh. I've built my lib using this generator https://github.com/tinesoft/generator-ngx-library:
- I run the gulp task to watch/re-build the lib.
- The served consuming app is refreshed with changes.
Windows:10, Library generated by generator-angular2-library. Linked dist dir inside lib using npm.
-
Angular Cli: 1.4.2 Run cmd: ng serve --preserve-symlinks when lib changes, cli detects changes, browser does NOT reflect changes.
-
Angular Cli :1.2.7 Works as expected.
I then ejected webpack from cli 1.4.2 setup using ng eject. Added ngtools/webpack to package.json
ngtools/[email protected] works. ngtools/[email protected] fails. This coincides with 1.3.0 release of angular cli and the introduction of --preserve-symlinks?
I cannot figure out what has changed between ngtools/webpack 1.6.0-rc5 and 1.6.0 ?
I am using cli 1.2.7 for now.
After spending some time investigating this issue I found the following solution for linking angular libs by source. Some has already been outlined above but this is the summary:
- run
npm linkfrom the source directory of your library - run
npm link <libname>from the root of your consuming application - add the following to the tsconfig.json or tsconfig.app.json of your consuming application:
"include": [ "../node_modules/<libname>/**/*", "**/*"] - add the
--preserve-symlinksoption to either theng serveorng buildcommands
This should fix the compilation errors. You may notice that during ng serve the file watcher in angular-cli/webpack doesn't trigger a recompile from library source file changes. This is due to the --preserve-symlinks flag not propagating to the webpack file watcher. I have opened an issue here: https://github.com/webpack/watchpack/issues/61 Essentially you need to open the node_modules\watchpack\lib\DirectoryWatcher.js file in your consuming application and change followSymlinks from false to true.
It is not ideal as you need to hardcode paths to the tsconfig.json file but its the best solution i could find apart from messing around with copying lib code into the source tree or using external commands to manage links/junctions.
What works for me is to consume the library when compiled separately by ngc --watch, and not let the host app compile the library. ng serve in the host app still picks up changes made by the separate library compilation process. Have to use the preserveSymlinks option as said here to avoid duplicate instances of common libraries from node_modules, but otherwise no need to configure anything extra tsconfig.json of the consuming app.
My solution is implemented in my own experimental library packager ngmakelib, and some notes about linking / watching can be found here:
https://github.com/fintechneo/ngmakelib/blob/master/README.md#watch-mode-during-development
Can you still reproduce this issue?
Little late for the party, but...
if anyone still has issues of that kind, try angular cli.
The "library" schematic (called by ng g library) will generate a nice, clean and official solution for building external libraries.
And yes, also supports watch mode out of the box.
learn more: https://github.com/angular/angular-cli/wiki/stories-create-library
Yeah I remembered watching this ticket when I had to build external libraries with a third party tool like ngPackagr. With angular cli, I think this has now no reason to live. (at least for the next versions of cli)
I am using new angular cli and library schematics, but still can reproduce the issue(((
I am building the library with ng build lib-name --watch , install library from local folder, and run main application with ng serve with preserveSymlinks.
@ToniaDemchuk did you enable resource-inlining in tsconfig.lib.json?
"angularCompilerOptions": {
"enableResourceInlining": true
}
@ToniaDemchuk did you enable resource-inlining in tsconfig.lib.json?
"angularCompilerOptions": { "enableResourceInlining": true }
@synctom Yes, I did