-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
get current working directory via global.__dirname #264
Comments
preferably via global.__dirname also see the discussions: |
What's wrong with a good old |
@Mithgol thanks for the hint :) We'd like to also support global.__dirname |
I hope you are planning a getter that would call Some other solution (like actually updating |
I see. Could you please reply in the mailing list as well? So the OP would know about this tip. |
I do not have an active Google Groups account, sorry. You could give the original poster a hyperlink to this issue on GitHub. Alternatively, you may point him to http://nodejs.org/docs/latest/api/process.html#process_process_cwd because he may find |
At the very least, this should be documented, as it's a departure from Node's API. For now, I suppose you could do global.__dirname = process.execPath.substr(0,process.execPath.lastIndexOf('/')); |
Thank you for explaining where I was mistaken. |
According with the Node.js behaviour, Furthermore, parse I think, it's an implementation bug, like if the module loader doesn't share these vars. My solution, waiting an upgrade, #1643. ;) |
Not supporting __dirname is pretty bad for node.js applications. Any update on this? Is there an overview about what node-webkit supports in terms of node.js features? __dirname seems pretty basic. Most applications use it to specify e.g. view-folders (express). |
|
I see, thanks for the clarification. As a related note, when I do a "console.log(__dirname)" from within my nodejs script, is there any way to see it somewhere in nodejs-webkit console? |
I agree should be closed. |
I agree as well. |
@Lcfvs , what did you mean in "process.execPath seems to be a bad idea, because your app is targeted by the package.json but may be everywhere". |
Per my last comment, I realized what you meant. When you run node (while supplying it some js for example), process.execPath returns the location of the node.exe, regardless of the location of the app. However in my case, when distributing my nwjs app, the app is bundled together with the nw.exe, and so process.execPath returns the desired result. |
For those looking for the directory of the index file name and not the nw executable, use:
|
Note that if your app is organized into subdirectories, changes the cwd, or uses a different location for the nw executable, the directory of the currently running WebKit script may not correspond with the above suggestions (your app's main entry point .html file, I suggest using: var __dirname = path.dirname(document.currentScript.src.slice(7)); or if you don't feel like doing a var __dirname = document.currentScript.src.slice(7, document.currentScript.src.lastIndexOf('/')); The content of |
Sorry, I missed the solution for this issue. How could I get current working directory? |
@drom to get the current working directory when the app was started before NW.js changes to the directory containing the app, on Linux & OS X |
@polpo I can't find any sign of |
Note that starting with nw13, global.__dirname now contains the proper dirname of the current .js file, and my comment above from Nov 4, 2015 no longer applies. |
@polpo That's incorrect.
In Node.js, NW.js should bind the variable dynamically in the context of a script when it's evaluated. |
FWIW (and I'm not using nw.js at the moment), here's my own polyfill to get the script's directory: Assumes const baseURL = typeof __dirname !== 'undefined'
? __dirname
: (() => {
const src = (document.currentScript && // May not be present if running from say console
document.currentScript.src
) || location.href;
return src.replace(/\/[^/]+\/?$/, '');
})(); Also, FWIW, there is a JavaScript proposal (currently at stage 3) to get this from within ES6 modules; see http://2ality.com/2017/11/import-meta.html . |
Happening problems with __dirname here: nodejs/node#18267 |
Want to back this issue? Place a bounty on it! We accept bounties via Bountysource.
The text was updated successfully, but these errors were encountered: