Passing Variable from One Js to Another Js File in Nodejs

How to get a variable from a file to another file in Node.js

Edit (2020):

Since Node.js version 8.9.0, you can also use ECMAScript Modules with varying levels of support. The documentation.

  • For Node v13.9.0 and beyond, experimental modules are enabled by default
  • For versions of Node less than version 13.9.0, use --experimental-modules

Node.js will treat the following as ES modules when passed to node as the initial input, or when referenced by import statements within ES module code:

  • Files ending in .mjs.
  • Files ending in .js when the nearest parent package.json file contains a top-level field "type" with a value of "module".
  • Strings passed in as an argument to --eval or --print, or piped to node via STDIN, with the flag --input-type=module.

Once you have it setup, you can use import and export.

Using the example above, there are two approaches you can take


// This is a named export of variableName
export const variableName = 'variableValue'
// Alternatively, you could have exported it as a default.
// For sake of explanation, I'm wrapping the variable in an object
// but it is not necessary.
// You can actually omit declaring what variableName is here.
// { variableName } is equivalent to { variableName: variableName } in this case.
export default { variableName: variableName }


// There are three ways of importing. 
// If you need access to a non-default export, then
// you use { nameOfExportedVariable }
import { variableName } from './sourceFile'
console.log(variableName) // 'variableValue'

// Otherwise, you simply provide a local variable name
// for what was exported as default.
import sourceFile from './sourceFile'
console.log(sourceFile.variableName) // 'variableValue'


// The third way of importing is for situations where there
// isn't a default export but you want to warehouse everything
// under a single variable. Say you have:
export const a = 'A'
export const b = 'B'


// Then you can import all exports under a single variable
// with the usage of * as:
import * as sourceFileWithoutDefault from './sourceFileWithoutDefault'

console.log(sourceFileWithoutDefault.a) // 'A'
console.log(sourceFileWithoutDefault.b) // 'B'

// You can use this approach even if there is a default export:
import * as sourceFile from './sourceFile'

// Default exports are under the variable default:
console.log(sourceFile.default) // { variableName: 'variableValue' }

// As well as named exports:
console.log(sourceFile.variableName) // 'variableValue

You can re-export anything from another file. This is useful when you have a single point of exit (index.{ts|js}) but multiple files within the directory.

Say you have this folder structure:

├── component
│   ├── index.js
│   ├── myComponent.js
│   └── state.js
└── index.js

You could have various exports from both store.js and my-component.js but only want to export some of them.


import createState from "./state";
export function example(){ };


export default function() {}


export { example as default } from "./myComponent";
export * from "./myComponent"


export * from "./component"

Original Answer:

You need module.exports:


An object which is shared between all instances of the current module
and made accessible through require(). exports is the same as the
module.exports object. See src/node.js for more information. exports
isn't actually a global but rather local to each module.

For example, if you would like to expose variableName with value "variableValue" on sourceFile.js then you can either set the entire exports as such:

module.exports = { variableName: "variableValue" };

Or you can set the individual value with:

module.exports.variableName = "variableValue";

To consume that value in another file, you need to require(...) it first (with relative pathing):

const sourceFile = require('./sourceFile');

Alternatively, you can deconstruct it.

const { variableName } = require('./sourceFile');
// current directory --^
// ../ would be one directory down
// ../../ is two directories down

If all you want out of the file is variableName then


const variableName = 'variableValue'
module.exports = variableName


const variableName = require('./sourceFile')

How to pass variable from one Javascript to another Javascript file

see about local and global variables for more info.

Make sure your var X is not inside a function and that your file is load in the correct order.

<script src="file1.js"><script> //declare var x=1 here
<script src="file2.js"><script> // you can access x from here.

How I can change value varible in a file in another file in javascript

It's not working because JavaScript doesn't pass variables with primitive data types, such as an integer, through its reference to other functions, but rather creates a whole new variable with a different memory address. The only way you can mutate the original memory location is if you're passing in an array or object, which JavaScript will then pass in a pointer to the original memory location, ie "reference", to the function.

So you'd have to change the datatype of length to an object, and add the value/length of the object as a property. The excute function would then just access that property and increment it like so:


const obj = { length: 0 }



module.exports = {
name: 'join',
description: "",

Keep in mind, you have to pass the whole object, otherwise if you just pass the obj.length, it will simply copy the value and create a whole new variable set to that value.

Related Topics

Leave a reply