When to use 'npm start' and when to use 'ng serve'?

后端 未结 5 1464
离开以前
离开以前 2020-12-02 05:19

ng serve serves an Angular project via a development server

 

npm start runs an ar

5条回答
  •  醉梦人生
    2020-12-02 05:44

    For a project that's using the CLI, you will usually use ng serve. In other cases you may want to use npm start. Here the detailed explanation:

    ng serve

    Will serve a project that is 'Angular CLI aware', i.e. a project that has been created using the Angular CLI, particularly using:

    ng new app-name
    

    So, if you've scaffolded a project using the CLI, you'll probably want to use ng serve

    npm start

    This can be used in the case of a project that is not Angular CLI aware (or it can simply be used to run 'ng serve' for a project that's Angular CLI aware)

    As the other answers state, this is an npm command that will run the npm command(s) from the package.json that have the identifier 'start', and it doesn't just have to run 'ng serve'. It's possible to have something like the following in the package.json:

       "scripts": {
         "build:watch": "tsc -p src/ -w",
         "serve": "lite-server -c=bs-config.json",
         "start": "concurrently \"npm run build:watch\" \"npm run serve\""
         ...
       },
       "devDependencies": {
         "concurrently": "^3.2.0",
         "lite-server": "^2.2.2",
    

    In this case, 'npm start' will result in the following commands to be run:

    concurrently "npm run build:watch" "npm run serve"
    

    This will concurrently run the TypeScript compiler (watching for code changes), and run the Node lite-server (which users BrowserSync)

提交回复
热议问题