ng serve通过一个开发服务器来服务Angular项目
NPM start运行包的“start”中指定的任意命令。 属性的“scripts”对象。如果没有指定“start”属性 在“scripts”对象上,它将运行node server.js。
看起来ng serve启动的是嵌入式服务器,而npm start启动的是Node服务器。
有人能解释一下吗?
ng serve通过一个开发服务器来服务Angular项目
NPM start运行包的“start”中指定的任意命令。 属性的“scripts”对象。如果没有指定“start”属性 在“scripts”对象上,它将运行node server.js。
看起来ng serve启动的是嵌入式服务器,而npm start启动的是Node服务器。
有人能解释一下吗?
当前回答
从文档中
npm-start:
这将运行包的“scripts”对象的“start”属性中指定的任意命令。如果在“scripts”对象上没有指定“start”属性,它将运行node server.js。
这意味着它将在package.json中调用开始脚本
"scripts": {
"start": "tsc && concurrently \"npm run tsc:w\" \"npm run lite --baseDir ./app --port 8001\" ",
"lite": "lite-server",
...
}
的服务:
由angular/angular-cli提供,用于启动由angular-cli创建的angular2应用。当你安装angular-cli时,它会创建ng。打开C:\Users\name\AppData\Roaming\npm (windows)下的cmd命令,执行“%~dp0\node.exe”“%~dp0\node_modules\angular-cli\bin\ng”%*
使用npm start,你可以自己执行,ng serve只适用于angular-cli
请参见:运行ng serve时会发生什么?
其他回答
对于使用CLI的项目,通常会使用ng serve。在其他情况下,你可能想使用npm start。下面是详细的解释:
的服务
将服务于一个“Angular CLI感知”的项目,即一个使用Angular CLI创建的项目,特别是使用:
ng new app-name
所以,如果你已经使用CLI搭建了一个项目,你可能会想要使用ng serve
npm开始
这可以在不支持Angular CLI的项目中使用(或者可以简单地为支持Angular CLI的项目运行'ng serve')。
正如其他答案所述,这是一个npm命令,将从包中运行npm命令。带有start标识符的Json,它不需要运行ng serve。在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",
在这种情况下,'npm start'将导致运行以下命令:
concurrently "npm run build:watch" "npm run serve"
这将同时运行TypeScript编译器(监视代码更改),并运行Node lite-server(使用BrowserSync)
最好的答案很好,简短而中肯,但我想把我的钱花得值。
基本上npm start和ng serve在Angular项目中可以互换使用,只要你不希望这个命令做额外的事情。我来详细说明一下。
例如,您可能希望在package中配置代理。Json启动脚本:"start": "ng serve——proxy-config proxy.config.json"
显然,仅使用ng serve是不够的。
另一个例子是,你需要使用一些额外的选项,而不是使用默认值,比如定义临时端口:ng serve——port 4444
有些参数只对ng serve可用,其他参数对npm start可用。注意,端口选项适用于这两种情况,所以在这种情况下,还是取决于您的喜好。:)
从文档中
npm-start:
这将运行包的“scripts”对象的“start”属性中指定的任意命令。如果在“scripts”对象上没有指定“start”属性,它将运行node server.js。
这意味着它将在package.json中调用开始脚本
"scripts": {
"start": "tsc && concurrently \"npm run tsc:w\" \"npm run lite --baseDir ./app --port 8001\" ",
"lite": "lite-server",
...
}
的服务:
由angular/angular-cli提供,用于启动由angular-cli创建的angular2应用。当你安装angular-cli时,它会创建ng。打开C:\Users\name\AppData\Roaming\npm (windows)下的cmd命令,执行“%~dp0\node.exe”“%~dp0\node_modules\angular-cli\bin\ng”%*
使用npm start,你可以自己执行,ng serve只适用于angular-cli
请参见:运行ng serve时会发生什么?
如果你想运行从另一台机器移植过来的angular应用,而不需要使用ng命令 然后编辑包。Json格式如下
"scripts": {
"ng": "ng",
"start": "node node_modules/.bin/ng serve",
"build": "node node_modules/.bin/ng build",
"test": "node node_modules/.bin/ng test",
"lint": "node node_modules/.bin/ng lint",
"e2e": "node node_modules/.bin/ng e2e"
}
最后运行常用的npm start命令启动构建服务器。
NPM start将运行你在包中为scripts对象的start命令定义的任何命令。json文件。
如果它看起来像这样:
"scripts": {
"start": "ng serve"
}
然后npm start将运行ng serve。