0

I have a main script (publish-all.js) from which I want to invoke the npm publish script of an Angular project, which also has a sub-script (publish.js that does general stuff (creating folders, copying files, moving folders...) after ng build.

I need to pass some environment variables to that second script. I am using shelljs to run unix-like commands.

I tried using:

npm run publish -- VERSION=${productVersion} DESTDIR=${productDestinationPath}

From publish-all.js where productVersion and productDestinationPath are constants declared above that line, and which invokes the following script from the package.json:

"publish": "ng build --prod && node ./scripts/publish.js"

But the actual command line I get is

ng build --prod && node ./scripts/publish.js "VERSION=value" "DESTDIR=value"

Finally, in my publish.js script I tried getting those variables the following way:

let version = process.env.VERSION;
let destinationPath = process.env.DESTDIR;

But I get undefined values.

What am I doing wrong? Is the a better way of doing all this?
Should I maybe use process.argv instead??

I am using this strategy because it is what I were told to do, but I would like to know if there is a less confusing way.ç


EDIT 2021年07月13日

I tried using export (with shelljs, since I am in Windows and using powershell) but I am getting an exception. I have the following code in publish-all.js now:

shelljs.exec(`export VERSION=${productVersion}`);
shelljs.exec(`export DESTDIR=${productDestinationPath}`);
shelljs.exec('npm run publish');

And in the publish.js script from the ANGULAR project:

version = process.env.VERSION;
destinationPath = process.env.DESTDIR;

Though it does not get to publish.js. It gets stuck in the shelljs.exec('npm run publish'), with the following exception:

Exception screenshot, which says: Error: ENOENT: no such file or directory, stat 'project folder/node.exe'

I had to hide the project folder because of privacy policies, but it is a subfolder inside the folder where I am executing publish-all.js.

asked Jul 12, 2021 at 7:39
2
  • If you're trying to set environment variables they go first, currently you're passing them to what would be process.argv. Commented Jul 12, 2021 at 7:41
  • @jonrsharpe So I should use instead VERSION=${productVersion} DESTDIR=${productDestinationPath} npm run publish? Thank you for the clarification! Commented Jul 12, 2021 at 7:47

1 Answer 1

1

Environmental variables go BEFORE the command. So, instead of passing them after you can add them BEFORE:

VERSION=${productVersion} DESTDIR=${productDestinationPath} npm run publish

Or, You can export the variables first then run the script:

export VERSION=${productVersion}
export DESTDIR=${productDestinationPath}
npm run publish
answered Jul 12, 2021 at 8:01
Sign up to request clarification or add additional context in comments.

3 Comments

Hello, thank you for your answer. I updated the question because I got stuck again. If you could look at it I would really appreciate it.
feel free to ask a different question cause the old question and new question has very less common.
to accept this answer press the gray tick below the arrows

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.