How to access node_modules directory and read the files?

My application is using some ember addons as a dependency. I want to extract all the available components from those addons. So I am trying to access the node_modules directory and read the addon’s component js Files with nodejs in ember-cli-build.js. Is there any possible do the following thing inside the ember router or service or components.

const fs = require('fs');   
let dirPath = '../node_modules/my-ember-addon/addon/components';   
fs.readdir(dirPath, function(err, list) {   
});

I want to extract all the available components from those addons

Could you describe what you’re trying to accomplish in a little more detail? You can import components from addons to use/re-export/extend/etc but it depends on what you mean by “extract”.

Is there any possible do the following thing inside the ember router or service or components.

The problem with this is that code that’s running in the router/service/components/etc is “run time” code and node_modules doesn’t exist at “run time”, just at build time. The ember-cli-build script is executed at build time and is used to process all the files in your source tree and in node_modules and smash them together into the “compiled” assets which are then executed. So looking through node_modules isn’t possible at runtime, but there could be a different way to accomplish what you’re attempting, depending on what that is.

1 Like

The file config/environment.js is the default bridge between Node and the runtime app. It evaluates in Node at build time, but the return value is available inside your app by doing import ENV from "your-app-name/config/environment".

So if you put that fs-using code inside config/environment.js and return the resulting list of strings as part of that module’s exported value, you can access the list of strings from your app.

Thanks for your valuable help!

I am trying to get the all components of my-addon via nodejs and reading the each component js file’s developer’s comments. Please refer my attachment

Right now, I have implemented these logics into the ember-cli-build.js. My question is, Is there any other better possible way to do this?

I guess it partly depends on what you’re trying to do with the information that you are extracting. If you just want to compile it into the built assets of the project what you’re doing now makes sense (though this sounds like it might be a good use case for a broccoli plugin instead).

If you want to use the comments within your ember app you’ll need a way to pass them off to Ember as a static file in the assets or something like that. That would probably also be done most easily with a broccoli plugin.