0

When I try to transpile this code with babel:

import admin from 'firebase-admin';

const serviceAccount = require('./.private/app-delivery-dev.json');


admin.initializeApp({
    credential: admin.credential.cert(serviceAccount),
    databaseURL: 'https://app-delivery-dev.firebaseio.com',
});


import app from './app';

app.listen(3333);

I get this result:

"use strict";

var _firebaseAdmin = _interopRequireDefault(require("firebase-admin"));

var _app = _interopRequireDefault(require("./app"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

const serviceAccount = require("./.private/app-delivery-dev.json");

_firebaseAdmin.default.initializeApp({
  credential: _firebaseAdmin.default.credential.cert(serviceAccount),
  databaseURL: 'https://app-delivery-dev.firebaseio.com'
});

_app.default.listen(3333);

But the app must only be imported after the firebase admin is initialized, otherwise it will throw an error of "firebase app does not exist". Is There a way to babel not change the order in which import statements appear?

This is my babel config file:

module.exports = {
    presets: [
        [
            '@babel/preset-env',
            {
                targets: {
                    node: 'current'
                }
            },
        ],
        '@babel/preset-typescript'
    ],
    plugins: [
        ['module-resolver', {
            alias: {
                '@controller': './src/controllers',
            }
        }]
    ],
    ignore: [
        '**/*.test.ts',
        '**/*.spec.ts',
        './.private/**/*'
    ]
};
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • https://stackoverflow.com/questions/37503707/what-determines-the-order-of-require-calls-in-babel-transpiled-scripts – Phix Jul 10 '20 at 22:08
  • Babel is not doing anything improper here. All modules imported by another module will be loaded in an unspecified, potentially asynchronous order _before_ the importing module is loaded. The hoisting is valid. – Aluan Haddad Jul 11 '20 at 06:16

0 Answers0