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.

"Module not found:Error: Can't resolve ... Parsed request is a module" for `electron` in `got`

See original GitHub issue

Do you want to request a feature or report a bug?

Report a bug.

What is the current behavior?

I have an Ionic project that uses the (node) package trakt.tv that uses the package got. Ionic builds projects with @ionic/app-scripts which uses webpack. Webpack unfortunately doesn’t like got require()ing electron in the code but not having it as a dependency:

PS C:\Users\Jan\Documents\yatsa> npm run ionic:build

> yatsa@0.0.1 ionic:build C:\Users\Jan\Documents\yatsa
> ionic-app-scripts build

[11:54:01]  ionic-app-scripts 2.0.2
[11:54:01]  build dev started ...
[...]
[11:54:06]  webpack started ...
[11:54:06]  copy finished in 4.72 s
[11:54:18]  ionic-app-script task: "build"
[...]
Error: ./node_modules/got/index.js
Module not found: Error: Can't resolve 'electron' in 'C:\Users\Jan\Documents\yatsa\node_modules\got'
resolve 'electron' in 'C:\Users\Jan\Documents\yatsa\node_modules\got'
  Parsed request is a module
  using description file: C:\Users\Jan\Documents\yatsa\node_modules\got\package.json (relative path: .)
  after using description file: C:\Users\Jan\Documents\yatsa\node_modules\got\package.json (relative path: .)
    resolve as module
      looking for modules in C:\Users\Jan\Documents\yatsa\node_modules
        using description file: C:\Users\Jan\Documents\yatsa\package.json (relative path: ./node_modules)
          Field 'browser' doesn't contain a valid alias configuration
        after using description file: C:\Users\Jan\Documents\yatsa\package.json (relative path: ./node_modules)
          using description file: C:\Users\Jan\Documents\yatsa\package.json (relative path: ./node_modules/electron)
            no extension
              Field 'browser' doesn't contain a valid alias configuration
              C:\Users\Jan\Documents\yatsa\node_modules\electron doesn't exist
            .ts
              Field 'browser' doesn't contain a valid alias configuration
              C:\Users\Jan\Documents\yatsa\node_modules\electron.ts doesn't exist
            .js
              Field 'browser' doesn't contain a valid alias configuration
              C:\Users\Jan\Documents\yatsa\node_modules\electron.js doesn't exist
            .json
              Field 'browser' doesn't contain a valid alias configuration
              C:\Users\Jan\Documents\yatsa\node_modules\electron.json doesn't exist
            as directory
              C:\Users\Jan\Documents\yatsa\node_modules\electron doesn't exist
[C:\Users\Jan\Documents\yatsa\node_modules\electron]
[C:\Users\Jan\Documents\yatsa\node_modules\electron.ts]
[C:\Users\Jan\Documents\yatsa\node_modules\electron.js]
[C:\Users\Jan\Documents\yatsa\node_modules\electron.json]
[C:\Users\Jan\Documents\yatsa\node_modules\electron]
 @ ./node_modules/got/index.js 45:20-39
 @ ./node_modules/trakt.tv/trakt.js
 @ ./src/services/trakt.service.ts
 @ ./src/app/app.module.ts
 @ ./src/app/main.ts
    at new BuildError (C:\Users\Jan\Documents\yatsa\node_modules\@ionic\app-scripts\dist\util\errors.js:16:28)
    at callback (C:\Users\Jan\Documents\yatsa\node_modules\@ionic\app-scripts\dist\webpack.js:119:28)
    at emitRecords.err (C:\Users\Jan\Documents\yatsa\node_modules\@ionic\app-scripts\node_modules\webpack\lib\Compiler.j
s:263:13)
    at Compiler.emitRecords (C:\Users\Jan\Documents\yatsa\node_modules\@ionic\app-scripts\node_modules\webpack\lib\Compi
ler.js:369:38)
    at emitAssets.err (C:\Users\Jan\Documents\yatsa\node_modules\@ionic\app-scripts\node_modules\webpack\lib\Compiler.js
:256:10)
    at applyPluginsAsyncSeries1.err (C:\Users\Jan\Documents\yatsa\node_modules\@ionic\app-scripts\node_modules\webpack\l
ib\Compiler.js:362:12)
    at next (C:\Users\Jan\Documents\yatsa\node_modules\tapable\lib\Tapable.js:154:11)
    at Compiler.compiler.plugin (C:\Users\Jan\Documents\yatsa\node_modules\@ionic\app-scripts\node_modules\webpack\lib\p
erformance\SizeLimitsPlugin.js:99:4)
    at Compiler.applyPluginsAsyncSeries1 (C:\Users\Jan\Documents\yatsa\node_modules\tapable\lib\Tapable.js:158:13)
    at Compiler.afterEmit (C:\Users\Jan\Documents\yatsa\node_modules\@ionic\app-scripts\node_modules\webpack\lib\Compile
r.js:359:9)
[...]

Here the relevant part (I think) again:

Module not found: Error: Can't resolve 'electron' in 'C:\Users\Jan\Documents\yatsa\node_modules\got'
resolve 'electron' in 'C:\Users\Jan\Documents\yatsa\node_modules\got'
  Parsed request is a module

Now got doesn’t have electron in package.json, that is correct.

In normal execution it doesn’t even use electron, and if I understood that correctly it only has some special config option that includes and uses electron (which then is to be though to be installed by the using project or globally):

https://github.com/sindresorhus/got#useelectronnet https://github.com/sindresorhus/got/blob/c0c6bcf1acb7132356d5fdddcf757466071799f4/index.js#L44-L46

As I don’t use electron and don’t have useElectronNetset, this shouldn’t be relevant to me. Webpack should not try to load electron and crash when it can’t find it.

If the current behavior is a bug, please provide the steps to reproduce.

  1. ionic start electrontest blank
  2. cd .\electrontest\
  3. npm install trakt.tv
  4. Add import * as Trakt from 'trakt.tv'; to home.ts
  5. Add private trakt = new Trakt(); directly after export class HomePage { in home.ts
  6. npm run ionic:build

Workaround to see that present electron solves the problem: npm install electron --save-dev

What is the expected behavior?

No crash, as electron is not used in the code.

Please mention other relevant information such as the browser version, Node.js version, webpack version and Operating System.

trakt.tv@5.1.0 got@7.1.0 webpack@3.2.0

Relevant parts of ionic info:

    @ionic/cli-utils : 1.5.0
    Ionic CLI        : 3.5.0

    @ionic/app-scripts              : 2.0.2
    @ionic/cli-plugin-cordova       : 1.4.1
    @ionic/cli-plugin-ionic-angular : 1.3.2
    Ionic Framework                 : ionic-angular 3.5.0

    Node       : v8.1.3
    OS         : Windows 10
    npm        : 5.1.0

Issue Analytics

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

github_iconTop GitHub Comments

4reactions
sokracommented, Jul 16, 2017

Sadly neither the CommonJS nor the ESM spec has “optional” requires/imports.

For the browser builds you can specify:

"browser": {
  "electron": false
}

in package.json. webpack will ignore the electron dependency in this case.

3reactions
sokracommented, Jul 17, 2017

Sorry of the confusion. @janpio At first I assumed you are using electron and wondered why the electron require doesn’t work, but this doesn’t seem to be the case. You ain’t using electron, but one of your dependencies (optionally) depend on it. You can fix this by adding the IgnorePlugin to your configuration and ignoring the electron dependency.

The second comment was addressed to @sindresorhus (who is the package author). The package author can also ignore dependencies in browser build by adding the "browser" field to the package.json. https://github.com/defunctzombie/package-browser-field-spec#ignore-a-module

Read more comments on GitHub >

github_iconTop Results From Across the Web

Module not found: Error: Can't resolve - Parsed request is a ...
Parsed request is a module using description file: C:\Users\Computer\Desktop\shop\package.json (relative paist or is not a directory.
Read more >
module not found error can't resolve 'fs' in react js - You.com
The "Module not found: Can't resolve 'fs'" error in React is caused by a missing or incorrect dependency. To solve this problem, you...
Read more >
module not found: Error: Can't resolve 'fs' : r/electronjs - Reddit
I'm getting this error by running `electron-forge make`: ERROR in ./node_modules/electron/index.js 1:11-24 Module not found: Error: Can't ...
Read more >
Configuring Jest
Jest will run .mjs and .js files with nearest package.json 's type field set to module as ECMAScript Modules.
Read more >
cannot find module [Node npm Error Solved] - freeCodeCamp
When you get the “cannot find module” error, or “module not found”, it means you've not installed the package you're trying to use....
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