LoginSignup
1
0

More than 5 years have passed since last update.

[Electron]process.cwdではなくapp.getPathを使う

Last updated at Posted at 2016-10-10

よく考えるとわかるものの、この間引っかかった(のを書いていなかったので1ヶ月ぐらい経ってから書いている)メモ

現象

Electronでディレクトリのパスを取得する際にprocess.cwd()を利用していると実行環境によって取得できるパスが変わる。

$electron index.js 
// ->//Users/my_account/Documents/electron/test 動かしているディレクトリ

で動かしているときと

$asar pack ./ ./test.asar
$electron test.asar
// ->//Users/my_account/Documents/electron/test 動かしているディレクトリ

のときとパッケージングしたあと

electron-packager ./ test  --platform=darwin,win32 --arch=x64 --version=1.3.3
//アプリを実行
// -> / (ルート直下)

で挙動が違うので注意が必要。

なので、利用する際はprocess.cwd()ではなく、app.getPathを利用する。

サンプルコード

以下のコードで動かしています(index.htmlのみ記載)

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title> Hello electron. </title>
    </head>
    <body>
        <script>
            function done() {
                alert("cwd: " + process.cwd() );
            }
        </script>
        <button onClick="done()" > done </button>
    </body>
</html>

参考

process.cwd() returns '/' #2108
ElectronでOS固有のファイルパスを得る app.getPath()の出力まとめ
app.getPath

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0