0

The title says it all. I'm trying to create a Windows exe file using npm pkg (details here). I've succeeded in creating the exe file, but it only runs when located in my project folder. I'd like to be able to run it from anywhere - even other computers. What am I doing wrong?

EDIT: Here is my script. Would using _dirname help? I'm not familiar with that function, so code samples would be appreciated.

var AWS = require('aws-sdk'),
    fs = require('fs');
    const Fs = require('fs')
const path = require('path');
AWS.config.loadFromPath(path.join(__dirname, 'config.json'));
var mysql      = require('mysql');
var connection = mysql.createConnection({
  connectionLimit : 10,
  host     : '...',
  user     : '...',
  password : '...',
  database: '...',
});
console.log(__dirname);

var localfile = path.join(__dirname, 'myverse.mp3')

 connection.query('SELECT COUNT(mp3) as "total" FROM myverses where mp3 = "empty"', function (error, results, fields) {
  console.log('error: ' + error);
  var totalverses = results[0].total

  if (totalverses > 0) {
    console.log('There are ' + results[0].total + ' verses to record.');
    addverse();
      } else {
    console.log("There are NO verses to record.")
    connection.end();
  }

  });

  function addanother() {
    connection.query('SELECT COUNT(mp3) as "total" FROM myverses where mp3 = "empty"', function (error, results, fields) {
   console.log('error: ' + error);
   var totalverses = results[0].total


   if (totalverses > 0) {
     console.log('There are ' + results[0].total + ' verses to record.');
     addverse();
       } else {
     console.log("There are NO verses to record.")
    connection.end();
   }

   });
}


function addverse() {
    connection.query('SELECT versetext, book, mp3, id, reference FROM myverses where mp3 = "empty" limit 1',
    function (error, results, fields) {
    console.log(error);
    var scripture = results[0].versetext;
    var book = results[0].book;
    var reference = results[0].reference.replace(":", " verse ");
    console.log(scripture + " " +  book.replace("1", "first").replace("2", "second").replace("3", "third") + " " + reference);
    var myverse = "<speak><break time='1s'/>" + scripture + " " +  book.replace("1", "first").replace("2", "second").replace("3", "third") + " " + reference + "<break time='1s'/></speak>";
    var link = "https://s3.amazonaws.com/myverses/" + book.replace(/ /g, "")+reference.replace(/ /g, "")+".mp3";

    writeit();
    var myvalue = fs.createReadStream(localfile);
    setTimeout(uploadit, 2000)

function linkit(){
   connection.query('update myverses set mp3 = ? where mp3 = "empty" limit 1', [link],
    function (error, results, fields) {
      console.log(error)

    })

}


function writeit() {
  const Polly = new AWS.Polly({
    signatureVersion: 'v4',
    region: 'us-east-1'
})

let params = {
    'Text': myverse.replace(" Job ", " Jobe "),
    'TextType': 'ssml',
    'OutputFormat': 'mp3',
    'VoiceId': 'Matthew'
}

Polly.synthesizeSpeech(params, (err, data) => {
    if (err) {
        console.log(err.code)
    } else if (data) {
        if (data.AudioStream instanceof Buffer) {
            Fs.writeFile(localfile, data.AudioStream, function(err) {
                if (err) {
                    return console.log(err)
                }
                console.log("Verse recorded successfully!")
            })
        }
    }
})

}

function uploadit () {
  console.log('Preparing to upload the verse.')
  s3 = new AWS.S3({apiVersion: '2006-03-01'});
 var uploadParams = {Bucket: 'myverses', key: '/test.mp3', Body: myvalue, ACL: 'public-read'};
  var file = localfile;

  var fs = require('fs');
  var fileStream = fs.createReadStream(file);
  fileStream.on('error', function(err) {
    console.log('File Error', err);
  });
  uploadParams.Body = fileStream;

  var path = require('path');
  uploadParams.Key = book.replace(/ /g, "")+reference.replace(/ /g, "")+".mp3";

  // call S3 to retrieve upload file to specified bucket
  s3.upload (uploadParams, function (err, data) {
    if (err) {
      console.log("Error", err);
    } if (data) {
      console.log("Upload Success", data.Location);
      linkit();
      addanother();
    }
  });

}

  });
}
6
  • would be helpful if you also give the output/error you are getting when run from somewhere else. Commented Feb 20, 2018 at 17:32
  • No error is displayed at all. Cmd prompt simply flashes on the screen, then disappears. Again, it works great when running from the project folder. Commented Feb 20, 2018 at 17:33
  • Probably depends on what your program is doing. It seems it is relying on something in your project folder. If this is the case, try using __dirname when referencing files inside your project. Commented Feb 20, 2018 at 17:36
  • It's not clear what your problem is but you can check if you have any relative paths within your project code since it works when located in your project folder, again it's just my guess, if you can give some details that maybe can sum up your problem Commented Feb 20, 2018 at 17:38
  • I just tried running it from my desktop. Notably, it worked after I copied my node_modules folder to the desktop as well. It seems the .exe is referencing the dependencies rather than building them into the file. I'm using the aws and mysql modules. How can I make sure these are included in the .exe? Commented Feb 20, 2018 at 17:43

1 Answer 1

1

try:

const path = require('path');
AWS.config.loadFromPath(path.join(__dirname, 'config.json'));

then, if that is not enough, try changing all references of 'myverse.mp3' to:

path.join(__dirname, 'myverse.mp3')

Documentation about __dirname

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. Just one more thing. I added the first snippet of code, as well as the following: var localfile = path.join(__dirname, 'myverse.mp3') Then, I replaced all instances of my .mp3 with localfile. However, I'm getting this error message: Cannot write to packaged file. See revised code above.
Please, share the all the stack of the error, so we can know where this error is coming from.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.