npm live-server 不会自动重新加载

Abu*_*yer 5 node.js npm package.json

我尝试在实时服务器上工作,安装了一个live-server使用以下命令调用的节点包:npm install -g live-server

它运行良好,安装成功并live-server通过live-server命令运行。

每当我更改代码并保存在代码编辑器中时,浏览器都不会自动刷新。

这是我的package.json文件:

"name": "nodejs",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Abul Khoyer",
  "license": "ISC"
}
Run Code Online (Sandbox Code Playgroud)

Gno*_*pps 9

我遇到了和你一样的问题,并通过确保 .html 文件的格式正确来设法让它工作。即像这样:

<!DOCTYPE html>
<html>
<body>
    <h1>Script tester!</h1>
    <script type="text/javascript" src="script.js"></script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)


Har*_*hal 0

您需要添加此代码:

来自节点的使用

例子:

var liveServer = require("live-server");

var params = {
    port: 8181, // Set the server port. Defaults to 8080.
    host: "0.0.0.0", // Set the address to bind to. Defaults to 0.0.0.0 or process.env.IP.
    root: "/public", // Set root directory that's being served. Defaults to cwd.
    open: false, // When false, it won't load your browser by default.
    ignore: 'scss,my/templates', // comma-separated string for paths to ignore
    file: "index.html", // When set, serve this file for every 404 (useful for single-page applications)
    wait: 1000, // Waits for all changes, before reloading. Defaults to 0 sec.
    mount: [['/components', './node_modules']], // Mount a directory to a route.
    logLevel: 2, // 0 = errors only, 1 = some, 2 = lots
    middleware: [function(req, res, next) { next(); }] // Takes an array of Connect-compatible middleware that are injected into the server middleware stack
};
liveServer.start(params);
Run Code Online (Sandbox Code Playgroud)

或者你可以添加一个文件.live-server.json

如果存在,它将被加载并用作命令行上 live-server 的默认选项。

有关更多详细信息,请参阅:https ://www.npmjs.com/package/live-server