Node.js包启动文件不工作

Node.js package start file not working

本文关键字:文件 工作 启动 js 包启动 Node      更新时间:2023-09-26

所以我有我的包。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时,它不起作用。为什么会这样?非常感谢你的帮助。

谢谢

包中的脚本。Json应该是:

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

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

npm test

npm start

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

你可以在这里阅读更多。

安装nodemon,这个工具可以在你做更改时自动重启你的节点应用程序。

只要使用nodemon而不是node来运行你的代码,现在你的进程会在你的代码改变时自动重启。从终端运行:npm install -g nodemon

现在在您的包中。添加以下脚本:

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

在你的命令行中运行这个命令:

npm run dev

这应该能让你对npm脚本的工作原理有一个基本的了解。

nodemon的文档在这里,如果你感兴趣的话

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

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