Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running a script inside package.json under node_modules?

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.

like image 267
Martin Avatar asked Feb 15 '18 07:02

Martin


People also ask

How do I run a script inside a json package?

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 does scripts work in package json?

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.

Can you edit files in node_modules?

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.


1 Answers

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

like image 197
TGrif Avatar answered Sep 28 '22 16:09

TGrif