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.

Can't import .mjs modules in the main process

See original GitHub issue

Describe the bug Some libraries include Ecmascript Modules as .mjs files, it is impossible to import them.

./node_modules/node-fetch/lib/index.mjs 496:11-22
Can\'t import the named export \'PassThrough\' from non EcmaScript module (only default export is available) at HarmonyImportSpecifierDependency._getErrors 
(/Users/totominc/Desktop/Git/poe-mapper-assistant/node_modules/webpack/lib/dependencies/HarmonyImportSpecifierDependency.js:90:6)
at HarmonyImportSpecifierDependency.getErrors (/Users/totominc/Desktop/Git/poe-mapper-assistant/node_modules/webpack/lib/dependencies/HarmonyImportSpecifierDependency.js:68:16)
at Compilation.reportDependencyErrorsAndWarnings (/Users/totominc/Desktop/Git/poe-mapper-assistant/node_modules/webpack/lib/Compilation.js:1334:22)
at Compilation.finish (/Users/totominc/Desktop/Git/poe-mapper-assistant/node_modules/webpack/lib/Compilation.js:1136:9)
at hooks.make.callAsync.err (/Users/totominc/Desktop/Git/poe-mapper-assistant/node_modules/webpack/lib/Compiler.js:545:17)
at _err0 (eval at create (/Users/totominc/Desktop/Git/poe-mapper-assistant/node_modules/tapable/lib/HookCodeFactory.js:24:12), <anonymous>:11:1)
at _addModuleChain (/Users/totominc/Desktop/Git/poe-mapper-assistant/node_modules/webpack/lib/Compilation.js:1064:12)
at processModuleDependencies.err (/Users/totominc/Desktop/Git/poe-mapper-assistant/node_modules/webpack/lib/Compilation.js:980:9)
at process._tickCallback (internal/process/next_tick.js:61:11)
@ ./src/background.ts\n @ multi ./src/background.ts

To Reproduce

  1. yarn add node-fetch && yarn add @types/node-fetch -D
import fetch from 'node-fetch';

fetch('https://google.fr')
	.then(response => console.log(response))
	.then(body => console.log(body));
  1. yarn serve:electron

Expected behavior It should import node-fetch successfully, same for all .ejs modules.

Environment (please complete the following information):

  • OS and version: Mac OS X 10.13.6
  • Node version: 10.8.0
  • npm version: 6.2.0
  • yarn version (if used): 1.9.4
  • vue-cli-plugin-electron-builder version: latest
  • other vue plugins used: typescript
  • custom config for vcp-electron-builder: none
  • (if possible) link to your repo: repo private, sorry

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
TotomInccommented, Sep 3, 2018

The issue seems to be a lot more complex and not anymore related to .mjs modules, setting the following config is working fine:

// vue.config.js
module.exports = {
  pluginOptions: {
    electronBuilder: {
      builderOptions: {
        chainWebpackMainProcess: (config) => {
          config.module.rule('javascript/auto').test(/\.mjs$/);
        },
      }
    }
  }
}

The issue I have now is kinda related to node-fetch or webpack, it is exactly the same problem as here.

I’m closing the issue since .mjs files are successfully imported, and I will look to use another HTTP fetch module.

0reactions
nklaymancommented, Sep 3, 2018

No, I set the externals manually. It is much faster to bundle with webpack so I only externalize native deps. I can’t always check if a dep is native so you may have to list it manually.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use MJS files in Node.js? - DEV Community ‍ ‍
Learn how to import and export from MJS files in Node.js applications. ... SyntaxError: Cannot use import statement outside a module ...
Read more >
import stuff from mjs file into js file OR how to use require in ...
Write a little wrapper to redefine require in ESM code. Use import() (dynamic import) in your CJS to import ESM code.
Read more >
module-not-found - Next.js
A module not found error can occur for many different reasons: The module you're trying to import is not installed in your dependencies;...
Read more >
Module Methods - webpack
The ES2015 Loader spec defines import() as method to load ES2015 modules dynamically on runtime. if (module.hot) { import('lodash ...
Read more >
Modules • JavaScript for impatient programmers (ES2022 ...
Entities that are not exported are private to a module and can't be accessed from outside. 27.6.2 Named imports #. Module main.mjs has...
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