Is it possible to use a variable in a file called first.js inside another file called second.js?
first.js contains a variable called colorcodes.
Is it possible to use a variable in a file called first.js inside another file called second.js?
first.js contains a variable called colorcodes.
You can export the variable from first file using export.
//first.js
const colorCode = {
black: "#000",
white: "#fff"
};
export { colorCode };
Then, import the variable in second file using import.
//second.js
import { colorCode } from './first.js'
Using Node.js you can export the variable via module.
//first.js
const colorCode = {
black: "#000",
white: "#fff"
};
module.exports = { colorCode };
Then, import the module/variable in second file using require.
//second.js
const { colorCode } = require('./first.js')
You can use the import and export aproach from ES6 using Webpack/Babel, but in Node.js you need to enable a flag, and uses the .mjs extension.