i have a file something.js
which has a function:
someFunction: function(arg1, arg2){
//code blocks
}
In my app.js
file i want to call this function in the app
class. I have imported the something.js
file like this import { someFunction } from './something.js';
. Now i am trying to use it in a function in the app
class
var res = someFunction("abc", 2);
console.log(res);`
i am getting a error Uncaught TypeError: (0 , _something.someFunction) is not a function
Some help would be appreciated.
i have a file something.js
which has a function:
someFunction: function(arg1, arg2){
//code blocks
}
In my app.js
file i want to call this function in the app
class. I have imported the something.js
file like this import { someFunction } from './something.js';
. Now i am trying to use it in a function in the app
class
var res = someFunction("abc", 2);
console.log(res);`
i am getting a error Uncaught TypeError: (0 , _something.someFunction) is not a function
Some help would be appreciated.
Share Improve this question asked Mar 27, 2017 at 12:37 ShocKwav3_ShocKwav3_ 1,7606 gold badges24 silver badges44 bronze badges 2 |4 Answers
Reset to default 5You need to write it like this:
something.js
file -
module.exports = {
A: funtion(){
},
B: funtion(){
}
}
Then import it like this:
import {A} from 'something';
Or use it like this:
something.js
file -
export A(){
}
export B(){
}
Then import it like this:
import {A} from 'something';
Read this article: https://danmartensen.svbtle.com/build-better-apps-with-es6-modules
In order to import something, you need to export it from the other module.
For example, you could export class YourComponent extends React.Component
in something.js.
Then in the other file you can import { YourComponent } from './something'
You could, for example, in something.js
do something like
const MyClass = {
methodName() { return true; }
}
export { MyClass as default } // no semi-colon
Then in the other file you could
import WhateverIWant from 'something';
WhateverIWant.methodName(); // will return true
Edit: An in-depth explanation with lots of examples is available here.
You could either do: in your something.js
file: module.exports = function(){}..
and in your app.js
file:
const functionName = require('./path_to_your_file');
Or export somethingFunction = {}
and in app.js
:
import { somethingFunction } from './path_to_your_file'
Or last: export default somethingFunction = {}
and in app.js
:
import whateverNameYouChoose from './path_to_your_file'
Let me know if that did the trick! :)
In your something.js
file, you can add export someFunction
near the bottom of the file. This will then allow you to import that function using the import { someFunction } from './something.js';
you have earlier.
export someFunction: function(arg1, arg2){ //code blocks }
– Mayank Shukla Commented Mar 27, 2017 at 12:38export default {//statements }
– ShocKwav3_ Commented Mar 27, 2017 at 12:40