Why is __dirname not defined in node REPL?

后端 未结 11 1363
傲寒
傲寒 2020-12-12 14:23

From the node manual I see that I can get the directory of a file with __dirname, but from the REPL this seems to be undefined. Is this a misunderstanding on my

相关标签:
11条回答
  • 2020-12-12 15:03

    __dirname is only defined in scripts. It's not available in REPL.

    try make a script a.js

    console.log(__dirname);
    

    and run it:

    node a.js
    

    you will see __dirname printed.

    Added background explanation: __dirname means 'The directory of this script'. In REPL, you don't have a script. Hence, __dirname would not have any real meaning.

    0 讨论(0)
  • 2020-12-12 15:10

    If you are using Node.js modules, __dirname and __filename don't exist.

    From the Node.js documentation:

    No require, exports, module.exports, __filename, __dirname

    These CommonJS variables are not available in ES modules.

    require can be imported into an ES module using module.createRequire().

    Equivalents of __filename and __dirname can be created inside of each file via import.meta.url:

    import { fileURLToPath } from 'url';
    import { dirname } from 'path';
    
    const __filename = fileURLToPath(import.meta.url);
    const __dirname = dirname(__filename);
    

    https://nodejs.org/api/esm.html#esm_no_require_exports_module_exports_filename_dirname

    0 讨论(0)
  • 2020-12-12 15:11

    I was running a script from batch file as SYSTEM user and all variables like process.cwd() , path.resolve() and all other methods would give me path to C:\Windows\System32 folder instead of actual path. During experiments I noticed that when an error is thrown the stack contains a true path to the node file.

    Here's a very hacky way to get true path by triggering an error and extracting path from e.stack. Do not use.

    // this should be the name of currently executed file
    const currentFilename = 'index.js';
    
    function veryHackyGetFolder() {
      try {
        throw new Error();
      } catch(e) {
        const fullMsg = e.stack.toString();
        const beginning = fullMsg.indexOf('file:///') + 8;
        const end = fullMsg.indexOf('\/' + currentFilename);
        const dir = fullMsg.substr(beginning, end - beginning).replace(/\//g, '\\');
        return dir;
      }
    }
    

    Usage

    const dir = veryHackyGetFolder();
    
    0 讨论(0)
  • 2020-12-12 15:11

    Though its not the solution to this problem I would like to add it as it may help others.

    You should have two underscores before dirname, not one underscore (__dirname not _dirname).

    NodeJS Docs

    0 讨论(0)
  • 2020-12-12 15:14

    As @qiao said, you can't use __dirname in the node repl. However, if you need need this value in the console, you can use path.resolve() or path.dirname(). Although, path.dirname() will just give you a "." so, probably not that helpful. Be sure to require('path').

    0 讨论(0)
  • 2020-12-12 15:14

    If you got node __dirname not defined with node --experimental-modules, you can do :

    const __dirname = path.dirname(import.meta.url)
                          .replace(/^file:\/\/\//, '') // can be usefull
    

    Because othe example, work only with current/pwd directory not other directory.

    0 讨论(0)
提交回复
热议问题