Node的fs.symlink()认为链接已经存在; 绝对没有

Mat*_*her 4 fs node.js

我有这个代码:

// ...

var symlinkPrecommit = function (callback) {

  console.log("\n > Creating pre-commit symlink in .git/hooks/pre-commit\n");

  source = path.resolve('.git/hooks/pre-commit');
  target = path.resolve('precommit.js');

  fs.symlink(source, target, 'file', function (err) {
    if (err) {
      console.log(
        err.code === 'EEXIST' ? "Link already created!\n" : "Error\n"
      );
      console.log(err);
    }
    if (callback) callback();
  });

}

// ...
Run Code Online (Sandbox Code Playgroud)

当我运行它时,我得到了这个:

MaffBookPro: matt$ ./build.js

 > Creating pre-commit symlink in .git/hooks/pre-commit

Link already created!

{ [Error: EEXIST, symlink '/Users/matt/work/project/.git/hooks/pre-commit']
  errno: 47,
  code: 'EEXIST',
  path: '/Users/matt/work/project/.git/hooks/pre-commit' }
Run Code Online (Sandbox Code Playgroud)

但是,当我ls -lah尝试创建符号链接的目录时,该文件肯定不存在 - 并且权限看起来完全正常:

MaffBookPro: matt$ ls -lah .git/hooks/
total 80
drwxr-xr-x  11 matt  staff   374B  9 Nov 15:39 .
drwxr-xr-x  15 matt  staff   510B  9 Nov 15:45 ..
-rwxr-xr-x   1 matt  staff   452B  9 Oct 12:52 applypatch-msg.sample
-rwxr-xr-x   1 matt  staff   896B  9 Oct 12:52 commit-msg.sample
-rwxr-xr-x   1 matt  staff   189B  9 Oct 12:52 post-update.sample
-rwxr-xr-x   1 matt  staff   398B  9 Oct 12:52 pre-applypatch.sample
-rwxr-xr-x   1 matt  staff   1.6K  9 Oct 12:52 pre-commit.sample
-rwxr-xr-x   1 matt  staff   1.3K  9 Oct 12:52 pre-push.sample
-rwxr-xr-x   1 matt  staff   4.8K  9 Oct 12:52 pre-rebase.sample
-rwxr-xr-x   1 matt  staff   1.2K  9 Oct 12:52 prepare-commit-msg.sample
-rwxr-xr-x   1 matt  staff   3.5K  9 Oct 12:52 update.sample
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?提前致谢!

Mik*_*keB 7

这对我来说是违反直觉的,因为我想象了src - > dst,但在: fs.symlink(srcpath, dstpath[, type], callback)

  • srcpath 是链接所指向的
  • dstpath 是创建新链接的位置

它的命名方式与unix ln命令相同,如果您不考虑正在构建的链接,它会更容易记住,而是记住它具有相同的接口cp,其中源是现有文件,目标是哪里将创建新文件.