1

Is there a way to run an npm script on every Meteor server restart?

I tried the postinstall hook but it runs only at the first local application start.

I assume, that there must be way, because the restart triggers several building processes and some of them must be "hookable".

I was first thinking of using build plugins, but it seems, that they move a lot of config away from my package.json then.

Anyone knows something about this?

1 Answer 1

1

You can run your npm script within your Meteor.startup() code on the server side. The following example which should be located under the /server folder might help.

import { exec } from 'child_process';

Meteor.startup(() => {
   async function sh(cmd) {
     return new Promise(function (resolve, reject) {
       exec(cmd, (err, stdout, stderr) => {
         if (err) {
           reject(err);
         } else {
           resolve({ stdout, stderr });
         }
       });
     });
   }

   async function excScript() {
     let { stdout } = await sh('npm -ls'); // runs "npm -ls"
     for (let line of stdout.split('\n')) {
       console.log(`npm -ls: ${line}`);
     }
   } 
   excScript();
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks a lot. It seems, that this goes in a right direction. However, there are some errors. First it can't reach the script, because in development the folder of the local build is {projectpath}/.meteor/local/build/programs/server which does not contain the original package.json file. Is there a way to call the script "some dirs above"? The second issue is the following error message: (node:56566) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Do you mean when it runs npm -ls you get "command not found error" ? If so, it is related with your environment setup. You should add "npm_bin_path" to your PATH variable in your production server. The one related with the usage of Promise is not an error but a deprecation warning, you can omit it for now.
Thank you. I am not running on windows, so no PATHvariables. Also I just want to use the scripts in development mode. The command not found error comes from the current directory being not my project directory but the local build in the .meteor folder, where the package.json with my script is not present (thus the error). Regarding depracation warning, I would not like to omit deprecated functions, since there is a reason why they are deprecated (it may not be good to deploy a new application with already deprecated features). Are there any other options to solve this? Best, Jan

Your Answer

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.