5

I would like to run an npm script that contains cd folderName/ or cd ../www/folderName/ but it seems that npm is unable to run the cd command nor is it able to run the cp command (i installed cpx which seems to work well with it).

Any solution for a script that npm will run that allows me to change directories?

Thanks!

Update:

Ok this is one of the npm scripts that I need cd for ..

"buildWWW": "babel-node tools/build.js && cpx 'dist/*.{js,map,css,ico}' '../www/public' && cd ../www/ && node app.js"

replacing cd ../www/ && node app.js with node ../www/app.js is not an option.

Update #2: Simple test case

https://i.stack.imgur.com/WDS4c.jpg

penguinsource
  • 1,120
  • 3
  • 16
  • 40
  • Show us your code. – SLaks Jan 25 '18 at 18:26
  • `cd` works in npm-scripts here on Windows and macOS. Which OS are you running (Linux?) and what version of npm? – RobC Jan 25 '18 at 19:20
  • I'm on macOS, running npm v 5.6.0. So if I try a basic command such as "test": "cd /dist", it will run and do nothing; thus I assume it does nothing. can someone else reproduce this? See the images posted in Update #2 – penguinsource Jan 25 '18 at 20:12
  • 1
    Given the scenario shown in your screenshot - that’s normal behaviour, i.e. when the npm-script named `test` exits, the `pwd` reported will be tthe same as when you initially run `npm run test`. It simply returns back to the directory you started at. However, that test case, doesn’t represent your initial question. It should `cd` to `../www` and then run `node app.js` as per your `buildWWW` script example though. – RobC Jan 25 '18 at 21:01
  • @RobC I agree with both your points yes. my node js command does not execute though :(. thanks for the answer. – penguinsource Jan 27 '18 at 02:27
  • That's certainly odd. To check it's not a file permissions issue: Does running `node ../www/app.js` via the command line invoke the script? The only other thing worth a try, _(although not a normal a requirement of npm-scripts)_, is to make the script executable by running `chmod u+x ../www/app.js`, then try `npm run buildWWW`. Good luck! – RobC Jan 27 '18 at 12:47

0 Answers0