1

I built an Angular 6 app consisting of a library containing components that should be reused in the future and an app. I created everything using angular cli, so I have the default structure

my-app
 |
 +-projects
 |   |
 |   +-my-lib
 |      |
 |      +-src
 |      +-package.json
 |
 +-src
 +-package.json

To build the project, I first build the library using ng build my-lib --prod and then build the app using ng build --prod. Afterwards the dist directory looks like this

dist
 |
 +-my-app
 +-my-lib

My question is now how to deploy this to my server, so that the app has access to the library. Locally everything works, so should I just transfer the whole dist directory to my server as usual? Or should I publish the library to npm and add it as a regular dependency to my package.json. And if yes, how do I set this up to not interfere with local development, where I want to build against the library in the dist directory?

2
  • I would probably publish on npm, that way anyone can pull from npm. I think with angular 6 you can package now and publish easier, blog.angularindepth.com/… or using an npm lib called ngpackagr npmjs.com/package/ng-packagr Commented Jul 26, 2018 at 14:19
  • Yes it would be no problem, however I would have to add the library as a npm dependency to my package.json, which means that for local test builds it would also build against the npm version. I would have to have two package.json s then Commented Jul 26, 2018 at 15:45

1 Answer 1

5

Your my-app build already contains my-lib library therefore it should be enough to deploy just dist/my-app.

As for publishing to npm it is really up to you.

For local development, you might consider using npm link:

  1. cd dist/my-lib
  2. npm link
  3. cd to/your/project/dir
  4. npm link your-library-name (probably my-lib)

When you change the library and build again, there is no need to redo link, it will pickup new version.

However from my experience when you update or install other "regular" packages you must do the link again.

Sign up to request clarification or add additional context in comments.

2 Comments

That would be great. I'll try to deploy the app itself. npm link is not necessary with the newest angular cli version. Adding the lib as a library to the angular.json is enough to build the whole project
finally had the chance to test this, and the library is indeed included in the my-app bundle. Thank you very much

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.