Patrik Schulze
Patrik Schulze

Reputation: 41

Adding npm package.json scripts per console command

If you use npm install <module> --save it writes this module into the package.json, which is very useful.

The package.json has an area for scripts. I want to add some scripts here automatically, so what would be useful would be a command to use add scripts here instead of opening the files yourself.

Something like npm add-script <name> <command>

Is there really nothing like this?

Upvotes: 4

Views: 2311

Answers (2)

FDisk
FDisk

Reputation: 9466

Older versions of NPM 7, 8 had a special command to add scripts to the package.json https://docs.npmjs.com/cli/v8/commands/npm-set-script and the syntax would be

npm set-script <name> "<command>"

But the new version of NPM has changed and now everything regarding package.json file can be changed using pkg argument

npm pkg set scripts.<name>="<command>"

This way npm pkg set you can modify any part of your package.json file. Keep in mind that there are set, get and delete

More info here https://docs.npmjs.com/cli/v10/commands/npm-pkg

Upvotes: 5

Javier C.
Javier C.

Reputation: 8267

Yo can use npm add script library.

  • Install the library in your project:

    npm install -g npm-add-script
    
  • Use it, like this npmAddScript -k <name> -v <command>, for example:

    npmAddScript -k test -v "node test.js"
    

More info in the official page of npm add script.

Upvotes: 3

Related Questions