nodejs + electron 如何导入某文件夹下的全部模块?
发布于 4 年前 作者 ZeronoFreya 4711 次浏览 来自 问答

开发平台: win10 个人使用,不打算发布到npm 我想实现的是:设置Extensions文件夹,里面存放扩展,electron运行时自动加载Extensions里的所有扩展 比如 Extensions/test.js

// test.js
module.exports = (ctx) => {
  const register = () => {
    console.log(ctx);
  }
  return {
    register
  }
}

Extensions文件夹在程序根目录

我的实现(伪代码):

import { app } from 'electron'
import path from 'path'
const pluginPath = path.join( path.dirname(app.getPath('exe')), 'Extensions')

getPlugin(path) {
    return require(path)('233')
}

getPlugin(pluginPath)

运行后报错:

Error: Cannot find module 'E:\Project\Electron\MoeEx\node_modules\electron\dist\Extensions\test.js'

请问,我应该怎样实现呢?

谢谢

回到顶部