1

I'm trying to use some variables from a JavaScript file. My file is in the same path than my App component, and it has stored some value.

This is the js file, that simple, it only has the variable:

let num = 5;

On my App.js file I'm importing the js file this way:

import * as utils from './myjsfile';

And then on the componentDidMount methodI'm tryong to show it with an alert:

  componentDidMount() {
      alert(utils.num)
  }

But what I get is a × TypeError: Cannot read property 'num' of undefined

I've read some examples and this approach was supposed to work, I don't know what I'm doing wrong or if I have to do it in other way.

Thanks

4 Answers 4

5

You have to export it. Try

export let num = 5;
Sign up to request clarification or add additional context in comments.

Comments

1

use export

export {num};

and import

import {num} from './myjsfile.js';

Comments

1

Export file lets.js:

let num_x = 5;
let num_y = 10;

export default { num_x, num_y };

import and use:

import * as nums from './lets';

let a = nums.default.num_y;
let b = nums.default.num_x;

1 Comment

what is the purpose of 'default' here?
0

To access anything you have to export it first in node. Add export before let

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.