Learn how to solve the NPM error 'cp' is not recognized as an internal or external command, operable program or batch file

Many developers use open source plugins and projects to speed up the development process of their projects. Sometimes, many of those projects were made in unix based platforms (not everybody uses windows my friend), so when you download a project for example made with Node.js and tools to build like NPM, you will find some problems when using it in a distinct platform from where it was originally made.

In this case, if you see the NPM error "cp" is not recognized as an internal or external command, the project was surely developed in Mac, Linux etc. For example, if you try to run the build command of the following script (package.json):

{
    /* Rest of your package.json file */
    "scripts": {
        "build": "babel-node -- node_modules/webpack/bin/webpack -p --config tools/webpack.config && cp demo\\assets\\js\\form-* dist\\",
    },
    /* Rest of your package.json file */
}

It will throw the exception in Windows as the cp command doesn't exist in Windows.

Solution 1

Fortunately, the problem is just a syntax incompatibility between Windows and Unix based systems, so changing manually the cp command with copy will fix the problem:

{
    /* Rest of your package.json file */
    "scripts": {
        "build": "babel-node -- node_modules/webpack/bin/webpack -p --config tools/webpack.config && copy demo\\assets\\js\\form-* dist\\",
    },
    /* Rest of your package.json file */
}

Now the previous command should work in Windows. The only problem is the maintainability of the project, as you will always need to change this when you are working in windows, however comes in handy if you are working only for you or you are not intending to commit any changes to the original repository of the project.

Solution 2

You can as well install Cygwin on Windows to provide support for the "cp" command. Cygwin is a large collection of GNU and Open Source tools which provide functionality similar to a Linux distribution on Windows. The most recent version of the Cygwin DLL till the date of publication of this article is 2.9.0. Install it by running setup-x86.exe (32-bit installation) or setup-x86_64.exe (64-bit installation). Use the setup program to perform a fresh install or to update an existing installation.

After installing Cygwin, closing the command prompt and restarting it will allow you to run the build command without a problem using the cp command of unix, so you wouldn't need to modify the package.json file manually.

Happy coding !

This could interest you

Become a more social person