angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

Access outside localhost

Open ShadowManu opened this issue 10 years ago • 37 comments

Please provide us with the following information:

  1. OS? Windows 7, 8 or 10. Linux (which distribution). Mac OSX (Yosemite? El Capitan?) Linux Ubuntu 16.04 LTS
  2. Versions. Please run ng --version. If there's nothing outputted, please run in a Terminal: node --version and paste the result here: angular-cli: 1.0.0-beta.11-webpack.2 (actually latest master at the time of this issue)
  3. Repro steps. Was this an app that wasn't created using the CLI? What change did you do on your code? etc. None in particular
  4. The log given by the failure. Normally this include a stack trace and some more information.
  5. Mention any other details that might be useful. Is there a way to access the app from ng serve (either dev or prod variants) from another LAN device? Thanks.

Thanks! We'll be in touch soon.

ShadowManu avatar Aug 22 '16 01:08 ShadowManu

As of at least Angular CLI 1.0 you can set these directly in .angular-cli.json underneath the defaults element:

{
  "defaults": {
    "serve": {
      "port": 4444,
      "host": "0.0.0.0"
    }
  }
}

Source

taurus227 avatar Apr 07 '18 18:04 taurus227

@taurus227 's solution is the ONLY one that worked for me. Remember to replace "0.0.0.0" host with your ip. ng-serve kept saying:

** NG Live Development Server is listening on 0.0.0.0:4200, open your browser on http://localhost:4200 **

or "localhost" in place of "0.0.0.0"

Now that it works, it says:

** NG Live Development Server is listening on 192.168.x.y:4200, open your browser on http://192.168.x.y:4200 **

alberthendriks avatar May 02 '18 10:05 alberthendriks

Actually for me it works with 0.0.0.0. That just means any IP address.

taurus227 avatar May 02 '18 13:05 taurus227

without use ng-cli

npm run start -- --host=0.0.0.0 --port=4500

after first -- start all parameters for ng-cli as you need it.

porfidev avatar May 08 '18 14:05 porfidev

run the command

ng serve --host=0.0.0.0 --disable-host-check

to disable host check and access with IPAddress

pallapraveen avatar May 09 '18 17:05 pallapraveen

I wanted to ask about the new angular.json file which replaced the .angular-cli.json. I used to override the host value there, but now i can not find how to override it. Does anyone has run into the same problem? Can you suggest me a solution other than writing host as parameter?

sotiris-rafail avatar May 10 '18 06:05 sotiris-rafail

@sotiris-rafail I think what you are looking for is "dev-server" which has the "host" option. It looks like "host" is not available in the "server" section of the angular.json.

References

  • https://github.com/angular/angular-cli/wiki/angular-workspace
  • https://github.com/angular/angular-cli/blob/v6.0.0-rc.8/packages/%40angular/cli/lib/config/schema.json#L907-L1028

ScottFred avatar May 26 '18 15:05 ScottFred

For anyone looking on how to solve that with the new angular.json file, adding the "host": "0.0.0.0" line at the right place works for me

        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "browserTarget": "project:build",
            "host": "0.0.0.0"
          },

jyte avatar Aug 17 '18 08:08 jyte

For mac:

  1. check the ip address of your machine with this command: ifconfig |grep inet
  2. copy the ip address.
  3. now type: ng serve --host screen shot 2018-09-11 at 1 54 12 pm

ankushthakur12 avatar Sep 11 '18 18:09 ankushthakur12

I have the same issue and using npm run ng serve --host 0.0.0.0 does not allow it to be accessed on network. Though app is still accessible locally.

naveedkamran avatar Sep 24 '18 15:09 naveedkamran

ng serve --host 0.0.0.0 is a security risk isn't it? Angular cli giving warning. I don't think we can use this for production deployment

> ng serve --host 0.0.0.0

WARNING: This is a simple server for use in testing or debugging Angular applications
locally. It hasn't been reviewed for security issues.

Binding this server to an open connection can result in compromising your application or
computer. Using a different host than the one passed to the "--host" flag might result in
WebSocket connection issues. You might need to use "--disableHostCheck" if that's the
case.
** Angular Live Development Server is listening on 0.0.0.0:4200, open your browser on http://localhost:4200/ **

and with --disableHostCheck

WARNING: Running a server with --disable-host-check is a security risk. See https://medium.com/webpack/webpack-dev-server-middleware-security-issues-1489d950874a for more information.

iampratapak avatar May 16 '19 06:05 iampratapak

I have the same issue and using npm run ng serve --host 0.0.0.0 does not allow it to be accessed on network. Though app is still accessible locally.

@naveedkamran if you are runing from npm you'll need to add two more hipens after ng serve. Like this npm run ng serve -- --host=0.0.0.0

rovertzxd avatar May 23 '19 00:05 rovertzxd

@pratapgowda007 You shouldn't be using ng's dev server for production, not sure what you mean

juanmendes avatar Jun 13 '19 19:06 juanmendes

update to @jyte answer - solution for angular 7 changed to

{ "projects": {
    "foo-project": {
      "architect": {
        "serve": {
          "configurations": {
            "host": {
              "host": "0.0.0.0"
            }
          }}}}}}

jkatnik avatar Aug 11 '19 08:08 jkatnik

update to @jyte answer - solution for angular 7 changed to

{ "projects": {
    "foo-project": {
      "architect": {
        "serve": {
          "configurations": {
            "host": {
              "host": "0.0.0.0"
            }
          }}}}}}

Doesn't work for me in Angular 8. But @jyte 's answer works for me in Angular 8.

JeffreyXieCn avatar Aug 14 '19 15:08 JeffreyXieCn

I follow all the possible solution from this page, and I can say I successfully accessed my application outside my localhost, but the problem is when I try to sign in, I cannot log in and I can't see what is the error. Is their any possible solution for this, might I missed something. by the way I am newbie here, hope you guys can support my query, thank you so much

cnbalbin avatar Aug 15 '19 08:08 cnbalbin

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.