title: Babel Not Working Outside of Main Node.js App
body: <p>My Express.js server is currently being started by running <code>start.js</code>, which in turn runs  <code>server.js</code> <em>(code below)</em>. For example,</p>
  
  <pre><code>nodemon start.js
  </code></pre>
  
  <p><strong>Problem:</strong> When I try to run the file at <code>src/lib/seed_database.js</code> using </p>
  
  <pre><code>node seed_database.js
  </code></pre>
  
  <p>we get the errors showing that babel is not transpiling any JS files, such as</p>
  
  <blockquote>
    <p>SyntaxError: Unexpected token 'export'</p>
  </blockquote>
  
  <p>What is the recommended way to get babel to work on a file other that <code>server.js</code>?</p>
  
  <p><strong>Environment</strong></p>
  
  <ul>
  <li>Node 13.7.0</li>
  <li>Mac OS X 10.15.2</li>
  <li>@babel/runtime 7.8.4</li>
  <li>@babel/core 7.8.4</li>
  <li>@babel/plugin-transform-runtime 7.8.3</li>
  <li>@babel/preset-env 7.8.4</li>
  <li>@babel/register 7.8.3"</li>
  </ul>
  
  <p><strong>Directory Structure</strong></p>
  
  <pre><code>my-app
  ├── .node_modules
  ├── src
  │   ├── lib
  │   │    └── seed_database.js
  │   ├── v1
  │   │    └── routes.js
  │   ├── start.js
  │   └── server.js
  ├── .babelrc
  ├── package-lock.json
  ├── package.json
  </code></pre>
  
  <p><strong>start.js</strong></p>
  
  <pre><code>require("@babel/register")({
      presets: [
        ["@babel/preset-env", {
          "targets": {
            "node": "current"
          }
        }]
      ]
  });
  
  module.exports = require('./server.js')
  </code></pre>
  
  <p><strong>server.js</strong></p>
  
  <pre><code>import express from 'express';
  import v1ApiRoutes from './v1/routes';
  import constants from './config/constants';
  
  const app = express();
  app.use('/v1', v1ApiRoutes);
  
  app.listen(constants.PORT, err =&gt; {
      if (err) {
          console.log('Cannot run!')
      } else {
          console.log(`App listening on port: ${constants.PORT}`)
      }
  });
  </code></pre>
  
  <p><strong>seed_database.js</strong></p>
  
  <pre><code>const db = require('../../config/db')['appDb'];
  
  ...
  </code></pre>
  
  <p><strong>db.js</strong></p>
  
  <pre><code>...
  
  export const appDb = appDb;
  </code></pre>
  
  <p><strong>.babelrc</strong></p>
  
  <pre><code>{
      "plugins": [
          ["@babel/transform-runtime"]
      ]  
  }
  </code></pre>
  
  <hr>
  
  <h2>Update</h2>
  
  <p>Jeremy Harris' suggestion works:</p>
  
  <pre><code>require("@babel/register")({
      presets: [
        ["@babel/preset-env", {
          "targets": {
            "node": "current"
          }
        }]
      ]
  });
  
  const db = require('../../config/db')['appDb'];
  </code></pre>
  
  <p>However, when we replace the <code>require</code> line with named imports:</p>
  
  <pre><code>import { appDb as db } from '../../config/db';
  </code></pre>
  
  <p>we now get a new error</p>
  
  <blockquote>
    <p>SyntaxError: Cannot use import statement outside a module</p>
  </blockquote>
  
  <hr>
  
  <h2>Update #2</h2>
  
  <p>Using Jeremy Harris' second suggestion of a single entry point works</p>
  
  <pre><code>node start.js seed
  </code></pre>
  
  <p><strong>start.js</strong></p>
  
  <pre><code>require("@babel/register")({
      presets: [
        ["@babel/preset-env", {
          "targets": {
            "node": "current"
          }
        }]
      ]
  });
  
  
  if (process.argv.indexOf('seed') &gt; -1) {
    module.exports = require('./lib/seed_database.js')
    return
  }
  
   module.exports = require('./server.js')
  </code></pre>