I am used to run yarn
/ npm
and then the command:
yarn build-me
or
npm run build-me
This calls a script command declared in the the local package.json
root file.
In my case I'd like to run a script command declared in the package.json
of a package in node_modules
folder.
How can I do this?
I have installed a test package i wrote inside app. Here is the dependency (in my root app) and I can see it under node_modules
"dependencies": {
"@tester/util-package": "^1.0.2"
}
Under node_modules this util-package also has it's own package.json with the following script
"scripts": {
"process-files": "node ./cli.js"
},
So, I was hoping to run this from the root of my app something like.
yarn util-package process-files
Of course, this doesn't seem to work.
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.
Scripts are stored in a project's package. json file, which means they're shared amongst everyone using the codebase. They help automate repetitive tasks, and mean having to learn fewer tools. Node npm scripts also ensure that everyone is using the same command with the same flags.
You can edit the file directly, but this would be overwritten whenever npm updates, the best thing to do is go straight to the source.
If you want to run a command inside a submodule, with npm, you can use the explore
command, as explained here:
npm explore submodule -- npm run subscript
It seems that yarn does not have an equivalent command,
but there is a cwd
option (not yet very well documented) that let you choose the working directory:
yarn --cwd node_modules/submodule/ run subscript
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With