Getting this following error: compare.js:1 Uncaught Error: Cannot find module 'ember'

I am trying to write a javascript helper function in my Ember application called compare.js, and that js file is trying to import Ember, where its throwing error, can anybody please suggest me something how to get-rid of this type errors? Here is my compare.js files code - thank you.

import Ember from ‘ember’;

export function compare(params) {
    if (params[3]) {  //handle case insensitive conditions if 4 param is passed.
        params[0] = params[0].toLowerCase();
        params[2] = params[2].toLowerCase();
    }
    let v1 = params[0];
    let operator = params[1];
    let v2 = params[2];
    switch (operator) {
        case '==':
            return (v1 == v2);
        case '!=':
            return (v1 != v2);
        case '===':
            return (v1 === v2);
        case '<':
            return (v1 < v2);
        case '<=':
            return (v1 <= v2);
        case '>':
            return (v1 > v2);
        case '>=':
            return (v1 >= v2);
        case '&&':
            return !!(v1 && v2);
        case '||':
            return !!(v1 || v2);
        default:
            return false;
    }
}  
export default Ember.Helper.helper(compare);</pre>  

I want to be able to import or use this function in my hbs file, how can I do it any help please to fix these two things please - need some help - thank you.

If you are using more modern Ember (>2.15 I believe) you can (or at least should) no longer import “Ember”, you shoudl import what you need from the correct module. In this case you want:

import { helper } from '@ember/component/helper';

...

export default helper(compare);
1 Like

Then what is the syntax to call it? Thank you please.

It should look like what I posted above :point_up:, import `helper from the correct module and then use it in the export line as shown

I did it, I did write in the following way:

import { helper } from '@ember/component/helper';
export function compare(params) {
    if (params[3]) {  //handle case insensitive conditions if 4 param is passed.
        params[0] = params[0].toLowerCase();
        params[2] = params[2].toLowerCase();
    }
    let v1 = params[0];
    let operator = params[1];
    let v2 = params[2];
    switch (operator) {
        case '==':
            return (v1 == v2);
        case '!=':
            return (v1 != v2);
        case '===':
            return (v1 === v2);
        case '<':
            return (v1 < v2);
        case '<=':
            return (v1 <= v2);
        case '>':
            return (v1 > v2);
        case '>=':
            return (v1 >= v2);
        case '&&':
            return !!(v1 && v2);
        case '||':
            return !!(v1 || v2);
        default:
            return false;
    }
} 
export default helper(compare);

And I was calling it as below in my hbs file

 {{#compare 'A' '==' 'A'}}
{{log 'Hi 1'}}
 {{/compare}}

And I am getting the following error, any help my friend? ember.debug.js:36804 Uncaught (in promise) Error: Assertion Failed: A component or helper named "compare" could not be found

Any help my friend

Is that file located at app/helpers/compare.js?

1 Like

It is located at app/helpers/print/compare.js, and when I run

ember install ember-truth-helpers why is it giving me the following error? ‘ember’ is not recognized as an internal or external command - I am not able to install “ember-truth-helpers”, for that they were saying I need to install ember-cli, when I try to install ember-cli I am getting the following error. I need help for both of them - thank you