Why does the error 'cannot find module' occur in render process


#1

I’m new to Electron, so I can’t understand what happen about this.

I write a module call configuration.js.

In the main process, the code const configuration = require('../app/js/configuration') doesn’t have error, because I can get the window. But in the render process, the code const configuration = require('./configuration') can cause an error:

    Uncaught Error: Cannot find module './configuration'
        at Module._resolveFilename (module.js:470:15)
        at Function.Module._resolveFilename (F:\Web\wxminpgEditor\node_modules\electron\dist\resources\electron.asar\common\reset-search-paths.js:35:12)
        at Function.Module._load (module.js:418:25)
        at Module.require (module.js:498:17)
        at require (internal/module.js:20:19)
        at file:///F:/Web/wxminpgEditor/app/js/settings.js:6:23

I have found a solution, when I change the code to

    const path = require('path')
    const configuration = require(path.resolve('app/js/configuration.js'))

it can run!

So my question is why in render process I can’t use relative path to find my module and how can I use relative path (because I think the solution is too long). I am not really clear about this. Can you help me? Thanks!

Here is my project folder
@$OSQ3H4UEAW$2VX8JN9Q

main.js is main process, setting.js is render process.


Can't require renderer in http protocol
#2

OK, maybe I have known what heppen. XD


#3

The path of the module is relative to my HTML file. I need to use require('./js/configuration') in settings.js to import the module. Because the settings.js is used in settings.html. This also answers why in main process using path is right. Because the main process is not used by HTML file.