"Module not found:Error: Can't resolve ... Parsed request is a module" for `electron` in `got`
See original GitHub issueDo 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.
ionic start electrontest blank
cd .\electrontest\
npm install trakt.tv
- Add
import * as Trakt from 'trakt.tv';
tohome.ts
- Add
private trakt = new Trakt();
directly afterexport class HomePage {
inhome.ts
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:
- Created 6 years ago
- Reactions:1
- Comments:5 (3 by maintainers)
Sadly neither the CommonJS nor the ESM spec has “optional” requires/imports.
For the browser builds you can specify:
in package.json. webpack will ignore the electron dependency in this case.
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 thepackage.json
. https://github.com/defunctzombie/package-browser-field-spec#ignore-a-module