Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6660625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:10:42+00:00 2026-05-26T02:10:42+00:00

Here is my first file: var self = this; var config = { ‘confvar’:

  • 0

Here is my first file:

var self = this;
var config = {
    'confvar': 'configval'
};

I want this configuration variable in another file, so I have done this in another file:

conf = require('./conf');
url = conf.config.confvar;

But it gives me an error.

TypeError: Cannot read property ‘confvar’ of undefined

What can I do?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T02:10:43+00:00Added an answer on May 26, 2026 at 2:10 am

    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

    ./sourceFile.js:

    // 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 } 
    

    ./consumer.js:

    // 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'
    

    ./sourceFileWithoutDefault.js:

    // 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'
    

    ./consumer2.js

    // 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:

    ./src
    ├── 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.

    ./src/component/myComponent.js:

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

    ./src/component/state.js:

    export default function() {}
    

    ./src/component/index.js

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

    ./src/index.js

    export * from "./component"
    

    Original Answer:

    You need module.exports:

    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');
    console.log(sourceFile.variableName);
    

    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

    ./sourceFile.js:

    const variableName = 'variableValue'
    module.exports = variableName
    

    ./consumer.js:

    const variableName = require('./sourceFile')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is my first attempt at validating XML with XSD. The XML file to
First here's what I'm using and trying to do: the minimal setup for this
My first post here, so i hope this is the right area. I am
Its my first time here and I don't know how to indent this sorry
i have code like this: loading js files dynamically via another js file? but
here my js file : var fm = Ext.form; var grid = new Ext.grid.EditorGridPanel({
This is my first question here :D, first sorry about my english. My question
I am new here so first of all my greetings to you I am
Here's my first question at SO. I have a internal application for my company
Here is the first part of my controller code: public class ControlMController : Controller

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.