electron - 如何在 Electron 中使用 require()?

标签 electron require

我第一次使用electronwebchimera。我看到这个 demo ,但我不想在主窗口中打开播放器。我不知道如何使用 Electron ,所以我尝试使用呈现索引页面的服务器,并在单击按钮后播放播放器页面。

客户端从服务器加载索引页面:

//this is for webchimera
if (process.platform == 'win32')
    process.env['VLC_PLUGIN_PATH'] = require('path').join(__dirname, 'node_modules/wcjs-prebuilt/bin/plugins');


const {app, BrowserWindow} = require('electron')
const path = require('path')
const url = require('url')

// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let win

function createWindow () {
  // Create the browser window.
  win = new BrowserWindow({width: 800, height: 600})

  // and load the index.html of the app.
  win.loadURL("http://localhost:8888/")

  // Open the DevTools.
  win.webContents.openDevTools()

  // Emitted when the window is closed.
  win.on('closed', () => {
    // Dereference the window object, usually you would store windows
    // in an array if your app supports multi windows, this is the time
    // when you should delete the corresponding element.
    win = null
  })
}

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow)

// Quit when all windows are closed.
app.on('window-all-closed', () => {
  // On macOS it is common for applications and their menu bar
  // to stay active until the user quits explicitly with Cmd + Q
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

app.on('activate', () => {
  // On macOS it's common to re-create a window in the app when the
  // dock icon is clicked and there are no other windows open.
  if (win === null) {
    createWindow()
  }
})

// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.

索引页面包含一个指向播放器页面的链接,如果单击它,服务器将响应播放器页面。 服务器在nodejs中。

服务器在 localhost:8888 中运行并发送此 html 页面:

<html>
<head>
  <style>
  body,html{ width: 100%; height: 100%; padding: 0px; margin:0px }
  #player { width: 100%; height: 100% }
  </style>
</head>
  
<body>
  <div id="player"></div>
  <script>
  var wjs = require("wcjs-player");
  var player = new wjs("#player").addPlayer({
    autoplay: true,
    wcjs: require('wcjs-prebuilt')
  });
  player.addPlaylist("http://archive.org/download/CartoonClassics/Krazy_Kat_-    _Keeping_Up_With_Krazy.mp4");
  </script>
</body>
</html>

但是我得到这个错误: 找不到模块 wcjs-player

我该如何解决这个问题?

附言: 我使用服务器只是因为我在决定使用 Electron 之前为 webapp 编写了它。没必要。我可以删除服务器并仅使用客户端。

最佳答案

这意味着模块 wcjs-player 没有安装。在你的控制台/终端运行 npm install wcjs-player 来安装它,这一定有帮助,而且你可能想看看 this

关于electron - 如何在 Electron 中使用 require()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41274017/

相关文章:

javascript - 使用node js访问本地网络驱动器

javascript - Nightmare Js 多次评估后评估超时

node.js - 将 QLPreviewPanel 与 Electron 一起使用

jquery - 如何在 Google Chrome 的 Greasemonkey 脚本中使用 jQuery?

php - file_exists() 或 is_readable()

Lua指定库

google-chrome-extension - 是否有从 chrome 扩展获取桌面事件应用程序名称的解决方案?

electron - dyld : Library not loaded:/usr/local/opt/openssl/lib/libcrypto. 1.0.0.dylib

node.js - "require(x)"和 "import x"的区别

php - Eclipse PDT - 文件系统更改或新的 Composer 自动加载路径会破坏自动加载器