question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error when using require to load an ES6 module

See original GitHub issue

Hi,

First off great NPM package by the way as well as your DI solution truly excellent work. Second, When I try to use the manager.require() method with my ES6 module I get the following error: Cannot use import statement outside a module If I install the package directly and use it with an import statement it works. I have check my package JSON sets the type to “module” which it does but I still get the error. Is this intentional that import is not supported or is this a bug. Or am I doing something wrong here? Sample code block of my import attempt below

async installCustomPluginLibrary (customPackage, version) {
    await this._manager.installFromNpm(customPackage, version)
    try {
      const newPluginLib = this._manager.require(customPackage)
      const plugins = newPluginLib.libraryPlugins
      const installed = []
      for (let i = 0; i < plugins.length; i++) {
        const plugin = {
          parentPackage: customPackage,
          parentPackageVersion: version,
          key: plugins[i].pluginKey(),
          name: plugins[i].pluginName(),
          description: plugins[i].pluginDescription(),
          params: plugins[i].parameters
        }
        console.log('starting creation of plugin ' + plugins[i].pluginKey())
        console.log(plugin)
        moduleService.CreateModulePlugin(plugin)
        console.log('Plugin ' + plugins[i].pluginKey() + ' created in plugin store')

        installed.push(plugin)
      }
      return installed
    } catch (error) {
      console.log(error)
      throw error
    }
  }

The install works but the line const newPluginLib = this._manager.require(customPackage) fails with the error above. I am going to try changing my files from .js to .mjs to see if that works and will report back. I would appreciate some info though as to whether this is supposed to work or not.

Update: I tried changing my ES6 module file types to .mjs and the live-plugin-manager failed with the following error: Invalid javascript file

Thanks!,

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
davideicardicommented, Nov 4, 2021

I have done some investigation, and I can confirm that it should be possible to support ECMAScript Modules. We should essentially use the vm.Module class. See doc here. But consider that as of today it is still marked as Experimental.

I think that this will be a major release with no backward compatibility (or at least it can be difficult to be backward compatible…).

Here the steps that I think we should do to implement it:

  1. Implement live-plugin-manager as a module (this is required to allow to use live-plugin-manager as a module, but not stricly related to the ability to load modules)
  2. Review PluginVm class to use the new vm.Module class (this is where we need to change most of the actual code). Essentially instead of using a vm.Script we should use a vm.Module … with all the required changes.
  3. replace the current PluginManager.require with a PluginManager.importModule function that return a Promise (marked as async) and call the new PluginVm functions.
1reaction
b3nabcommented, Oct 23, 2021

Ciao @davideicardi, I quote all words from @mackie1311 you have done really a good job.

I’m planning to use your package in some way to orchestate an extension manager for b3nab/deckpad. Probably you need to be aware that ESM now is getting traction and can be used without experimental flags at minimum Node.js 12.20, 14.14, or 16.0. To understand more about the “Pure ESM” you can take a look at this discussion https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c.

If you want I could contribute to this package with a PR if I will find out a way. 😄

To help continue the discussion reagarding an improvement on live-plugin-manager interoperability with cjs and esm I will leave some interesting articles and links: https://blog.logrocket.com/how-to-use-ecmascript-modules-with-node-js/ https://redfin.engineering/node-modules-at-war-why-commonjs-and-es-modules-cant-get-along-9617135eeca1 https://nodejs.org/api/esm.html https://nodejs.org/api/esm.html#interoperability-with-commonjs

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error [ERR_REQUIRE_ESM]: require() of ES Module not ...
The node-fetch latest version doesn't use the require() syntax to import the package. You need to go to your package.json and type
Read more >
error require esm - You.com | The AI Search Engine You Control
Error [ERR_REQUIRE_ESM]: How to use es6 modules in node 12? ... In your code, var pg = require('pg'); loads the pg module, a...
Read more >
Error thrown when due to failure to use import() for es6 ...
The main index.js file is successfully imported as an es6 module. [REQUIRED] Actual behavior. This error is thrown when attempting to deploy:.
Read more >
Using ES modules with CommonJS modules in Node.js
You cannot use require to load an ES module. Error [ERR_REQUIRE_ESM]: Must use import to load ES Module. So, what does work? An...
Read more >
import - JavaScript - MDN Web Docs - Mozilla
In order to use the import declaration in a source file, the file must be interpreted by the runtime as a module. In...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found