Discovering an npm module within an ember-cli addon component

I recently encountered an issue while using ember-browserify to locate npm modules in my ember-cli applications - it seems to not function properly for ember-cli addons.

This leads me to wonder: Are there alternative methods for importing npm modules into an ember-cli addon?

Update:

Although I faced difficulties importing the npm module, I discovered that the specific module I needed was also available as a bower component. Therefore, I successfully installed it and imported it through the index.js file as shown below:

included: function(app) {
  this._super.included(app);

  app.import('bower_components/dropzone/dist/dropzone.js');
}

Using this approach resolved the issue. Unfortunately, importing npm modules directly from node_modules proved to be challenging. It's frustrating that incorporating npm modules into an ember-cli addon is so complicated.

Answer №1

If you're looking to implement this functionality, ember-fetch has a solution available. The code may seem a bit complicated at first glance:

treeForVendor: function(tree) {

  // Determining the path for whatwg-fetch in node modules
  var fetchPath = require.resolve('whatwg-fetch');

  // Applying a utility function to expand it into a pattern
  var expandedFetchPath = expand(fetchPath);

  // Renaming all files in a tree with items matching the pattern
  var fetch = rename(find(expandedFetchPath), function(path) {
    return 'whatwg-fetch/fetch.js'
  });

  // Implementing a template for each file in the tree and then merging them
  return mergeTrees([
    new Template(fetch, templatePath, function variables(content) {
      return {
        moduleBody: content
      };
    })
  ]);
},

included: function(app) {
  this.app = app;
  this._super.included(app);

  // Importing the created tree from the treeForVendor function
  app.import('vendor/whatwg-fetch/fetch.js', {
    exports: {
      default: [
        'default',
        'Headers',
        'Request',
        'Response'
      ]
    }
  });
}

Extracted from https://github.com/stefanpenner/ember-fetch/blob/master/index.js

Hopefully, this explanation aids your understanding.

Answer №2

To implement the addon, remember to include the import statement for the app/ version of the object (which typically only exports the object).

In the applications that utilize the addon, make sure to install both ember-browserify and the required npm module.

For instance, in the app/models/user.js file within an addon:

import TimezoneDetect from 'npm:jstimezonedetect';

export { default } from 'common/models/user';

Refer to https://github.com/ef4/ember-browserify#using-ember-browserify-in-addons

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Explain the concept of render hijacking in react technology

After learning about High Order Components (HOC), I came across the concept of render hijacking. Can someone please explain what exactly render hijacking entails? ...

The issue with angular JavaScript in the child component is causing the left side navigation dropdown to malfunction

I'm currently facing an issue with the left side navigation in my home component. The dropdown functionality is not working within one of the routing modules (admin-routing.module.ts). Interestingly, the navigation works perfectly fine in app-routing. ...

Different ways to split up information on the client side into separate "pages"

Looking for a way to split a lengthy HTML text-only article into multiple divs for easier page navigation on mobile devices? Perhaps dividing it into separate pages that users can swipe through on their iPad or iPhone? I've experimented with JavaScri ...

Develop a simple application using the MEAN stack framework

I am new to Node.js and Express and I am interested in creating a basic AngularJS application. However, I am unsure of where to begin in terms of file organization. My desired file structure is as follows: - public ----- app ---------- components -------- ...

Analyzing the attributes of an array object in JavaScript

I have an assignment that requires me to filter an array into a new array for a vehicle with the term "ford" in it. The format should be in ES6 using arrow function syntax, like this: const arr = [ {name: "Honda", type: "Accord"}, {name: "ford", type: "fu ...

The NPM Install process seems to be skipping certain files that were successfully installed in the past

When I first installed NPM Install in a folder, it created multiple folders and files: node_modules public src .DS_Store package.json package-lock.json webpack.config.js After that, npm start functioned perfectly. Now, as I embark on a new project for th ...

Plugin for managing responses other than 200, 301, or 302 in forms

Currently, I am using the jQuery Form Plugin in my project. Everything works smoothly when the server sends a 200 response as it triggers the success listener seamlessly. However, according to the standard protocol, the browser automatically handles 301 ...

Stop zombie.js from loading exclusively third-party resources

During a test, I am using zombie.js to load a page from a local express server. However, the page contains a script element that makes a call to Google Analytics. I want to prevent this external script from loading while allowing other local scripts to run ...

The JScolor Color Picker has a slight misalignment

I am having an issue with the jscolor color picker on my webpage. The rainbow part of it appears offset within the rest of the picker. You can see what it looks like on my site here: (https://ibb.co/9N8dHXs). On my website, I have a large canvas for three ...

Loading JSON data from a file in an AngularJS service

Looking to retrieve JSON data from a file named driverStandings.json, which can be found in the structure from the provided image. I am utilizing a service API to fetch this information by using the code displayed below (refer to image). After compiling, ...

Encountering Laravel npm installation warnings with npm showing deprecated graceful-fs notifications

As a newcomer to Laravel, I could use some guidance. Currently, I am working through Laracasts training on managing css and js. Upon attempting to run npm install, I encountered warnings such as: npm WARN install Couldn't install optional dependency ...

The Axios GET request was not functioning properly after attempting to use Axios stream.on("data")

I am working with a backend API that has an endpoint named /subscribe, which gives back a continuous stream of data. Along with this, I have a node process, and I am utilizing axios to send a request to my backend API with a response type of "stream& ...

Accessing a Variable in one JavaScript File from Another JavaScript File

In the process of creating a basic game using only JavaScript and jQuery, I have split it into two separate pages. The first page contains all the rules and necessary information, while the second page is where the actual game takes place. My goal is to in ...

Is it possible to integrate semantic-release with Jenkins CI?

I came across semantic-release on GitHub and was curious about its compatibility with Jenkins CI. After checking the CI configuration documentation, I couldn't find any specific details regarding Jenkins integration. If anyone has insights or inform ...

What is the process for adding an item to an object?

I currently have the following data in my state: fbPages:{'123':'Teste','142':'Teste2'} However, I am in need of a dynamic solution like the one below: async getFbPages(){ var fbPages = {} awa ...

Content briefly appears and then vanishes with the use of ng-if

On my webpage, I have content that is enclosed in an ng-if directive as shown below: <div ng-if="ShowMessgae" class="text-danger"> <p> <strong> Message displayed to User </strong> </p> < ...

Methods to maintain the select2 dropdown event open while interacting with another select2 dropdown

I have a situation where I need to dynamically add a class to a div whenever a select2 dropdown is open. To achieve this functionality, I am utilizing the open and close events of select2. The current code successfully adds the class when opening a selec ...

The error message "Unable to retrieve property 'commands' of undefined (discord.js)" indicates that the specified property is not defined

As I try to incorporate / commands into my Discord bot, I encounter a persistent error: An issue arises: Cannot read property 'commands' of undefined Below is my main.js file and the problematic segment causing the error: Troublesome Segment c ...

"Convert a date string to a date object using the verbose moment date

I utilized the materialize datepicker to select a date in French format. Now I need to convert this formatted date back to a date object for use in my API. Here's how I attempted to revert the date to a standard format: moment("dimanche 30 juillet 20 ...

I'm looking for a method in JavaScript that can search through my XML file to locate specific attributes and return the entire parent element containing that attribute. Can anyone help with

I'm completely new to XML and Javascript. I currently have this code in my HTML file: <select id="eigenschaften" name="eigenschaften" type="text" onchange=""> <option value="">Choose Property</option> <option value="soci ...