davideicardi / live-plugin-manager

Plugin manager and installer for Node.JS
MIT License
225 stars 43 forks source link

Plugin manager require gives module not found but passing the path directly works #55

Closed tyler-dunkel closed 2 years ago

tyler-dunkel commented 2 years ago

Hi,

I am trying to use live-plugin-manager in my cli tool and am having a couple issues.

  1. running pluginManager.install installs the package but does not install its dependencies.
  2. running pluginManager.require gives a "module not found" error even if I cd to the module directory and install the dependencies manually.

error text:

Error: Cannot find module 'src/plugins/@myNamespace/myModule/dist/services/index.js'

I am able to successfully require the package using either await import('${pathToModuleInPluginDir') or require('$pathToModuleInPluginDir' after manually installing dependencies but running pluginManager.require('moduleName') fails with no found.

My setup is fairly simple, here is how I am initializing the plugin manager:

this.pluginManager = new PluginManager({
      pluginsPath: './src/plugins',
      // hostRequire: undefined,
    })

I tried the suggestion from another issue of using hostRequire: undefined but had the same result.

Here is the code requiring the plugin:

const installOra = ora(`Installing ${providerName} plugin`).start()
        await this.pluginManager.install(importPath)
        installOra.succeed(`${providerName} plugin installed successfully!`)
        // plugin = client
        // plugin = require('../plugins/@cloudgraph/cg-provider-aws/dist/services/index.js')
        plugin = this.pluginManager.require(importPath)

Note importPath === something like @myNamespace/myModule Any ideas?

Thanks!

tyler-dunkel commented 2 years ago

I have isolated the issue down to this line

this.manager.options.requireCoreModules
                && require.resolve(requiredName) === requiredName;

within pluginVM.js function isCoreModule with requiredName === src/plugins/@myNamespace/myModule/dist/services/index.js

Still investigating further.

Note: I updated the install and require commands to be pluginManager.install('moment') and pluginManager.require('moment') and am encountering the same error at the same line.

Error: Cannot find module 'src/plugins/moment/moment.js'

tyler-dunkel commented 2 years ago

I have isolated further and believe it has something to do with the pluginsPath property of the PluginManager constructor. I have a small file that I just run node index.js on. This works:

import {PluginManager} from "live-plugin-manager";

async function test() {
  const pluginManager = new PluginManager({
  })
  await pluginManager.install('moment')
  const moment = pluginManager.require('moment')

  console.log(moment)
}

test()

This produces an error

import {PluginManager} from "live-plugin-manager";

async function test() {
  const pluginManager = new PluginManager({
    pluginsPath: './plugins'
  })
  await pluginManager.install('moment')
  const moment = pluginManager.require('moment')

  console.log(moment)
}

test()
davideicardi commented 2 years ago

If I remember well pluginsPath should be an absolute path. Sorry in fact documentation is not clear... Can you try to put an absolute path?

tyler-dunkel commented 2 years ago

yes this worked thank you. Just having issues with certain packages now.