npm 为什么glob promise返回空字符串数组?

toe95027  于 2023-01-09  发布在  其他
关注(0)|答案(1)|浏览(104)

我只使用了typescript、ts节点、npm和路径
我只是尝试使用glob-promise返回由我的模式定义的文件的字符串数组(我导入的一个npm,它使用glob,但基于它的promise)。
我创建了一个定制的npm脚本来运行终端的ts文件,以显示下面package.json中的信息

{
"name": "glob-test",
"version": "1.0.0",
"description": "A Glob Test",
"scripts": {
    "build:glob": "ts-node --files globby.ts"
},
"dependencies": {
    "glob-promise": "^6.0.1",
    "path": "^0.12.7",
    "ts-node": "^10.9.1",
    "typescript": "^4.9.4"
}

}
下面是运行glob脚本的globby.ts文件

import * as path from 'path';
import glob from 'glob-promise';

const dir = path.dirname(__dirname);
    const txtURL = path.resolve(dir, 'glob-test', 'folder-area', '*.txt');

    glob(txtURL).then(function (cnt) {
    console.log('Path: ' + txtURL);
    console.log('Content: ', cnt);
});

我的项目目录如下所示:

我在我的终端输入npm run build:glob,但是它返回一个空数组,并且路径显示正确我不确定我做错了什么.我尝试使用path.join和path.resolve,但是都给予了相同的结果.它应该返回joke.txt文件.有人知道吗?

ltqd579y

ltqd579y1#

结果是path模块和glob-promise模块在我的windows环境中运行得不好,如果我使用一个普通的字符串,不管有没有concats,glob都会完美地返回我所要求的任何东西。
这似乎是Windows环境中最有可能的bug/问题或不兼容。尚未在Linux上测试。
例如:以下作品

import * as path from 'path';
import glob from 'glob-promise';

// const dir = path.dirname(__dirname);
const txtURL = path.resolve('folder-area', '*.txt');
const testURL = './folder-area/*.txt';

const returnTxtFiles = async () => {
const txtFiles = await glob(testURL);
console.log(txtFiles);
};

returnTxtFiles();

以上正确返回['joke.txt', 'file1.txt']

相关问题