Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I run an npm script of a dependent package

Tags:

npm

I have a package that itself has a script in its package.json that I would like to be able to run in my top-level project. This package is one of my top-level projects dependencies. I'm looking for a way to directly or indirectly call the dependency packages script.

Let us assume the module name I'm working with is named foo and the script I want to run is updateFooData.

I tried using the syntax npm run-script <package> <...> to run it, but this appears to be deprecated functionality as I can't find it in the current official documentation but I see it in other (very old) search results.

npm run-script foo updateFooData

# npm ERR! missing script: foo

I also looked into the npm api and while npm.commands.run-script(args, callback) will do what I want, I can't figure out how to load the module into npm

{
  ...
  "scripts":{
    "foo:updateFooData": "node --eval \"... ??; npm.commands.run-script('updateFooData', callback)\""
  }
}

npm run foo:updateFooData
# Obviously fails

The only thing I've found that works so far is to CD into the submodule directory and run npm from there. This is not the preferred solution for me.

cd node_modules/foo
npm run updateFooData
like image 772
Nate Avatar asked Jun 22 '15 21:06

Nate


People also ask

How do I run a custom npm script?

To define an NPM script, set its name and write the script under the 'scripts' property of your package. json file: To execute your Script, use the 'npm run <NAME-OF-YOUR-SCRIPT>' command. Some predefined aliases convert to npm run, like npm test or npm start, you can use them interchangeably.

How do I use npm dependencies?

To add dependencies and devDependencies to a package. json file from the command line, you can install them in the root directory of your package using the --save-prod flag for dependencies (the default behavior of npm install ) or the --save-dev flag for devDependencies.

How do I run a script after npm install?

You can use npm hook scripts to do something after package is installed. Create node_modules/. hooks/postinstall executable and it will be run also after npm install <package> .


1 Answers

I ran into this trying to run the updatedb script for geoip-lite. You should use the npm explore command which will spawn a new shell in a dependencies' directory.

So for your use case, try npm explore foo -- npm run updateFooData

like image 163
Cezary Wojtkowski Avatar answered Sep 30 '22 19:09

Cezary Wojtkowski