gpt4 book ai didi

javascript - 遍历 Node 中的目录 (Grunt)

转载 作者:行者123 更新时间:2023-12-03 12:23:12 25 4
gpt4 key购买 nike

构建 grunt plugin 时遇到奇怪的问题.

基本上,我想在我的 grunt 任务本身中使用 node_module 。为此,我想向上遍历一层,然后向下遍历 Node 模块以专门调用它们的一个文件。

最初,我想这样做:

../node_modules/github-changes/bin/index.js

但是,我收到以下错误:

Warning: Command failed: /bin/sh: 1: ../node_modules/github-changes/bin/index.js: not found
Use --force to continue.

所以,现在我有一个使用 Node 的 __dirname 变量的技巧,但它不是很漂亮:

var dirHack = __dirname.replace("/tasks", ""), // Terrible hack, need to fix
ghC = dirHack + '/node_modules/github-changes/bin/index.js';

这可行,但我很想避免它。

您可以看到行 here .

缺少什么导致该错误?我在这里遗漏了什么吗?

最佳答案

如果github-changes是一个依赖项,您可以使用 ghC = require.resolve('github-changes/bin/index.js') 解析包内文件的路径.

但是您收到该错误是因为您尝试将该文件作为 shell 脚本运行,但事实并非如此。您需要使用 Node 来运行它。查找 Node 可执行文件路径的最简单方法是 process.execPath 。请参阅http://nodejs.org/api/process.html#process_process_execpath

这是一个例子:

var exec = require('child_process').exec;
var ghC = require.resolve('github-changes/bin/index.js');
exec(process.execPath + ' ' + ghC, function(error) {});

关于javascript - 遍历 Node 中的目录 (Grunt),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24346751/

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