I have a monorepo with the following folder structure.
packages/
- server
- src
- index.js
- build
- lib1
- lib2
My node server uses express and graphql, with a very simple setup.
index.js
const start = async () => {
const server = new ApolloServer({
schema: clientSchema,
tracing: true,
introspection: process.env.NODE_ENV !== "production",
});
await server.start()
const app = express();
app.use(express.json({limit: '10mb'}));
app.use(express.urlencoded({limit: '10mb', extended: true}));
server.applyMiddleware({ app });
app.listen({ port }, () =>
console.log(`🚀 Server ready at http://localhost:${port}${server.graphqlPath}`)
);
}
I am using babel to transpile the node server using a .babelrc file with the following config
{
"presets": ["@babel/preset-env"],
"plugins": ["@babel/plugin-transform-runtime"]
}
lib1 and lib2 are packages using import and export syntax.
When I build the node server using something like babel -d ./build ./src and then start the server using node ./build/index.js
I am presented with the error SyntaxError: Cannot use import statement outside a module for one of the files in package/lib1.
Now I have gotten this to work by changing index.js in the server package to be like the example below however im finding the server uses a lot of memory and takes a long time when initially starting up which makes my Heroku server cost really expensive as it will crash due to memory quotas going past 1GB.
require('@babel/register')({
presets: ['@babel/preset-env'],
ignore: ['node_modules'],
plugins: [
"@babel/plugin-transform-runtime"
]
});
// Import the rest of our application.
module.exports = require('./server.js');