Shaobin Jiang
Shaobin Jiang

Reputation: 310

__dirname ceases to work when using pkg to pack my NodeJs apps

I applied the code underneath in my NodeJs application:

console.log(__dirname);

I wish to pack the application using pkg. However, where normally the path to the .js file would have been printed (let's assume it is "F:\Files\apps\my_dir", I now only get a "F:\snapshot\my_dir" (the middle part has been replaced by "snapshot". How can I fix that?

Upvotes: 3

Views: 1411

Answers (1)

Shaobin Jiang
Shaobin Jiang

Reputation: 310

I finally worked out the problem. We should not be using __dirname. Instead, we should do it like this:

const process = require('process');
console.log(process.cwd())

Upvotes: 2

Related Questions