Jake Millington Jake Millington - 1 year ago 79
Node.js Question

Can I use a script alias existing in another node module's package.json?

I've created a node module that produces a

file with data from multiple APIs. It's designed to be able to save the
file into whatever directory you're within in the

I'm able to run it successfully by itself as a project, but I've developed it independently from the main project that it's to be used within. It has been installed in the main project but it can only be run where it's
exists because that's where my script alias resides.

There are other node modules that need to use the
file, but once I navigate to the other modules' root directories in
node wants to use that module's
and can't find the script alias since it doesn't exist there.

Adding the script alias to every module isn't a viable solution since the project is a prototype and constantly being added upon.

Is there a way for the relevant
to be referenced and used for the script alias regardless of where I'm running it from within the main project?

Am I going about solving this problem wrong? Any help would be greatly appreciated. This is my first question so if I've done anything wrong asking this, please let me know so I can improve. Thanks in advance.

Answer Source

A little bit late but an answer nonetheless.

We ended up going for what is peteb's approach (thanks) of just using it from the appropriate package (the one with the script alias internal to it) and feeding it the external filepath through an argument.

This works because the existing structure for the arguments was:

npm run <script> <arg1> <filepath>

Although the filepath argument was meant to be pwd or %CD%, the output of both is a normal filepath anyway. So any filepath would theoretically work, so long as you don't run into any other problems that would warrant looking for a solution to this problem elsewhere, like the length being too long for the filesystem and whatnot.

Something that might be worth mentioning is that maybe something like this would have been better suited to an environment other than NodeJS. It doesn't look like using a script outside the package it's internal to is a possibility and it might have been better to program an external program to deal with such issues.

I'm not as proficient at NodeJS as a lot of people here on the site are, but maybe an executable containing the code that is invoked by a .js file could work, if that's at all possible.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download