change - node.js command prompt commands



change directory command cd..not working in batch file after npm install (1)

I have the following command in one bat file.

echo STEP12
cd HDC
git config --global url."https://".insteadOf git://

echo STEP13
cd hui-components-style
npm install --registry http://localhost:23510
cd ..

In STEP13, after npm install command cd.. is not working. Its not going back to the parent HDC folder. i have other commands to run in the parent folder.Am I making some syntax errors?


npm is on Windows a Windows batch script with file extension .cmd and not an executable which in this case modifies current directory and does not restore it before exiting.

I suggest to use instead of

cd hui-components-style

the command

pushd hui-components-style

and use instead of

cd ..

the command

popd

For details on the two commands – push and pop directory – open a command prompt window and run pushd /? and popd /? to get displayed the help for each command.

An explanation for better understanding using absolute paths.

  1. The current directory is C:\Temp\HDC .
  2. The command pushd hui-components-style saves C:\Temp\HDC on stack and sets as new current directory C:\Temp\HDC\hui-components-style .
  3. npm is executed which modifies the current directory.
  4. The command popd gets C:\Temp\HDC from stack and sets this directory as current directory independent on which directory is the current directory.

So the code with those two modifications is:

echo STEP12
cd HDC
git config --global url."https://".insteadOf git://

echo STEP13
pushd hui-components-style
call npm.cmd install --registry http://localhost:23510
popd

It is necessary to use command call because of npm is a batch file with complete file name npm.cmd and not an executable, i.e.

call npm.cmd install --registry http://localhost:23510

Otherwise the command processing of the current batch file is continued on npm.cmd and whatever commands are in current batch file after the line with npm are never processed by Windows command processor. For details about the various methods to execute a batch file see answer on How to call a batch file that is one level up from the current directory? . And see also answer on copy command in batch file is not getting executed when calling the batch file from another batch file, but is getting executed when I double click .

Alternatively it would be also possible to use following code:

echo STEP12
cd HDC
git config --global url."https://".insteadOf git://

echo STEP13
cd hui-components-style
setlocal
call npm.cmd install --registry http://localhost:23510
endlocal
cd ..\

The command setlocal does following:

  1. It pushes path of current directory on stack.
  2. It pushes state of command extensions on stack.
  3. It pushes state of delayed expansion on stack.
  4. It pushes the memory address of the current environment variables table on stack.
  5. It creates a copy of the current environment variables table in memory and makes this new environment variables table active.

Those 5 steps are always done even with setlocal being called with 1 or 2 of the 4 possible options EnableExtensions , DisableExtensions , EnableDelayedExpansion , DisableDelayedExpansion to additionally change state of command extensions and/or delayed environment variable expansion.

Now batch file npm.cmd can change the current working directory, can add, delete and modify environment variables, can enable/disable command extensions, and can enable/disable the usage of delayed expansion.

But all those modifications on command processing environment don't matter after next command endlocal because endlocal

  1. deletes the current environment table;
  2. pops memory address of previous environment table from stack and uses this address resulting in restoring initial environment variables;
  3. pops state of delayed expansion from stack and disables/enables delayed expansion accordingly;
  4. pops state of command extensions from stack and disables/enables command extensions accordingly;
  5. pops previous current directory path from stack and sets current directory to this path to restore the current directory.

For examples which demonstrate that see the answers on

  • Why is my cd %myVar% being ignored? (example for current working directory management)
  • Echoing a URL in Batch (example for environment variable and delayed expansion management)

The names of the two commands are actually self-explaining:

  • setlocal ... setup local command process environment based on current environment.
  • endlocal ... end local command process environment and restore previous environment.




npm