简体   繁体   English

我可以使用另一个节点模块的package.json中存在的脚本别名吗?

[英]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.

相关问题 节点找不到在 tsconfig 和 package.json 中定义的模块别名 - Node unable to find module alias defined in tsconfig and package.json Node 模块的 package.json 中没有定义“exports” main - No "exports" main defined in Node module's package.json 在其中一个脚本中使用package.json的version属性 - Use version attribute of package.json in one of it's script 如何通过 package.json 脚本使用默认程序打开文件 - How can I open a file with it's default program via a package.json script 如何在package.json中的脚本中添加时间戳? - How can I add a timestamp into a script in package.json? 在 package.json 脚本中,我如何将参数传递给 node,而不是脚本? - In a package.json script, how do I pass an argument to node, as opposed to the script? 使用package.json脚本运行另一个package.json脚本 - Using package.json script to run another package.json script .min或src作为node_module的package.json中的“main”-file - .min or src as “main”-file in package.json for node_module 比较本地节点模块版本与package.json版本 - Compare local node module version with package.json version 弃用警告:在 node_modules\postcss\package.json 的 package 的“导出”字段模块解析中使用弃用的文件夹映射“./” - DeprecationWarning: Use of deprecated folder mapping "./" in the "exports" field module resolution of the package at node_modules\postcss\package.json
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM