__dirname paths do not resolve correctly when electron target is packaged

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

What is the current behavior?
file paths that do not resolve correctly

If the current behavior is a bug, please provide the steps to reproduce.
have an es5 module that uses __dirname to reference folders located next to it.
use the node config option

node: {
    __dirname: true
}

run in dev, works
run after packaging, fails

What is the expected behavior?
the filesystem should be the same structure, but it ends up not
verified by setting a breakpoint at the line that would join dirname with another path
found that global.__dirname and __dirname differed. fine, probably caused by the node: __dirname: true. still works in development

require('fs').readdirSync(__dirname); // successful in development

after packaging, the previous line fails but I am able to get the following line to work at the same breakpoint

require('fs').readdirSync(path.join(global.__dirname, '..', __dirname));

to get to the same place

Please mention other relevant information such as the browser version, Node.js version, webpack version and Operating System.
I am on mac osx 10.12.6 (16G29)
tried building with target: electron and electron-renderer
webpack v3.4.1
node v7.10.0

Author: Fantashit

1 thought on “__dirname paths do not resolve correctly when electron target is packaged

  1. For anyone who is having problems with this issue the solution for me was:

    In your package.json you need to add this to your build

     "build": {
        "extraResources": [
          "{PATH TO YOUR FOLDER THAT NEEDS TO RESOLVE ie: app/helpers}"
        ], 
    etc etc etc..

    In your file that you are trying to use __dirname you’ll need to set something like this up

    const appPath =
      process.env.NODE_ENV === 'production'
        ? `${process.resourcesPath}/app`
        : __dirname;

    When you are trying to use join path it will look like this for example (the way I used it)

    const injectorPath = path.join(appPath, 'helpers/inputInjector.js');
    const injectorCodeString = fs.readFileSync(injectorPath, { encoding: 'utf8' });

    This should resolve the path when you package your app and allow you to read the files with FS

Comments are closed.