My Express.js server is currently being started by running start.js
, which in turn runs server.js
(code below). For example,
nodemon start.js
Problem: When I try to run the file at src/lib/seed_database.js
using
node seed_database.js
we get the errors showing that babel is not transpiling any JS files, such as
SyntaxError: Unexpected token 'export'
What is the recommended way to get babel to work on a file other that server.js
?
Environment
- Node 13.7.0
- Mac OS X 10.15.2
- @babel/runtime 7.8.4
- @babel/core 7.8.4
- @babel/plugin-transform-runtime 7.8.3
- @babel/preset-env 7.8.4
- @babel/register 7.8.3"
Directory Structure
my-app
├── .node_modules
├── src
│ ├── lib
│ │ └── seed_database.js
│ ├── v1
│ │ └── routes.js
│ ├── start.js
│ └── server.js
├── .babelrc
├── package-lock.json
├── package.json
start.js
require("@babel/register")({
presets: [
["@babel/preset-env", {
"targets": {
"node": "current"
}
}]
]
});
module.exports = require('./server.js')
server.js
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 => {
if (err) {
console.log('Cannot run!')
} else {
console.log(`App listening on port: ${constants.PORT}`)
}
});
seed_database.js
const db = require('../../config/db')['appDb'];
...
db.js
...
export const appDb = appDb;
.babelrc
{
"plugins": [
["@babel/transform-runtime"]
]
}