0

I have a script which scaffolds an app. This involves making a new directory in the current directory inside which the app's scaffold files will be unpacked.

At the end of my script I run a series of npm/node commands as follows (appDir is a reference to the child dir created to host the app; it exists by the time these commands run):

const postInstCmds = [
    'cd '+appDir,
    'npm init -y',
    'npm install add-npm-scripts --save-dev',
    'npm install http-server --save-dev',
    'add-npm-scripts server "http-server"',
    'npm run server'
];
const execSync = require('child_process').execSync;
postInstCmds.forEach(cmd => execSync(cmd));

However it seems the first cd command is being ignored; the subsequent commands are being executed in the parent, not child, directory.

I've read this, which explicitly says that npm supports cd * operations, but perhaps I'm misunderstanding something (I'm no npm wizard). I've also tried using npm's --prefix argument to specify path, but no joy.


[UPDATE]

The linked dup suggests npm start <dir> but this seems to work only if <dir> already contains a package.json. As per my sequence of commands, I want to move to the directory and then create the package.json (via npm init). Another answer there suggests npm run --prefix, but run again requires an extant pacakge.json, since it reads from the scripts object.

I have also tried npm init --prefix <dir> but the arg doesn't seem to be supported for npm init.

Mitya
  • 33,629
  • 9
  • 60
  • 107
  • 1
    Does this answer your question? [How can I get npm start at a different directory?](https://stackoverflow.com/questions/36172442/how-can-i-get-npm-start-at-a-different-directory) – jonrsharpe Jul 10 '21 at 13:42
  • Thank you - I didn't find that during my searching. I will go read. – Mitya Jul 10 '21 at 13:43
  • @jonrsharpe I've updated my question after studying your linked dup. Thanks for any help you're able to offer. – Mitya Jul 10 '21 at 13:50
  • Yes; the prefix has to point to a package directory. Also I believe every time you exec it starts again, so the cd doesn't "stick". But that's kind of an awkward way to approach it; why not copy a preset package file with the right script and dependency into the directory, then prefix will work for install and run? What's the bigger context here? – jonrsharpe Jul 10 '21 at 14:25
  • Yeah that's pretty much the conclusion I've come to, too. I'll have my bin script create the package.json by writing the file manually to the directory. I was just trying to harness `npm init` but it doesn't seem like you can get that to happen in a non-cwd directory. Thank you. – Mitya Jul 10 '21 at 15:01

1 Answers1

1

Your code is running different child processes for each command. So the cd command runs in a subprocess and exits. Then the next command runs in a new subprocess that is in the original directory you started in and not in the directory that the cd command navigated to.

One fix would be to get rid fo the cd command and instead pass the directory as a cwd option for each execSync() call.

postInstCmds.forEach(cmd => execSync(cmd, {cwd: appDir}));
Mitya
  • 33,629
  • 9
  • 60
  • 107
Trott
  • 66,479
  • 23
  • 173
  • 212