change directory command cd ..not working in batch file after npm install
Solution 1:
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.
- The current directory is
C:\Temp\HDC
. - The command
pushd hui-components-style
savesC:\Temp\HDC
on stack and sets as new current directoryC:\Temp\HDC\hui-components-style
. -
npm
is executed which modifies the current directory. - The command
popd
getsC:\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:
- It pushes path of current directory on stack.
- It pushes state of command extensions on stack.
- It pushes state of delayed expansion on stack.
- It pushes the memory address of the current environment variables table on stack.
- It creates a copy of the current environment variables table in memory and makes active this new environment variables table.
Those five steps are always done even with setlocal
being called with one or two of the four 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 execution environment don't matter after next command endlocal because endlocal
- deletes the current environment table;
- pops memory address of previous environment table from stack and uses this address resulting in restoring initial environment variables;
- pops state of delayed expansion from stack and disables/enables delayed expansion accordingly;
- pops state of command extensions from stack and disables/enables command extensions accordingly;
- 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 execution environment based on current environment.
- endlocal ... end local execution environment and restore previous environment.