How to run angular-cli from Vagrant box?

2019-02-21 15:00发布

问题:

I've installed angular-cli with node v6.5.0 and npm v3.10.3 to my vagrant (Scotch) box. After running 'ng serve --port 4201' I can see that the build was successful and that the app is now serving on http://localhost:4201/

However I can't just use above address because I'm running on VM. I've tried to add '127.0.0.1:4201' to hosts file on host machine, but can't get anything working.

Thx

回答1:

Vagrant (VM) doesn't trigger file change events. This bug is reported to VirtualBox community and is set to won't fix. For now it's not possible to work with Angular 2 via Vagrant. See this issue on GitHub for relevant discussion on this.



回答2:

Run ng serve like this:

ng serve --host 0.0.0.0

This will make the server in your vagrant machine accessible from outside using its ip (mine is 192.168.10.10 using homestead) and the default 4200 port for angular serve (http://192.168.10.10:4200).

Some more info:

  • StackOverflow answer:

127.0.0.1 is a loopback address so if you assign this IP (or localhost) to the config of your server, its not accessible from other network interfaces, so you cannot access it from outside of the VM. If you bind your server to 0.0.0.0 its available to all interfaces

  • Angular CLI


回答3:

You have to add some config to Vagrantfile.

To access site add these lines (it helped me):

config.vm.network "private_network", ip: "192.168.100.100"
config.vm.network "forwarded_port", guest: 4200, host: 4200
config.vm.network "forwarded_port", guest: 49152, host: 49152

You can access site on: http://192.168.100.100:4200/



回答4:

There are multiple ways you can do it , for myself, what i did was first i installed VMware(Virtual machine), make sure you get the latest version.

After succesfully installing vm , download nodejs and vagrant. open up cmd >> locate it to the the folder where your vagrant is, Next type in Vagrant up and it should run .. If you face any more problems. try keying this vagrant ssh [name|id] [-- extra_ssh_args]..

Hope that helps