【问题标题】:Node.js package start file not workingNode.js 包启动文件不起作用
【发布时间】:2016-08-13 09:47:06
【问题描述】:

所以我的 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"
  }
}

这是我的 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');
})

当我输入:node start 时,它不起作用。为什么是这样?非常感谢您的帮助。

谢谢

【问题讨论】:

  • package json 应该是 "start" : "node index"

标签: javascript node.js


【解决方案1】:

你的 package.json 中的脚本应该是:

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

要运行这些脚本,请使用命令

npm test

npm start

使用 npm 脚本可以让您灵活地链接命令和使用构建工具。

You can read more about this here.

示例

安装 nodemon,这是一个在您进行更改时自动重新启动节点应用程序的工具。

只需使用 nodemon 而不是 node 来运行您的代码,现在您的进程将在您的代码更改时自动重新启动。 ...从您的终端运行:npm install -g nodemon

现在在你的 package.json 中添加以下脚本:

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

然后从你的命令行运行这个命令:

npm run dev

这应该让您对 npm 脚本的工作方式有一个基本的了解。

The docs for nodemon are here if you are interested.

【讨论】:

  • 感谢展示nodemon,它看起来很有用!我可以通过说“dev”:“nodemon index.js gulp”同时启动 gulp 和 node 吗?还是我需要写点别的,谢谢!
  • 如果您在这里查看,您应该会找到一种有效的模式。 stackoverflow.com/questions/28029929/…
  • 我还意识到我可以同时运行两个终端窗口,从而在不同的终端上同时运行不同的任务。感谢大家的帮助!
【解决方案2】:

脚本只需在 shell/终端中运行您在其中编写的命令。所以如果你想运行npm start 并让节点运行index.js 你需要写

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

【讨论】:

    猜你喜欢
    • 2013-09-07
    • 2017-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-16
    • 1970-01-01
    相关资源
    最近更新 更多