Arundale Ramanathan
Arundale Ramanathan

Reputation: 2109

import node js module in ember js framework

I am trying to import a simple node js module into Ember js. I followed the quick start at https://guides.emberjs.com/v3.8.0/getting-started/quick-start/ and got the People List working.

Then I added the simple upper-case module using npm install upper-case and added app.import('node_modules/upper-case/upper-case.js'); to ember-cli-build.js as mentioned in https://guides.emberjs.com/release/addons-and-dependencies/managing-dependencies/.

After that, I opened scientists.js and added import to upper-case as follows:

import Route from '@ember/routing/route';
//import uc from 'upper-case';

export default Route.extend({
  model() {
    var arr = new Array();
    arr[0] = 'Marie Curie'; // uc('Marie Curie');
    arr[1] = 'Mae Jemison';
    arr[2] = 'Albert Hofmann';
    return arr;
  }
});

If I remove the comments, it shows me a blank screen. If I use 'Marie Curie'.toUpperCase() it works, but I want to be able to import such node modules. How can I achieve this?

I have already tried exception while importing module in ember js and ember-auto-import, but they don't seem to work for me. The above method I tried seems to be simple and would be nice if it can work this way.

PS: I could make upper-case work in other JS frameworks such as React and Vue, so the module itself doesn't have any issues.

Upvotes: 0

Views: 930

Answers (1)

NullVoxPopuli
NullVoxPopuli

Reputation: 65183

if you install ember-auto-import, you'll be able to use any npm package like how the particular npm package's documentation says to use it (provided the particular npm package is configured correctly on build).

https://github.com/ef4/ember-auto-import

This'll be a default soon (and is recommended over using app.import)

The reason ember-auto-import is recommended over app.import is because there are ~ 5 different module formats JS can exist in, and you need to worry about those when using app.import. ember-auto-import, powered by webpack, abstracts all that away from you.

fwiw, JS has .toUpperCase() built in: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toUpperCase

so you don't need that particular module.

Edit

I have already tried exception while importing module in ember js and ember-auto-import, but they don't seem to work for me. The above method I tried seems to be simple and would be nice if it can work this way.

did you get any errors with this?

Upvotes: 1

Related Questions