[英]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. 我创建了一个节点模块,该模块使用来自多个API的数据生成
JSON
文件。 It's designed to be able to save the JSON
file into whatever directory you're within in the CMD/Terminal
. 它旨在将
JSON
文件保存到您在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. 它已安装在主项目中,但它只能在
package.json
存在的地方运行,因为这是我的脚本别名所在的位置。
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. 还有其他节点模块需要使用
JSON
文件,但是一旦我导航到CMD/Terminal
节点中的其他模块的根目录,想要使用该模块的package.json
并且找不到脚本别名,因为它没有存在于那里。
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? 是否有一种方法可以引用相关的
package.json
并将其用于脚本别名,无论我在主项目中运行它的位置如何?
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. 我们最终选择了peteb的方法(谢谢),只需从相应的包(具有脚本别名内部的包)中使用它,并通过参数将外部文件路径提供给它。
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. 尽管
filepath
参数意味着是pwd
或%CD%
,但两者的输出仍然是普通的文件路径。 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. 可能值得一提的是,类似这样的东西可能更适合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. 我不像网站上的很多人那样精通NodeJS,但是如果可能的话,可能包含由
.js
文件调用的代码的可执行文件也可以工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.