尝试运行npm http-server时出错

时间:2016-08-08 20:01:41

标签: javascript node.js http npm server

我有一个node.js / angular2项目,我之前一直使用lite-server服务,但由于我需要提供服务器端文件,我转而使用http-server。为了同时运行lite服务器和typescript编译器,我在package.json中输入了命令:"start": "tsc && concurrently \"tsc -w\" \"lite-server\" ",。我最初将其切换到"start": "tsc && concurrently \"tsc -w\" \"http-server\" ",,我发现错误:

[1] 'http-server' is not recognized as an internal or external command,
1] operable program or batch file.
1] http-server exited with code 1

看过this SO post后,我尝试了:tsc && concurrently "tsc -w" "./node_modules/http-server/bin",它返回了同样的错误。 运行http-server的正确命令是什么?

UPDATE:

我按照建议修改了package.json

{
  "name": "angular2-quickstart",
  "version": "1.0.0",
  "description": "QuickStart package.json from the documentation, supplemented with testing support",
  "scripts": {
    "start": "tsc && concurrently \"tsc -w\" \"node app/index.js\" ",
    "docker-build": "docker build -t ng2-quickstart .",
    "docker": "npm run docker-build && docker run -it --rm -p 3000:3000 -p 3001:3001 ng2-quickstart",
    "pree2e": "npm run webdriver:update",
    "e2e": "tsc && concurrently \"http-server -s\" \"protractor protractor.config.js\" --kill-others --success first",
    "lint": "tslint ./app/**/*.ts -t verbose",
    "lite": "lite-server",
    "http": "http-server",
    "postinstall": "typings install",
    "server": "http-server",
    "test": "tsc && concurrently \"tsc -w\" \"karma start karma.conf.js\"",
    "test-once": "tsc && karma start karma.conf.js --single-run",
    "tsc": "tsc",
    "tsc:w": "tsc -w",
    "typings": "typings",
    "webdriver:update": "webdriver-manager update"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@angular/common": "2.0.0-rc.4",
    "@angular/compiler": "2.0.0-rc.4",
    "@angular/core": "2.0.0-rc.4",
    "@angular/forms": "0.2.0",
    "@angular/http": "2.0.0-rc.4",
    "@angular/platform-browser": "2.0.0-rc.4",
    "@angular/platform-browser-dynamic": "2.0.0-rc.4",
    "@angular/router": "3.0.0-beta.2",
    "@angular/router-deprecated": "2.0.0-rc.2",
    "@angular/upgrade": "2.0.0-rc.4",
    "angular2-in-memory-web-api": "0.0.14",
    "bootstrap": "^3.3.6",
    "cheerio": "latest",
    "core-js": "^2.4.0",
    "express": "latest",
    "file-system": "^2.2.1",
    "http-server": "^0.9.0",
    "reflect-metadata": "^0.1.3",
    "request": "latest",
    "rxjs": "5.0.0-beta.6",
    "systemjs": "0.19.27",
    "zone.js": "^0.6.12"
  },
  "devDependencies": {
    "concurrently": "^2.2.0",
    "lite-server": "^2.2.0",
    "typescript": "^1.8.10",
    "typings": "^1.0.4",
    "canonical-path": "0.0.2",
    "http-server": "latest",
    "tslint": "^3.7.4",
    "lodash": "^4.11.1",
    "jasmine-core": "~2.4.1",
    "karma": "^0.13.22",
    "karma-chrome-launcher": "^0.2.3",
    "karma-cli": "^0.1.2",
    "karma-htmlfile-reporter": "^0.2.2",
    "karma-jasmine": "^0.3.8",
    "protractor": "^3.3.0",
    "rimraf": "^2.5.2"
  },
  "repository": {}
}

我在项目根目录中运行了命令npm install http-server --save并出现以下错误:

+-- @reactivex/rxjs@5.0.0-beta.10  extraneous
+-- UNMET PEER DEPENDENCY reflect-metadata@0.1.3
`-- UNMET PEER DEPENDENCY rxjs@5.0.0-beta.6

npm WARN optional Skipping failed optional dependency /chokidar/fsevents:
npm WARN notsup Not compatible with your operating system or architecture: fsevents@1.0.14
npm WARN angular2@2.0.0-beta.15 requires a peer of reflect-metadata@0.1.2 but none was installed.
npm WARN angular2@2.0.0-beta.15 requires a peer of rxjs@5.0.0-beta.2 but none was installed.

最初安装了这些软件包,这可能导致这些错误?

1 个答案:

答案 0 :(得分:2)

corrent命令是http-server,但您需要确保正确安装了http-server。

如果您使用npm install -g http-server,则只需在任何地方使用http-server。 CLI或package.json脚本没关系,它只是起作用。

如果您使用npm install http-server --save或save-dev等,那么仍然只使用http-server,但作为package.json脚本的一部分。它只能通过npm脚本工作,因为npm会为你完成所有的环境引导。不要试图猜测http-server bin位置。

对于奖励积分,将所有不同的东西捕获为他们自己的npm任务,然后使用npm任务组合或类似npm-run-all

...
"scripts": {
  "server": "http-server",
  "tsc": "tsc",
  "tsc:w": "tsc -w",
  "start": "npm-run-all tsc tsc:w server"
},
...
"dependencies": {
  ...
  "http-server": "...",
  ...
}

2017年编辑

或者,如果您不想要全局包,也可以使用npx作为“您唯一全局安装的”,然后您可以使用npx modulename运行本地模块,在此case,npx http-server