How to reference a project root directory in an npm script

Mohamed Lamine Allal
3 min readJun 6, 2020

You may find yourself in a situation where you need to change directory (cd) to a certain directory! Execute a command ! Then cd again to root! To execute another command that need to be done in root as for example for gulp!

To illustrate here an example

"tas:adminApp:build": "cd src/KidoService/AdminApp && npm run build && gulp build_copyAdminApp",

This will fail for the gulp execution! We need to go back to the project root folder! And if we don’t. Trying to run gulp will throw an error because it need to be run on root! And gulp here is just an example.

(Update: gulp here was just an example! It’s to be noted! Gulp in the latest version automatically do the backward traversing and check for any gulpfile! The first found will be used! So in the newer version the above will not fail! But it stay a good example to illustrate the problem (just think of it as the old version))

In this quick writing! I’m going to show you the solution to it! But too another interesting technique that allow more!

And if you were searching that then you are in the right place! And if you are just reading! You may thank me in the future!

The answer is simply

INIT_CWD

We can reference the root directory of the project through the environment variable `INIT_CWD` that npm set for us!

From the doc :

Scripts are run from the root of the module, regardless of what your current working directory is when you call npm run. If you want your script to use different behavior based on what subdirectory you’re in, you can use the INIT_CWD environment variable, which holds the full path you were in when you ran npm run.

https://docs.npmjs.com/cli/run-script

Example:


“tas:adminApp:build”: “cd src/KidoService/AdminApp && npm run build && cd $INIT_CWD && gulp build_copyAdminApp”,

And we are cool! npm have it all set for us!

Second Method (Env var)

And just because i love to think too much! I went and asked myself

What if we didn’t have such a variable set for us by npm !?

After thinking! The answer is we can do a lot of things!

For example setting shell or any type scripts and call them! is an option! But not that cool!

And came to me one of the solutions is that we can execute an env variable set command at all start!

export ROOT_DIR=$PDW

for linux for example.

set VAR=VALUE

for windows. (Know that i didn’t test it on windows ! )

And then when we need it ! We can use it!

Our first example will become


“tas:adminApp:build”: “export ROOT_DIR=$PDW && cd src/KidoService/AdminApp && npm run build && cd $ROOT_DIR && gulp build_copyAdminApp”,

You can see it’s a nice technique ! We can use that to reference some directories that we want to go back to! That are not the root one!

So it’s just more extensible! And simple to caste ! And it’s always nice to have quick castable tools and moves as cool magician do!

And it’s always nice to think!

I hope that was helpful!

Inspiration (out of subject)

As i’m a person that like to think a lot! And also care and kind of obsessed about performance as a concept and a life! I lose myself easily to the immersion into the worlds of all strings of thoughts! Here a nice inspirational line!

It’s always great to think and go beyond! That far from our context here! And in all!

Otherwise for us! We tend to search for the solution directly or we can! And it’s nice as it can be fast and productive and even necessary! And also we get to know what the tools offers and the doc have for us!

But it’s cool too to think and bring up solutions! Ask yourself and think further! And more too if the search come short! Or taking time! Snapping out of it and thinking for ourselves! Can be both time saving! And too an amazing process!

Setting such a small micro block in our core! And make it an at all times thing! Can bring a lot to our system!

And in the end thinking should never stop! And so does growth!

--

--

Mohamed Lamine Allal

Developper, Entrepreneur, CTO, Writer! A magic chaser! And A passionate thinker! Obsessed with Performance! And magic for life! And deep thinking!