Uncaught EvalError при использовании NPM live-server в приложении Electron - PullRequest
0 голосов
/ 12 февраля 2020

Я пытаюсь создать электронное приложение, которое открывает документ пользователя в браузере по умолчанию. Я хотел бы открыть документ с пакетом live-server, чтобы пользовательский сайт автоматически обновлялся при внесении изменений.

Это код, который я использую:

У меня есть кнопка в моем index.html:

<button onclick="liveserve()">Click me</button>

И мой JS находится между двумя <script> тегами в моем index.html, как показано ниже:

<script>
 function liveserve() {
   console.log('working')
   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: "C:\\Users\\alexr\\Desktop\\index.html", // Set root directory that's being served. Defaults to cwd.
      open: true, // 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 (server root relative) 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);

 }
<script>

Когда я нажимаю на кнопку, работа записывается на консоль, но я получаю сообщение об ошибке:

C:\Users\alexr\node_modules\depd\index.js:413 Uncaught EvalError: Refused to evaluate a string as JavaScript because 'unsafe-eval' is not an allowed source of script in the following Content Security Policy directive: "script-src 'self' 'unsafe-inline'".

    at Function.wrapfunction [as function] (C:\Users\alexr\node_modules\depd\index.js:413)
    at populateConstructorExports (C:\Users\alexr\node_modules\serve-index\node_modules\http-errors\index.js:247)
    at Object.<anonymous> (C:\Users\alexr\node_modules\serve-index\node_modules\http-errors\index.js:29)
    at Object.<anonymous> (C:\Users\alexr\node_modules\serve-index\node_modules\http-errors\index.js:262)
    at Module._compile (internal/modules/cjs/loader.js:968)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:986)
    at Module.load (internal/modules/cjs/loader.js:816)
    at Module._load (internal/modules/cjs/loader.js:728)
    at Module._load (electron/js2c/asar.js:717)
    at Function.Module._load (electron/js2c/asar.js:717)

Есть ли способ, которым я могу это исправить? Или я использую пакет неправильно, и если да, то как я могу его использовать или есть альтернатива?

...