Ember.js: Cannot use computed properties in node.js

Created on 10 Mar 2018  Â·  9Comments  Â·  Source: emberjs/ember.js

require('ember-metal');
require('ember-runtime');

const EmberObject = Ember.Object;
const computed = Ember.computed;

const User = EmberObject.extend({
  firstName: 'Izel',
  lastName: 'Nakri',
  fullName: computed('firstName', 'lastName', function() {
    return `${this.get('firstName')} ${this.get('lastName')}`;
  })
});

let me = User.create();

console.log(me.get('firstName')); // works prints 'Izel'
console.log(me.get('lastName')); // works prints 'Nakri'
console.log(me.get('fullName')); // !! CRASHES: TypeError: this.func.call is not a function

// Stacktrace:
// at ComputedProperty.Cp.get (..myproject../node_modules/ember-metal/ember-metal.js:3738:21)
Inactive

Most helpful comment

@k3n use const Ember = require('ember-source/dist/ember.debug'); and then try Object.keys(Ember) and then do new Ember.Object

If you did 2 === require('ember-source/dist/ember.debug') it would be false.

Also, the inspect issue is fixed in canary https://github.com/emberjs/ember.js/blob/3a7fd70ace89286eb192897d57d61ab4e0603fa6/packages/ember-utils/lib/inspect.ts#L29-L30

https://nodejs.org/api/util.html#util_custom_inspection_functions_on_objects

All 9 comments

ok I figured ember-metal npm package is 6 years old :) I can do this instead and it works!

require('ember-metal-node');
require('ember-runtime-node');

const EmberObject = Ember.Object;
const computed = Ember.computed;

const User = EmberObject.extend({
  firstName: 'Izel',
  lastName: 'Nakri',
  fullName: computed('firstName', 'lastName', function() {
    return `${this.get('firstName')} ${this.get('lastName')}`;
  })
});

let me = User.create();

console.log(me.get('firstName')); // works prints 'Izel'
console.log(me.get('lastName')); // works prints 'Nakri'
console.log(me.get('fullName')); // works prints 'Izel Nakri'

Generally speaking, we should make this better. The main package that should be used (and is current) is ember-source. We should make its main entry point export the global (right now you’d have to require(‘ember-source/dist/ember.debug’))...

right now you’d have to require(‘ember-source/dist/ember.debug’)

I tried that, in trying to get access to the main Ember object, and it only returns a literal 2.

I'm obviously missing something...

I tried it too, this is an odd return Ember dist gives in node environment. You've actually loaded Ember successfully in node, try Ember.Object you will see that it is loaded correctly in node.

I wish ember will return the right object as it does in browser environment in near future to avoid confusion, in addition I hope that we can very soon have @ember packages truly distributed as ES Modules instead of the current AMD module in-disguise architecture enabled via ember babel shims in ember-cli.

It’s not actually 2 it is printing 2 because node utils inspect is calling Ember.inspect with 2 as the arg

$ node -v
v8.11.1
$ node
> require('ember-source/dist/ember.debug');
2
> Ember
ReferenceError: Ember is not defined

Is it that I also need to require() the ember-metal and ember-runtime packages?

@k3n use const Ember = require('ember-source/dist/ember.debug'); and then try Object.keys(Ember) and then do new Ember.Object

If you did 2 === require('ember-source/dist/ember.debug') it would be false.

Also, the inspect issue is fixed in canary https://github.com/emberjs/ember.js/blob/3a7fd70ace89286eb192897d57d61ab4e0603fa6/packages/ember-utils/lib/inspect.ts#L29-L30

https://nodejs.org/api/util.html#util_custom_inspection_functions_on_objects

@izelnakri @k3n @krisselden @rwjblue is this still an issue, perhaps we should close, what do you think?

No, I believe I was just doing it wrong ;)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

skoryky picture skoryky  Â·  50Comments

MelSumner picture MelSumner  Â·  33Comments

marcoow picture marcoow  Â·  59Comments

workmanw picture workmanw  Â·  79Comments

matheusdavidson picture matheusdavidson  Â·  37Comments