title: NodeJS: "Unexpected identifier" when trying to import module
body: <p>I am trying to use ES6 imports in Node v10.15.3 LTS, but I keep running into the same syntax error.  It occurs no matter whether I use esm, babel, or the --experimental-modules flag to enable support for ES6 imports.  Here is the error message:</p>
  
  <pre><code>/home/derrick/demo/index.js:1
  (function (exports, require, module, __filename, __dirname) { import otherFunction from './otherFunction';
                                                                       ^^^^^^^^^^^^^
  
  SyntaxError: Unexpected identifier
      at new Script (vm.js:80:7)
      at createScript (vm.js:274:10)
      at Object.runInThisContext (vm.js:326:10)
      at Module._compile (internal/modules/cjs/loader.js:664:28)
      at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
      at Module.load (internal/modules/cjs/loader.js:600:32)
      at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
      at Function.Module._load (internal/modules/cjs/loader.js:531:3)
      at Function.Module.runMain (internal/modules/cjs/loader.js:754:12)
      at startup (internal/bootstrap/node.js:283:19)
  </code></pre>
  
  <p>Here is my code (using esm):</p>
  
  <p><strong>package.json</strong></p>
  
  <pre><code>{
    "name": "demo",
    "version": "1.0.0",
    "description": "",
    "main": "index.js",
    "scripts": {
      "test": "echo \"Error: no test specified\" &amp;&amp; exit 1"
    },
    "keywords": [],
    "author": "",
    "license": "ISC",
    "dependencies": {
      "esm": "^3.2.22"
    }
  }
  </code></pre>
  
  <p><strong>index.js</strong></p>
  
  <pre><code>import otherFunction from './otherFunction';
  </code></pre>
  
  <p><strong>otherFunction.js</strong></p>
  
  <pre><code>const otherFunction =&gt; {
      console.log('Inside other function');
  }
  export default otherFunction;
  </code></pre>
  
  <p>I have gone through a couple dozen tutorials trying to figure this out, but I keep getting the same error message.</p>
  
  <p>I've read up on the syntax for imports/exports.  I also tried importing { otherFunction } instead of otherFunction, changing the file extensions to .mjs, named vs. default exports, and anything else I can find when I Google this error message.</p>
  
  <p>I am grateful for any suggestions. I've spent 8 hours on this and am about to scream :-)</p>