Gulp watch /tmp 权限被拒绝

Gulp watch /tmp permission denied

我在我的一个项目中使用 gulp。当运行ning gulp watch from OS (Arch Linux)

fs.js:921
  return binding.readdir(pathModule.toNamespacedPath(path), options.encoding);
             ^

Error: EACCES: permission denied, scandir '/tmp/systemd-private-c33e4391b18f4b24af3055190fb15730-systemd-hostnamed.service-zrxyaX/'
at Object.fs.readdirSync (fs.js:921:18)
at Gaze._addToWatched (/home/majmun/code/wp/wp-content/plugins/project/node_modules/glob-watcher/node_modules/gaze/lib/gaze.js:274:22)
at Gaze._internalAdd (/home/majmun/code/wp/wp-content/plugins/project/node_modules/glob-watcher/node_modules/gaze/lib/gaze.js:191:10)
at /home/majmun/code/wp/wp-content/plugins/project/node_modules/glob-watcher/node_modules/gaze/lib/gaze.js:400:16
at Array.forEach (<anonymous>)
at /home/majmun/code/wp/wp-content/plugins/project/node_modules/glob-watcher/node_modules/gaze/lib/gaze.js:396:12
at FSReqWrap.oncomplete (fs.js:153:20)

我 运行ning gulp 来自主机 OS 而不是来自虚拟机,因为它要快得多。

我知道问题是 gulp 正在主机 OS 的 /tmp 文件夹中执行某些操作,并且某些文件具有 root 权限。

如果我 运行 sudo chown -R majmun:users /tmp,之后会出现一个具有 root 权限的新文件,它会破坏 gulp。

为什么 gulp 手表需要 /tmp 文件夹。 有人解决了这个问题吗?

这里是 gulp watch 任务的代码 // gulp 观看

 Gulp.task('watch', ['dev'], function() {
        console.log('Initializing assets watcher:')

    let stream = new Stream();
    let filter = '*.{css,scss,sass,less,js,png,jpg,jpeg,svg}';
    let tmp = Tmp.fileSync();
    let glob = [
        path.src.assets + '**/' + filter,
        path.src.elements + '**/' + filter,
        path.src.properties + '**/' + filter,
    ];

    for (let i = 0; i < glob.length; i++) {
        console.log('- ' + glob[i]);
    }

    let watcher = Gulp.watch(glob, ['dev']);
    watcher.add(tmp.name);

    process.on('SIGINT', function() {
        stream.emit('end');
        process.exit(0);
    });

    KeyPress(process.stdin);
    process.stdin.on('keypress', function(char, key) {
        if (key && key.ctrl && key.name == 'c') {
            process.emit('SIGINT');
        }
        else if (key && key.ctrl && key.name == 'r') {
            Fs.utimes(tmp.name, new Date(), new Date(), function() {});
        }
    });
    process.stdin.setRawMode(true);
    process.stdin.resume();

    console.log('Watcher up and running, Ctrl+R to refresh, Ctrl+C to exit.');
    return stream;
});

我通过在使用 tmp 库时添加一些选项来解决这个问题。我没有指定默认选项,而是指定了临时文件夹的位置及其权限。

所以不是这个:

let tmp = Tmp.fileSync();

我这样做了:

let tmp = Tmp.fileSync({ 
    // Use settings from .env if defined 
    dir: process.env.MY_TMP_DIR || ''
    mode: process.env.MY_TMP_MODE || '', 
    prefix: process.env.MY_TMP_PREFIX || '', 
});