gpt4 book ai didi

javascript - NPM 模块 - 如何利用 package.json 文件?

转载 作者:行者123 更新时间:2023-11-30 15:24:38 25 4
gpt4 key购买 nike

我正在为基于 cli 的工具编写 NPM 模块。在有人安装包后,我需要读取他们的 package.json 文件。我知道如何用语法阅读文件。我的问题是,我怎样才能始终如一地检索 package.json 文件,我应该使用相对路径还是绝对路径。

因此,安装后,我的代码将驻留在 node_modules 文件夹中的某个位置:

package.json // I need to require this file
node_modules/
↳ my-package/
↳ dist/
↳ my-package.js // the actual file running the code

有什么建议/最佳做法吗?或者只是使用正确的文件路径的问题?

最佳答案

读取 package.json 文件非常简单,因为您可以简单地要求它:

var package = require('./path/to/package.json');

找到正确的道路并不是那么简单。

这是一项非常常见的任务,没有简单的方法可以通过核心模块来实现,但 npm 上几乎没有模块可以帮助您完成这项任务。

查找包 json:

Easily find package.json files that you're searching for. It spiders parent directories for package.json files and correctly reads the results. It follows an ES6 iterator design so it should be easy to implement.

父包json:

Using parent-package-json, you can find the parent package.json, so the package.json of the module that uses your module.

查找和读取包 json:

Find and read data from a package.json file.

参见:

关于javascript - NPM 模块 - 如何利用 package.json 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43182682/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com