Tino Caer
Tino Caer

Reputation: 463

Node.js package start file not working

so I have my package.json file like this:

{
  "name": "chat_app",
  "version": "0.2.0",
  "description": "The second iteration of the chat app",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "index.js"
  },
  "author": "Tino",
  "license": "MIT",
  "dependencies": {
    "express": "^4.14.0",
    "jade": "^1.11.0"
  },
  "devDependencies": {
    "gulp": "^3.9.1",
    "gulp-jade": "^1.1.0"
  }
}

Here is my index.js file:

var app = require('express')(),
express = require('express'),
http = require('http').Server(app),
jade = require('gulp-jade');

app.use(express.static(path.join(__dirname, '/public')));
app.get('/', function(req, res) {
    res.sendfile(__dirname + '/index.html');
})

http.listen(3000, function() {
    console.log('listening on localhost:3000');
})

When I type: node start it does not work. Why is this? Much help is appreciated.

Thanks

Upvotes: 0

Views: 1504

Answers (2)

Bamieh
Bamieh

Reputation: 10906

the scripts simply run the command you write in them in the shell/terminal. so if you want to run npm start and have node running index.js you need to write

"scripts": {
  "start": "node index.js"
}

Upvotes: 1

alex
alex

Reputation: 5573

The scripts in your package.json should read:

"scripts": {
  "test": "echo \"Error: no test specified\" && exit 1",
  "start": "node index.js"
},

To run these scripts use the commands

npm test

or

npm start

Using npm scripts gives you the flexibility to chain commands and use build tools.

You can read more about this here.

Example

Install nodemon, a tool that automatically restarts your node application when you make changes.

Just use nodemon instead of node to run your code, and now your process will automatically restart when your code changes. ...from your terminal run: npm install -g nodemon

Now in your package.json add the following script:

"scripts": {
  "test": "echo \"Error: no test specified\" && exit 1",
  "start": "node index.js",
  "dev": "nodemon index.js"
},

And from your command line run this command:

npm run dev

This should give you a basic understanding of how npm scripts work.

The docs for nodemon are here if you are interested.

Upvotes: 3

Related Questions