简体   繁体   English

Node.js包启动文件不起作用

[英]Node.js package start file not working

so I have my package.json file like this: 所以我有这样的package.json文件:

{
  "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: 这是我的index.js文件:

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. 当我键入: node start它不起作用。 Why is this? 为什么是这样? Much help is appreciated. 非常感谢您的帮助。

Thanks 谢谢

The scripts in your package.json should read: package.json中的脚本应显示为:

"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. 使用npm脚本使您可以灵活地链接命令和使用构建工具。

You can read more about this here. 您可以在此处了解更多信息。

Example

Install nodemon, a tool that automatically restarts your node application when you make changes. 安装nodemon,该工具可在您进行更改时自动重新启动节点应用程序。

Just use nodemon instead of node to run your code, and now your process will automatically restart when your code changes. 只需使用nodemon而不是node来运行代码,现在,当代码更改时,您的进程将自动重新启动。 ...from your terminal run: npm install -g nodemon ...从终端运行: npm install -g nodemon

Now in your package.json add the following script: 现在在您的package.json中添加以下脚本:

"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. 这应该使您对npm脚本的工作原理有基本的了解。

The docs for nodemon are here if you are interested. 如果您有兴趣,可以在这里找到nodemon的文档。

the scripts simply run the command you write in them in the shell/terminal. 这些脚本只需运行您在shell /终端中编写的命令即可。 so if you want to run npm start and have node running index.js you need to write 因此,如果要运行npm start并让节点运行index.js ,则需要编写

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM