Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running npm script on windows starting with a period

I have a package.json file with the following script defined:

"scripts": { "test": "./node_modules/selenium-cucumber-js/index.js" }

When I run npm test on linux or mac this script runs as expected. On Windows however I get an error:

/node_modules/selenium-cucumber-js/index.js '.' is not recognized as an internal or external command, operable program or batch file. npm ERR! Test failed. See above for more details.

However if I run the command ./node_modules/selenium-cucumber-js/index.js directly from a cmd prompt it works correctly. The same issue also occurs if I try to run any other script through npm that starts with a ".". I haven't been able to find any other thread talking about this as an issue.

I am running npm versions 5.6.0 on Windows 10 Home.

Does anyone know how I can get this working?

like image 480
Scotty Avatar asked Jun 23 '18 05:06

Scotty


People also ask

Why is my npm run start not working?

You need to make sure that the package. json file is present from where you run the npm start command. When you don't see the package. json file, then run the pwd command to see if you are in the right directory.

What script does npm start run?

So npm start runs the node script that is listed under start in the package. json. As in the article that cbr mentioned in a comment, in the case of create-react-app , this happens: A new instance of the WebpackDevServer from the library of the same name is created, passing in the compiler and configuration.

What is the difference between npm start and npm run start?

npm start is the short form for npm run start . So, its one and the same thing.

Does npm run on Windows?

js and NPM installations on Windows are relatively simple. You can install both from a download link. First, go to the Node installation page and download the installer. Choose the operating system you're using, and then go from there.


2 Answers

Since npm 5.1

npm config set script-shell "C:\\Program Files (x86)\\git\\bin\\bash.exe"  

or (64bit installation)

npm config set script-shell "C:\\Program Files\\git\\bin\\bash.exe"

Note that you need to have git for windows installed.

You can revert it by running: npm config delete script-shell

like image 54
yuner.bekir Avatar answered Oct 13 '22 11:10

yuner.bekir


Everything defined under scripts gets executed in the default system shell and on Windows ./node_modules/selenium-cucumber-js/index.js is definitely not a valid command (or rather a path). It just happens that the same file has a +x argument and a shebang pointing to a Node.js (or another JS interpreter) binary so that it gets executed on Linux without intervention.

I'm quite certain you'll get the same error if you were to execute the same command in the default Windows shell (cmd.exe) but you may get away with it in some ports/emulations of *nix shells (i.e. Cygwin, MSYS, bash.exe etc.) which may give you a false sense of everything working correctly outside of the npm chain.

If you want to make sure your script gets executed by Node.js while using relative paths and keeping cross-platform compatibility, call it explicitly as:

"scripts": {
    "test": "node ./node_modules/selenium-cucumber-js/index.js"
}

This will also take care of things like not having a proper x flag or shebang in the script you're executing and since Node.js is perfectly comfortable with using *nix paths on Windows it won't complain either.

like image 25
zwer Avatar answered Oct 13 '22 10:10

zwer