0

I have a node.js/angular2 project which I had previously been serving using lite-server but as I need to serve server-side files, I am switching to using http-server. To run lite server and the typescript compiler simultaneously before, I had the command: "start": "tsc && concurrently \"tsc -w\" \"lite-server\" ", in my package.json. I initially switched it to "start": "tsc && concurrently \"tsc -w\" \"http-server\" ", which I found gave the error:

[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

After having seen this SO post, I tried: tsc && concurrently "tsc -w" "./node_modules/http-server/bin" which returned the same error. What is the correct command to run http-server?

UPDATE:

I modified my package.json as was suggested:

{
  "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": {}
}

I ran the command npm install http-server --save in the project root and got the following errors:

+-- @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.

These packages were installed initially so what could be causing these errors?

Community
  • 1
  • 1
loremIpsum1771
  • 2,497
  • 5
  • 40
  • 87

1 Answers1

2

The corrent command is http-server, but you need to make sure you have http-server installed properly.

If you used npm install -g http-server, you just use http-server, anywhere. CLI or package.json scripts, doesn't matter, it just works.

If you used npm install http-server --save or save-dev etc, then you still just use http-server, but as part of a package.json script. It will then only work through npm scripts, because npm does all the environment bootstrapping for you. Don't try to guess at the http-server bin location.

For bonus points, capture all the different things as their own npm tasks, and then use npm task composition or something like 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 Edit

Alternatively, if you don't want global packages, you can also use npx, as "the only thing you ever install globally" and then you can run local modules using npx modulename, in this case, npx http-server.

Mike 'Pomax' Kamermans
  • 49,297
  • 16
  • 112
  • 153