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?
Great Scott!
@synctom It works! There is a different behavior.
I have linked the dist folder outside of the workspace directory. Now I can use ng serve without --preserve-symlinks and without the @NgModule annotation error. When I generate the dist by gulp into the folder outside of the workspace it also reflects the changes in the library and the app shows the updates directly.
So what is the root of all evil?
npm link works differently since npm version 5.
thanks to @stevencohn, he explained the issue pretty good https://github.com/angular/angular/issues/15763#issuecomment-312296944
Current solutions are:
Solution1: Remove symlink and then copy dist folder into node_modules. Solution2: Delete node_modules from library project. Solution3: Build library into dist folder that is not inside library project.
@synctom I guess that moving the dist folder outside the lib folder makes sure we do not get polluted by its node_modules, right?
Is there any way to ask the compiler to ignore all the lib's node_modules? (because moving the folder outise is not very handy)
Maybe using "compilerOptions": "paths": { "*": ["../node_modules/*"]} ?
EDIT: I tried "*": ["../node_modules/*"], it does not work
Any ideas @jvandemo? 🤔 https://github.com/angular/angular-cli/issues/6195#issuecomment-334706792
I have found a solution without moving the dist folder.
Instead, I remove the lib's node_modules folder, and I replace it with a symlink to the project's node_modules.
Hence, there is only one unique node_modules, no confusion possible, but it is available on both sides, so we can run build, tests, have IDE auto-completion, etc.
same issue here :(
Similar problem here, we've got angular cli based app and library created using https://github.com/filipesilva/angular-quickstart-lib.
We followed steps from https://github.com/angular/angular-cli/wiki/stories-linked-library and tried using npm link and also referencing library inside src/tsconfig.app.json of cli based project (through path pointing to src/lib of library).
In both cases, we are getting "Please add a @NgModule annotation" error once cli based app is run. We pinned it down to problem with @angular/core inside library's node_modules. Current work around for us is: -> temporarily rename 'library_folder/node_modules/@angular/core' to something else like core1 -> run npm start in cli based app -> rename core1 back to core inside library
We would very much appreciate this being fixed as manual rename step is really annoying.
The node_modules symlink solution is mentionned here https://github.com/angular/angular-cli/issues/6195#issuecomment-334755843 does work fine. But there is another solution:
- first make a symlink to your lib src folder into your project:
cd project/src
ln -s ../../library/src ./lib
- then use tsconfig
pathsto fix the path:
"paths": {
"my-library": ["lib"]
}
And now your lib code is in the project code (it will trigger autoreload and so on), but you can use import things just like if it was a regular dependency:
import { MyComponent } from 'my-library';
(credits should go @sunew https://github.com/plone/plone.restapi-angular/commit/6f9984f462b5ce0a65a02fb7fc4f5774a32bb146)
I just tried symlink solution as you described @ebrehault . It does reference the library code without needing to delete/rename node_modules/@angular/core but it doesn't trigger code refresh in cli based app when library code changes
UPDATE -. posted wrong link at first
See here: https://github.com/plone/plone.restapi-angular/blob/master/docs/development.rst
Maybe you need some step. Code refresh works for me (+ sourcemaps, debugger, etc).
The documentation here https://github.com/angular/angular-cli/wiki/stories-linked-library is not very clear, but after reading 100's of tickets for angular-cli I think I can conclude that method is for building the library first, and then running on the buildt version (ie. the js and not the ts)). The method uses tsconfig paths for the peerdependencies of the library, so angular does not get loaded twice (in perhaps different versions).
For plain development, debugging, exploring the code in the debugger, etc. my method works fine (docs/development.rst)
Let me know if you can make it work following docs/development.rst - maybe I forgot to document some step...
@adamlubek with node_modules symlink approach, I forgot to mention an important:
you need to create a package.json in library/src and add it as dependency in your project:
npm install ../library/src
It will create a symlink to library/src and code changes in the lib will trigger the CLI.
But anyway, @sunew solution is certainly cleaner and better.
Hi at all!
I have found a “solution” (more a workaround I think) in other way. But first...
@ebrehault I am confused about your node_modules symlink approach. It may work (did not test it) but if I do understand it right it is limited to the one App project. So you are just able to use the library with this one App (do I understand it right?). How would you re-use the library in other projects?
@sunew Your solution is similar to the one of @synctom (comment) or not? In your case you do copy the dist folder into the App project directly, right?
My Workaround Depending on the “get the dist folder out of the lib folder” comments by @synctom and @sunew I have tried the following approach.
What to do (based on my repositories):
- Set the path of the dist folder in the gulpfile.js to the global node_modules.
const distFolder = path.join(process.env.AppData, '\\npm\\node_modules\\@wampe\\lib-6195');
Now dist gets generated into the global node_modules directly (no npm link usage in the library project).
- Extend the delete function in the gulpfile.js with the force option.
function deleteFolders(folders) {
return del(folders, { force: true });
}
This option is necessary for the delete process outside of the workspace.
-
Link the generated dist folder (in global node_modules) into the App project
npm link @wampe/lib-6195 -
Run
ng serve(without --preserve-symlinks). The App should get showen (No ngModule annotation error). -
Make any changes in the library
After a manually gulp build (or automatically by gulp build:watch) again in the library project the changes should be reflected in the App (in Browser).
In short: Just move the dist folder to the global node_modules
disadvantage The dist folder is out of the workspace. So I have to navigate to the globals. In the meanwhile I am using gulp-directory-sync so it would be justifiable for me to have the dist folder redundant if I sync the local dist with the global dist.
advantages
• I can use the library (while developing) in multiple projects
• Several developers in a team can work together on the library and use it based on this uniform approach
• ng serve without --preserve-symlinks
• Changes in the library getting reflected
• I do not have to set any paths in any config files
This is just a workaround but this approach works best for me in my case until this issue is fixed.
@Wampe my solution can work with several projects as far as those projets have identical dependencies (as they will share the same node_modules). So yes it is a restriction.
I finally got it working without any need for rename/delete of node_modules in libraries. But, I encountered problems with symlink on win10 machine and I ended up using mklink through admin command prompt (instead of git bash).
Steps:
- install your lib in cli based app
- in root of your cli based app, create folder to hold libs (e.g. C:\CliApp\symlinklibs)
- create symlink to root src of your library (say your library root i.e. index.ts is in "C:\featureLib\src\lib" e.g.
mklink /J "C:\CliApp\symlinklibs\featureLib" "C:\featureLib\src\lib" - Now, with symlink created, in tsconfig.app.json located in C;\CliApp\src, add:
"paths": {
"feature-lib-packagejson-name": [
"../symlinklibs/featureLib"
]
}
- In .angular-cli.json add:
"defaults": {
"build": {
"preserveSymlinks": true
}
}
thanks @ebrehault and @sunew ,your comments and documentation are very much appreciated!
I have the same problem on a mac 10.12.6 with:
@angular/cli: 1.5.2
@angular-devkit/build-optimizer: 0.0.33
@angular-devkit/core: 0.0.20
@angular-devkit/schematics: 0.0.36
@ngtools/json-schema: 1.1.0
@schematics/angular: 0.1.5
typescript: 2.5.3
webpack: 3.8.1
I used https://github.com/trekhleb/angular-library-seed as a initial setup.
Same as @sebastian-man for me. The hack proposed by @synctom (disabling webpack caching) works fine, but it seems to be deprecated to disable caching https://github.com/angular/angular-cli/issues/7939.
Still waiting for an acceptable solution ? This problem is a big one for lib developpement isn't it ?
@sebastian-man @juliemr The solution is really quite simple, as described in the link in my above post.
@sunew I try your steps but this didnt solved my problem.
In my project I add this to the appplication/tsconfig.json
"paths": {
"@angular/*": [
"node_modules/@angular/*"
],
"@company/my-library": [
"packages/@company/my-library"
]
},
In the application/src/tsconfig.app.json
{
"extends": "../tsconfig.json",
"compilerOptions": {
"outDir": "../out-tsc/app",
"baseUrl": "./",
"module": "es2015",
"types": []
},
"exclude": [
"test.ts",
"**/*.spec.ts"
]
}
But same behavior. The app is reloaded but without the change.
In my library I also tried to change some things from the angular-library-seed.
So I create a own package.json with:
{
"name": "@company/my-library",
"version": "0.0.1",
"description": "Library",
"main": "./index.umd.js",
"module": "./index.js",
"typings": "./index.d.ts",
"license": "MIT",
"author": {
"name": "Username",
"url": "http://www.domain.com"
},
"dependencies": {},
"peerDependencies": {
"@angular/common": "5.x",
"@angular/core": "5.x",
"rxjs": "5.x",
"zone.js": "0.x"
}
}
And copy this to my library/dist folder.
In my dist folder I do the npm link command. So the link is to the compiled library source.
But this all didnt solved my problem. Currently its pain in the a** to develop our custom library.
I hope there is any handle solution (i mean without change manually the library and stuff like this, that isnt practicable in a team).
@sunew After reading your document again I get the point that you link directly to the src folder and not the compiled js. But what I didnt know is how to resolved this if I didnt compile the library?
"main": "./index.umd.js",
"module": "./index.js",
"typings": "./index.d.ts",
Update Ok the entries main, module, typings seems to be ignored if i linked directly the src folder.
And with this it works like @sunew told 👍
So thx @sunew for sharing this!
Ok it works fine when creating a specific symlink to have the src of the library in the consuming app, thank you for that. But I don't get the point if the lib dependency is not in the node_modules folder ? If I understand well your solution @sunew, you never treat those dependencies as npm packages, but only as "linked sources" ?
Please consider this simple development workflow :
- I create a new application
- I install my external library using npm install, it appears in node_modules
- Now I need to make changes in my library
- I use npm link to make the node_modules/library of my application target my local sources
- But I want these changes to be live reloaded, and for now it doesn't work.
Did I miss a solution for that case ?