1

I am using Node.js v6.3.1 and ncp v2.0.0

I can only get ncp to copy the contents of a directory, but not a single file within that directory.

Here is the code copying the contents of a directory recursively that works:

var ncp = require("ncp").ncp;
ncp("source/directory/", "destination/directory/", callback);

...and here is the same code but with a file as the source:

var ncp = require("ncp").ncp;
ncp("source/directory/file.txt", "destination/directory/", callback);

From this all I can think is that ncp was specifically designed to copy directories recursively, not single files maybe?

I had thought about using something like fileSystem's read/write stream functions as described here but really for consistency I was hoping to stick with ncp.

Update:

I have found another package called node-fs-extra which does what I want without the need for me to add event handlers to the operations, like I would have to do with the fileSystem read/write solution.

Here is the code that is working:

var fsExtra = require("fs-extra");
fsExtra.copy("source/directory/file.txt", "destination/directory/file.txt", callback);

Obviously this still is inconsistent, but at least is a little less verbose.

Community
  • 1
  • 1
Jeremy
  • 2,959
  • 1
  • 29
  • 38
  • Looking at their source code, they do appear to be able to copy just one file. I don't think it matters, but for your awareness you don't have to call '.ncp()' on the module, you could just `require('ncp')` and the resulting object is the same as calling `.ncp()` on it, for... reasons. Their code is a tad fugly. – Paul Jul 28 '16 at 13:12
  • @Paul could you provide an example of the syntax so I know where I'm going wrong with the usage, thanks. – Jeremy Jul 28 '16 at 13:13
  • It's the syntax you were using still. They literally set: `module.exports = ncp; ncp.ncp = ncp;` No idea why. So it'd still be something like `const ncp = require('ncp'); ncp(src, dest, cb);` – Paul Jul 28 '16 at 13:15
  • Sorry I should have been clearer, I meant could you provide an example of copying a single file using ncp. the above syntax is not working for me. Thanks. – Jeremy Jul 28 '16 at 13:19
  • I understand that it's not working; what I'm saying is that there is no different syntax for a single file. They use fs.stats under the hood to determine if you're grabbing a single file or directory, then call a different private function for each case. This is also comments not an answer b/c I don't see a good reason that your code isn't working based on what I'm seeing in their source. – Paul Jul 28 '16 at 13:24
  • ok thanks, I'll have a closer look again at the source. – Jeremy Jul 28 '16 at 13:32

2 Answers2

4

Ok I have figured out what I was doing wrong.

I was trying to copy a file into a directory, where as I needed to copy and name the file inside a directory.

So here is my original code that does not work:

var ncp = require("ncp");
ncp("source/directory/file.txt", "destination/directory/", callback);

...and here is the fixed code working, notice the inclusion of a file name in the destination directory:

var ncp = require("ncp");
ncp("source/directory/file.txt", "destination/directory/file.txt", callback);

So it looks like ncp wont just take the file as is, but needs you to specify the file name at the other end to successfully copy. I guess I was assuming that it would just copy the file with the same name into the destination directory.

Jeremy
  • 2,959
  • 1
  • 29
  • 38
1

I have found another package called node-fs-extra which does what I want without the need for me to add event handlers to the operations, like I would have to do with the fileSystem read/write solution.

Here is the code that is working:

var fsExtra = require("fs-extra");
fsExtra.copy("source/directory/file.txt", "destination/directory/file.txt", callback);

Obviously this still is inconsistent, but at least is a little less verbose.

Jeremy
  • 2,959
  • 1
  • 29
  • 38