title: Imported module output flushed on exit only
body: <p>I've been trying to build a plugin system for my NodeJS application. So it does not run in a DOM/browser context.</p>
  <p>The code to import the module is:</p>
  <pre><code>const loadAsyncImport = () =&gt; {
    const asyncImport = async () =&gt; {
      return await myDynamicImport(pluginMainPath);
    };
  
    return asyncImport().then((result) =&gt; {
      return result;
    });
  };
  
  const myDynamicImport = async (path) =&gt; {
    return await import(path);
  };
  loadAsyncImport().then(value =&gt; {
    pluginData = value['default'].initializePlugin();
    console.log('pluginData is: ' + pluginData);
  });
  </code></pre>
  <p>Here is the plugin code:</p>
  <pre><code>function initializePlugin() {
    console.log('BEGIN main.bootStrapPlugin function');
    let returnData = 'HELLO WORLD - FROM PluginOne';
    console.log('END main.bootStrapPlugin function');
    return returnData;
  };
  
  export default {
    initializePlugin
  };
  </code></pre>
  <p>This app has a main program loop that accepts input and executes commands. One command is &quot;X&quot; that will exit the CLI interface main program loop and terminate the Node process. I have the code that loads the plugin in the bootStrapper so it executes well before the program loop even begins. However, the output from the plugin doesn't seem to get set until after I exit the application. I've tried multiple (await (await ()) style wrappers but nothing seems to allow for the execution to actually execute in-line with the application when it is actually executed. The rest of my code is synchronous.</p>
  <p>Here is the output:</p>
  <pre><code>BEGIN testHarness.application Function
  
  0.0.1
  A test harness application to test the haystacks framework.
  BEGIN controllers.warden.loadPlugin Function
  pluginPath is: C:/haystacks-plugins/pluginOne/
  resolvedPluginPath is: C:\haystacks-plugins\pluginOne\package.json
  pluginMetaData is: {&quot;name&quot;:&quot;appplugin-one&quot;,&quot;version&quot;:&quot;0.0.1&quot;,&quot;description&quot;:&quot;An example plugin 
  implementation for the haystacks platform.&quot;,&quot;main&quot;:&quot;./src/main.js&quot;,&quot;type&quot;:&quot;module&quot;,&quot;scripts&quot;: 
  {&quot;test&quot;:&quot;echo \&quot;Error: no test specified\&quot; &amp;&amp; exit 1&quot;},&quot;repository&quot;: 
  {&quot;type&quot;:&quot;git&quot;,&quot;url&quot;:&quot;git+https://github.com/SethEden/pluginOne.git&quot;},&quot;keywords&quot;: 
  [&quot;NodeJS&quot;,&quot;Plugin&quot;,&quot;NodeJS-App&quot;],&quot;author&quot;:&quot;Seth Hollingsead&quot;,&quot;license&quot;:&quot;MIT&quot;,&quot;bugs&quot;: 
  {&quot;url&quot;:&quot;https://github.com/SethEden/AppPlugin- 
  One/issues&quot;},&quot;homepage&quot;:&quot;https://github.com/SethEden/AppPlugin-One#readme&quot;}
  pluginMainPath is: ./src/main.js
  pluginMainPath is: C:\haystacks-plugins\pluginOne\src\main.js
  pluginMainPath is: file:///C:/haystacks-plugins/pluginOne/src/main.js
  // -------&gt;&gt;&gt;&gt;&gt;          &lt;---- This is where the output from the plugin loading should be. 
  returnData is: false
  END controllers.warden.loadPlugin Function
  BEGIN main program loop
  BEGIN command parser
  &gt;x                       // &lt;---- User entry to exit the application
  END command parser
  END main program loop
  Exiting TEST HARNESS APPLICATION
  END testHarness.application Function
  BEGIN main.bootStrapPlugin function // &lt;---- Plugin execution begins
  END main.bootStrapPlugin function // &lt;---- Plugin execution ends
  pluginData is: HELLO WORLD - FROM PluginOne // &lt;---- Data from plugin is finally logged
  </code></pre>
  <p>You can see in the output:
  pluginPath, resolvedPluginPath, pluginMetaData and pluginMainPath are all my attempts to load the package.json and extract the main path to the entry-point for the plugin. Then I load that path after resolving it to the URL.</p>
  <p>Is there something with the CMD buffer or is it an order of operations thing? Something with call-stack or a child-process thing? Or is this really another async issue?</p>
  <p>Is it something that can be solved with synchronous?
  I've tried to flush the CMD buffer and I've tried to drain the CMD buffer, and as I've said I've tried creating multiple async await wrappers to force it to resolve.
  What am I missing here?</p>
  <p>Yes I know it is a blocking operation. It's just loading a plugin, I don't expect it to take more than 1 second, even that would be just fine given all of this is happening as a native application.</p>
  <p>EDIT:</p>
  <p>I have mocked up a completely empty project that reproduces the problem this is the package.json:</p>
  <pre><code>{
    &quot;name&quot;: &quot;ultraplugintest&quot;,
    &quot;version&quot;: &quot;1.0.0&quot;,
    &quot;description&quot;: &quot;&quot;,
    &quot;main&quot;: &quot;main.js&quot;,
    &quot;type&quot;: &quot;module&quot;,
    &quot;scripts&quot;: {
      &quot;test&quot;: &quot;node main.js&quot;
    },
    &quot;author&quot;: &quot;Seth Hollingsead&quot;,
    &quot;license&quot;: &quot;MIT&quot;
  }
  </code></pre>
  <p>This is the code for main.js:</p>
  <pre><code>import fs from 'fs';
  import url from 'url';
  import path from 'path';
  
  function importPlugin(pluginPath) {
    console.log('BEGIN main.importPlugin function');
    console.log(`pluginPath is: ${pluginPath}`);
    let resolvedPluginPath = path.resolve(pluginPath + '/package.json');
    console.log('resolvedPluginPath is: ' + resolvedPluginPath);
    let pluginMetaData = getJsonData(resolvedPluginPath, '');
    console.log('pluginMetaData is: ' + JSON.stringify(pluginMetaData));
    let pluginMainPath = pluginMetaData['main'];
    console.log('pluginMainPath is: ' + pluginMainPath);
    pluginMainPath = path.join(pluginPath, pluginMainPath);
    console.log('pluginMainPath is: ' + pluginMainPath);
    pluginMainPath = url.pathToFileURL(pluginMainPath);
    console.log('pluginMainPath is: ' + pluginMainPath);
    let importedModule;
    let pluginData;
  
      const loadAsyncImport = () =&gt; {
        const asyncImport = async () =&gt; {
          return await myDynamicImport(pluginMainPath);
        };
  
        return asyncImport().then((result) =&gt; {
          return result;
        });
      };
  
      const myDynamicImport = async (path) =&gt; {
        return await import(path);
      };
      loadAsyncImport().then(value =&gt; {
      pluginData = value['default'].initializePlugin();
      console.log('dataLoaded is: ' + pluginData);
    });
    console.log('plugin data should be fully loaded by now and...');
    console.log('the execution log should show that it was executed before 
    this line.');
    console.log('END main.importPlugin function');
  };
  
  /**
   * @function getJsonData
   * @description Loads the specified file and parses it into a JSON 
   * object(s).
   * @param {string} inputData The path and file name of the JSON file
   * that
   * should be loaded and parsed into JSON objects.
   * @param {string} inputMetaData Not used for this business rule.
   * @return {object} The JSON object as it was loaded from the file with
   * minimal to no additional processing.
   * @author Seth Hollingsead
   * @date 2022/04/28
   * @NOTE Cannot use the loggers her, because of a circular dependency.
   */
  const getJsonData = function(inputData, inputMetaData) {
    let functionName = getJsonData.name;
    console.log(`BEGIN main.${functionName} function`);
    console.log(`inputData is: ${inputData}`);
    console.log(`inputMetaData is: ${inputMetaData}`);
    // Make sure to resolve the path on the local system,
    let pathAndFilename = path.resolve(inputData);
    let rawData = fs.readFileSync(pathAndFilename, { encoding: 'UTF8' });
    let returnData = JSON.parse(rawData);
    console.log(`DONE loading data from: ${inputData}`);
    console.log(`returnData is: ${JSON.stringify(returnData)}`);
    console.log(`END main.${functionName} function`);
    return returnData;
  };
  
  console.log('BEGIN main.js');
  importPlugin('C:/haystacks-plugins/pluginOne/');
  
  console.log('END main.js');
  </code></pre>
  <p>And running npm test gives this output, same as my other project and code:</p>
  <pre><code>C:\ultraPluginTest&gt;npm test
  
  &gt; ultraplugintest@1.0.0 test
  &gt; node main.js
  
  BEGIN main.js
  BEGIN main.importPlugin function
  pluginPath is: C:/haystacks-plugins/pluginOne/
  resolvedPluginPath is: C:\haystacks-plugins\pluginOne\package.json
  BEGIN main.getJsonData function
  inputData is: C:\haystacks-plugins\pluginOne\package.json
  inputMetaData is:
  DONE loading data from: C:\haystacks-plugins\pluginOne\package.json
  returnData is: {&quot;name&quot;:&quot;appplugin
  one&quot;,&quot;version&quot;:&quot;0.0.1&quot;,&quot;description&quot;:&quot;An example plugin implementation
  for the haystacks
  platform.&quot;,&quot;main&quot;:&quot;./src/main.js&quot;,&quot;type&quot;:&quot;module&quot;,&quot;scripts&quot;
  {&quot;test&quot;:&quot;echo \&quot;Error: no test specified\&quot; &amp;&amp; exit 1&quot;},&quot;repository}
  {&quot;type&quot;:&quot;git&quot;,&quot;url&quot;:&quot;git+https://github.com/SethEden/pluginOne.git&quot;},
  &quot;keywords&quot;:[&quot;NodeJS&quot;,&quot;Plugin&quot;,&quot;NodeJS-App&quot;],&quot;author&quot;:&quot;Seth
  Hollingsead&quot;,&quot;license&quot;:&quot;MIT&quot;,&quot;bugs&quot;:
  {&quot;url&quot;:&quot;https://github.com/SethEden/AppPlugin-
  One/issues&quot;},&quot;homepage&quot;:&quot;https://github.com/SethEden/AppPlugin-
  One#readme&quot;}
  END main.getJsonData function
  pluginMetaData is: {&quot;name&quot;:&quot;appplugin-
  one&quot;,&quot;version&quot;:&quot;0.0.1&quot;,&quot;description&quot;:&quot;An example plugin implementation 
  for the haystacks 
  platform.&quot;,&quot;main&quot;:&quot;./src/main.js&quot;,&quot;type&quot;:&quot;module&quot;,&quot;scripts&quot;:
  {&quot;test&quot;:&quot;echo \&quot;Error: no test specified\&quot; &amp;&amp; exit 1&quot;},&quot;repository&quot;:
  {&quot;type&quot;:&quot;git&quot;,&quot;url&quot;:&quot;git+https://github.com/SethEden/pluginOne.git&quot;},
  &quot;keywords&quot;:[&quot;NodeJS&quot;,&quot;Plugin&quot;,&quot;NodeJS-App&quot;],&quot;author&quot;:&quot;Seth
  Hollingsead&quot;,&quot;license&quot;:&quot;MIT&quot;,&quot;bugs&quot;:
  {&quot;url&quot;:&quot;https://github.com/SethEden/AppPlugin-
  One/issues&quot;},&quot;homepage&quot;:&quot;https://github.com/SethEden/AppPlugin-
  One#readme&quot;}
  pluginMainPath is: ./src/main.js
  pluginMainPath is: C:\haystacks-plugins\pluginOne\src\main.js
  pluginMainPath is: file:///C:/haystacks-plugins/pluginOne/src/main.js
  plugin data should be fully loaded by now and...
  the execution log should show that it was executed before this line.
  END main.importPlugin function
  END main.js
  BEGIN main.bootStrapPlugin function  &lt;--- Again this line should have 
      been printed above
  END main.bootStrapPlugin function  &lt;--- Again this line should have been 
      printed above
  dataLoaded is: HELLO WORLD - FROM PluginOne   &lt;--- Again this line 
      should have been printed above
  </code></pre>