简体   繁体   中英

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

I've created a node module that produces a JSON file with data from multiple APIs. It's designed to be able to save the JSON file into whatever directory you're within in the CMD/Terminal .

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 package.json exists because that's where my script alias resides.

There are other node modules that need to use the JSON file, but once I navigate to the other modules' root directories in CMD/Terminal node wants to use that module's package.json 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 package.json 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM