Home>

Hello everyone!

There is a constant that lies in a separate file with other similar ones, conditionally looks like this:

export const BASE_URL= 'https://example.com/';

I want to use this variable in another file which does module.exports

module.exports= {
    siteUrl: BASE_URL,
    generateRobotsTxt: true
    autoLastmod: false
}

Is it possible to somehow import such a variable?

Import viaimport {BASE_URL } from ''does not work, and there is no way to create a separate file and export the variable via module.exports either :).

What does "Import via import {BASE_URL } from '' does not work" mean?

Gelloiss2022-02-14 02:18:12
  • Answer # 1

    Most likely you are writing server-side code in NodeJS. And Node doesn't support ES6 imports by default.
    (you can change the package type tomoduleand then there will be supportimport, but that's a completely different story. more details in the documentation: here)

    The solution is this: in the file from which you want to export the variable, change the export to modular:

    module.exports= 'https://example.com/';
    

    And then import in another file viarequire:

    const BASE_URL= require('filename')
    
  • Answer # 2

    Most likely you are writing server-side code in NodeJS. And Node doesn't support ES6 imports by default.
    (you can change the package type tomoduleand then there will be supportimport, but that's a completely different story. more details in the documentation: here)

    The solution is this: in the file from which you want to export the variable, change the export to modular:

    module.exports= 'https://example.com/';
    

    And then import in another file viarequire:

    const BASE_URL= require('filename')