4

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?

3

2 Answers 2

5

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

Sign up to request clarification or add additional context in comments.

Comments

3

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.

Comments

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.