admin管理员组

文章数量:1419611

In a file called File_A.js I have a function that contains a constant. I want to export this const, and only this one (not the whole function) in order to use the constant's value in another file called File_B.js. I tried to use module.exports but it returns that the variable is undefined. Below a simplified example. Thanks

// my function in File_A.js
const MyFunctionA = () => {

  const myVariable = 'hello'
  module.export = {myVariable: myVariable}

  return (
  /*...*/
  );
}

// my second function in File_B.js
const MyFunctionB = () => {

  const {myVariable} = require('./File_A.js');
  console.log(myVariable) // undefined

  return(
  /*...*/
  );
}

In a file called File_A.js I have a function that contains a constant. I want to export this const, and only this one (not the whole function) in order to use the constant's value in another file called File_B.js. I tried to use module.exports but it returns that the variable is undefined. Below a simplified example. Thanks

// my function in File_A.js
const MyFunctionA = () => {

  const myVariable = 'hello'
  module.export = {myVariable: myVariable}

  return (
  /*...*/
  );
}

// my second function in File_B.js
const MyFunctionB = () => {

  const {myVariable} = require('./File_A.js');
  console.log(myVariable) // undefined

  return(
  /*...*/
  );
}

Share Improve this question edited Dec 2, 2020 at 11:15 rkhan asked Dec 2, 2020 at 11:05 rkhanrkhan 3151 gold badge6 silver badges15 bronze badges 5
  • 1 Isn't it module.exports (with an s)? – T.J. Crowder Commented Dec 2, 2020 at 11:10
  • The module.export should be in the global scope since it won't be assigned until MyFunctionA is called. – John Commented Dec 2, 2020 at 11:10
  • 1 @PraveenKumarPurushothaman - Surprisingly, with the style of export the OP is using, you can...it's just not a good idea. :-) – T.J. Crowder Commented Dec 2, 2020 at 11:13
  • 1 If it's a const reference to an immutable thing (in this case a string) just move it out of the function into the top level, there's no reason to hide it in a closure. – Jared Smith Commented Dec 2, 2020 at 11:19
  • 1 @T.J.Crowder Thanks man. This is something new I learnt today. – Praveen Kumar Purushothaman Commented Dec 2, 2020 at 12:00
Add a ment  | 

1 Answer 1

Reset to default 3

how to export a constant which is inside a function?

There are two answers to this:

  1. You don't. It doesn't make sense. Instead, you move the constant out of the function and export it.

  2. You do it exactly as you have done, but the constant won't be in the module's exports until MyFunctionA has been executed at least once. This is possible because the CommonJS-style modules that you're using are dynamic and can change at runtime. However, making your exports dependent on a function call is asking for trouble, as you've discovered.

So taking #1 on board, we get:

// my function in File_A.js
const myVariable = "hello"; // Odd name for a constant? ;-)
module.exports.myVariable = myVariable;
const MyFunctionA = () => {
    return (
        /*...*/
    );
};

A couple of notes on that:

  1. MyFunctionA still closes over the constant and references it exactly the way it used to.

  2. myVariable doesn't bee a global, because the top-level scope of a CommonJS module isn't global scope.

本文标签: javascripthow to export a constant which is inside a functionReactJSStack Overflow