Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

npm - Install Angular library from local folder or network folder

If I create an angular library like this:

ng new libraries-workspace --create-application=false
cd libraries-workspace
ng generate library test-library

And now I build the library

ng build test-library --watch

Now if I create a new angular application in a completely different folder (away from the workspace)

ng new test-project

cd test-project

How can I reference or install my library without publishing to NPM. So basically I want to install the library from a folder

something like this:

ng add test-library c:libraries-workspacedistest-library...

I don't want to add my project to the workspace folder.

Thanks

question from:https://stackoverflow.com/questions/65853612/install-angular-library-from-local-folder-or-network-folder

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Edit 07.02.2021: An easier way

You can link your angular library directly to your application of choice, e.g. your main app. To do so, follow these steps:

  1. Navigate to your angular library project's output directory in a terminal and run the command npm link. You may have to trigger ng build once, so that the output directory exists. This will make your library locally available on your machine.
  2. Now navigate to your main application in which your angular library is used as a dependency and run the command npm link [name_of_your_module]
  3. Add "preserveSymlinks": true to your angular.json file under projects > architect > build
  4. Run your main application with ng serve, make some changes in your library, build it. Depending on your IDE, you can create a watch task which triggers an automatic build. You should be able to see your changes.
  5. Finally if you are done testing your changes, you can unlink your dependency from your main application by running the command npm unlink [name_of_your_module] in your main application's root directory. Go to your angular library project directory and run npm unlink so that your library isn't locally available anymore. You can now publish your changes and intall your latest version with npm install in your main application.

Remember it has to be the dist output folder since the angular cli does not support directly linking your library. You can also link libraries to other angular libraries in the same way, but you will have to add "preserveSymlinks": true to your library's tsconfig.json

Old answer:

You have to first pack the build output of your library. For that you have to use npm pack command inside the build output folder. Npm pack will create a package, copy the path of the created package, navigate to your test project in the terminal and hit npm install "C:patho pmpackpackage"


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...