How to use package.json scripts to copy files with specific file extension

npm copyfiles
npm cp
npm cpx
npm script zip folder
npm script to create folder
npm scripts
gulp copy package json
npm zip folder

I am trying out npm as a build tool.

One stumbling block I have encountered is that I need to copy javascript files from one folder to another. The source folder contains typescript files, javascript files and map files, but in the target folder I am only interested in javascript files.

I do not want to make a copy-statement for each file, but would like to copy all .js files. Also, my source folder contains subfolders that also contains javascript files. These need to be copied as well, and maintain the subfolder structure.

What I have tried is using NCP with a filter, but I cannot get the filter to work. I have tested the regex used in the filter and it appears to work fine. The test was done at Regex Tester with regular expression .*\.js$ and test-strings like main.ts, main.js main.js.map etc, and only the .js strings were matched.

My package json contains the following (abbreviated):

{
    "scripts": {
        "copy": "ncp scripts wwwroot/scripts --filter=\".*(\\\\.js$)\"" 
    }, 
    "devDependencies": { 
        "ncp": "2.0.0.0" 
    }
}

Since my regex is in a string in a string I have double-escaped it. I have also tried other variations, for example:

--filter=/.*\.js$/g       - compilation error
--filter=/.*\\.js$/g      - no files copied
--filter=\".*\\.js$\"     - no files copied
--filter=\"/.*\\.js$/g\"  - no files copied
(no filter)               - all files copied

I am in no way married to NCP. If something else works better then I will use that.

So: How do I, inside package.json's scripts section copy only files with a speific extension to another folder? I am pretty sure I have overlooked something blindingly obvious...

Warning! The cpx package appears to be abandoned. cpy-cli, copyfiles, and other solutions are listed in comments here or answers, below.

cpx might be a good substitution.

It has a CLI, allows you to use globs instead of regex, can preserve the directory tree, and is relatively up-to-date as I write this....

Simple pure npm scripts build process · GitHub, "copy:html": "find src -name '*.html' -type f -exec cp {} dist \\;", "prebuild:dev": " npm run clean && npm run mkdirs && npm run copy", assets, when you delete a file (say, a .png one), the file still remains in the dist folder, right? want to continue making win specific versions of scripts that require it, and use an env. variable  npm - example - How to use package.json scripts to copy files with specific file extension package.json copy files to dist (2) I am trying out npm as a build tool.

There's also npm module called copyfiles https://github.com/calvinmetcalf/copyfiles

E.g. to copy all *.css files from the ./src folder to the ./styles folder:

copyfiles --flat src/*.css styles

copyfiles, Command Line. Usage: copyfiles [options] inFile [more files ] outDirectory. Options: -u, --up slice a path off the bottom of the paths [number]. Command-specific properties. The package.json file can also host command-specific configuration, for example for Babel, ESLint, and more. Each has a specific property, like eslintConfig, babel and others. Those are command-specific, and you can find how to use those in the respective command/project documentation. Package versions

Quick compatibility build script (also works on Windows):

"build": "react-scripts build && mv build docs || move build docs",

copy-files-from-to, Copy files from one path to another, based on the instructions provided in a configuration file. to copy the scripts out of node_modules / bower_components folder; You may like to use this tool if you prefer In your package.json file, add the "copyFiles" and "copyFilesSettings" (optional) Data type: string, array, or object. npm - files - package.json example How to use package.json scripts to copy files with specific file extension (2) cpx might be a good substitution. It has a CLI, allows you to use globs instead of regex, can preserve the directory tree, and is relatively up-to-date as I write this.

You can use gulp.js for this. Write a gulp task to isolate only the js files (/path/to/files/*.js) and move it to destination of your choice. It will require only a few lines of code. Include that in the script section of package.json if necessary.

The link to gulp.js : https://gulpjs.com/

var gulp = require('gulp');
gulp.task('jscopy', function(){
  return gulp.src('client/templates/*.js')
    .pipe(gulp.dest('build/js'))
});

Copying extra files into the directory you publish • Surge, When using npm run scripts as your build tool, you can add an npm run script that will automatically move your CNAME file or other folder from one directory to  When there is no .npmignore file, the contents of .gitignore will be used. So be careful when adding generated directories like dist/to your .gitignore as they might end up not being in your production tarball. When that happens, you can use the "files" array in package.json to include them, as includes from that file will never be excluded.

ncp copies recursively, therefore before copying files it will check whether directory matches filter or not, e.g.:

d:\path\to\app\src\server
d:\path\to\app\src\server\middleware
d:\path\to\app\src\server\middleware\cool-middleware.js
d:\path\to\app\src\server\middleware\cool-middleware.js.map

So your regex must matches all these paths and your file also

You, me and package.json - DEV, For this, is the use of lock files is recommended. Anyone that gets a copy of the project, now and forever (as long as the package hasn't been unpublished), will Before creating a package.json file, there's one rule to know: The file must be in a valid JSON format and must abide by the JSON style spec. I am trying to copy all .doc files that I have scattered throughout several subdirectories of one main directory into another directory using a batch file. I have managed to get a filelist.txt of all the files (there are hundreds) out of these directories that I want to copy using:

Config Files · Babel, .babelrc.json files, with the different extensions; package.json files with a a babel.config.json file, or an equivalent one using the supported extensions, . babelrc packages/ mod1/ package.json src/index.js mod2/ package.json src/ index.js. Copy If all of your build scripts run relative to your repository root, things should  You can use this script to copy files from one folder to another. The files you want to copy should be specified in the text file that you specify using the strFileList constant. Use the constants to specify source folder, target folder and if files should be overwritten or not if they already exist at the target.

Workspace and project file structure, The workspace root folder contains various support and configuration files, and a See npm documentation for the specific format and contents of this file. When the workspace file structure is in place, you can use the ng generate and CSS files when building your app, so you typically don't need to add any <script> or  If there is no destination identified , it defaults to the present working directory. copy c:\lists.txt Rem The file lists.txt will be copied from C:\ to C:\tp location copy C:\lists.txt c:\tp Rem Quotation marks are required if the file name contains spaces copy “C:\My File.txt” Rem Copies all the files in F drive which have the txt file

package.json Scripts, Learn package.json Scripts as part of the A$AP GraphQL Server with NodeJS and --copy-files && npm run build:copy", "build:copy": "copyfiles package.json . Luckily for us and most developers, one would never use this stuff, other than the which in our case is babel-node execution of the src/server.js file, the entry   The following script has three parameters that need to be specified: $source = The source directory of the files. $destination = The destination to where they will be copied. $files = The parameter (s) that the file names need to meet in order to be copied. In the example below, files need to have either the word “research” in their name or or the .xlsx file extension to be copied.

Comments
  • Perfect. Now my script is "copy": "cpx \"scripts/**/*.js\" wwwroot/scripts", with the devDependency "cpx": "1.3.1". It is exactly what I was looking for.
  • Yeah, cpx is a really great tool. Good recommendation.
  • At now cpx seems to be abondened and has open vulnerabilities in the dependencies. I would suggest to use cpy-cli based on cpy.
  • I think ncp might be a good substitute since cpx now seems to be abandoned. It's async and does recursive file and directory copying.
  • The last release was 3 years ago. Looks like it is abandoned. It has vulnerabitlies
  • Why not just cp -R? I'm using cp -R ./email/templates ./build/email and just curious about your solution.
  • @Woppi my reasoning would be, because it uses OS commands, which might not work on other OS's.
  • @Coo I see. Thanks a lot for sharing your reasoning. Appreciate it.
  • Adding Gulp as a dependency just for copying files is a bit overdone. If you already/still use gulp, it could be an option.
  • Yeah if someone is using something like webpack they wouldn't want to add gulp to just use copy.
  • package.json files imply a project that could be run on any OS that node supports. PowerShell does not support all of the same operating systems.