Search code examples
javascriptnode.jsecmascript-6modulebabeljs

Inline import by ES6 modules in Node.js


In an "old way" of managing modules in Node.JS (CommonJS modules) you can do something like this:

Example of Express.js route: app.use('/user', require("./user"));

How to do this when I am using ES6 Modules (import, export) and transcribing by Node.JS server by babel?

I can't just do: app.use('/user', import {user} from './user');


Solution

  • Try separating it out into multiple expressions - import (as well as export) are not available at the same lexical level as you are trying to use it the example:

    import { user } from './user'
    
    ...
    
    app.use('/user', user)