DEV Community

Cover image for Mastering NPM Scripts

Mastering NPM Scripts

Paula Santamaría on February 15, 2021

You may have come across the scripts property in the package.json file and even write some scripts yourself. But do you know all you can do with NP...
Collapse
 
strdr4605 profile image
Dragoș Străinu

Here is a tool to document npm scripts:
npx why

Collapse
 
jaballadares profile image
John Balladares

This is the most useful article I have seen on NPM scripts. Thank you for:

  1. Being so thorough yet clear
  2. Providing examples (I feel like I knew I could have a package.json to hold global configs like ES Lint, but this really drove it home)

Wish there was a repo with useful NPM scripts.

Collapse
 
paulasantamaria profile image
Paula Santamaría

Thanks, John! I'm glad it was helpful.
I was going to create a repo with examples to reference in this post, but in the end, it was taking me too long, and decided to just include the examples in the post. Maybe I'll keep working in the repo later 🤔

Collapse
 
jaballadares profile image
John Balladares

Yay! I would definitely take a look at that.

Is it common for folks to have a package.json file in their home directory for global access to NPM scripts/

Collapse
 
igorsantos07 profile image
Igor Santos • Edited

This article is very concise and complete! That's rare to see around 🙏

One thing to keep in mind about parallel scripts: when you put & at the end of a bash command you are, actually, placing that command in background. This means that, if you use that with a server command, you'll never be able to close it with Ctrl^C!

I realized that when trying to run the Firebase emulators + Svelte dev server in a single command. I can't find a clean solution to that, unfortunately 🤷🏻‍♂️
The best I could do is run a couple of tmux commands to create a single process with two outputs, but it's a bit convoluted and makes your project dependent on a platform feature (having tmux installed). I got a quick introduction to tmux in this article, and ChatGPT gave me the right commands to control the tmux session (since tmux --help is of no help lol).

Collapse
 
anonymousm profile image
mludovici

very cool, some nice new stuff that I learned :)

Collapse
 
paulasantamaria profile image
Paula Santamaría

Thank you! Glad to hear that :)

Collapse
 
johnb21 profile image
Johnb21

This article is filled with so much goodness and very useful information! Thank you!

Collapse
 
paulasantamaria profile image
Paula Santamaría

Glad to hear that 😊

Collapse
 
weakish profile image
Jang Rush

Hi, I'd like to translate this awesome tutorial to Chinese. Can you give me the permission? The translated text will be published at nextfe.com

Collapse
 
paulasantamaria profile image
Paula Santamaría

Sure! It's ok, as long as you credit me :)

Collapse
 
weakish profile image
Jang Rush

Chinese translation published: nextfe.com/mastering-npm-scripts/

There is a backlink to this original post at the beginning of the translated text.

Thread Thread
 
paulasantamaria profile image
Paula Santamaría

Thank you!

Collapse
 
ruyadorno profile image
Ruy Adorno

Super useful reference! Thanks for putting it all together ❤️

Collapse
 
paulasantamaria profile image
Paula Santamaría • Edited

Thanks, Ruy! Happy to help :)

Collapse
 
wojtekmaj profile image
Wojciech Maj

I thought I knew everything about NPM scripts, or most of it at least. You proved me wrong. Thanks!

Collapse
 
paulasantamaria profile image
Paula Santamaría

Writing it had the same effect on me :P Thanks, Wojciech!

Collapse
 
ignassedunovas profile image
ignas-sedunovas

On Windows "hello:cmd": "cd scripts && helloworld.cmd" could be written as "hello:cmd": "scripts\\helloworld.cmd"

Collapse
 
paulasantamaria profile image
Paula Santamaría

That didn't work for me on Windows (I tried it in CMD and PowerShell), that's why I went for the cd alternative. Did it work for you?

Collapse
 
ignassedunovas profile image
ignas-sedunovas

Yes. But only when using double backslashes as directory separator.

Thread Thread
 
paulasantamaria profile image
Paula Santamaría

I see it now, thank you! I'll update the post ASAP.

Collapse
 
damiensavoldelli profile image
damienSavoldelli

Thanks, very helpful. I was looking for how passing arguments as variable and I find this article with useful informations ! Thanks a lot ! 😃

Collapse
 
paulasantamaria profile image
Paula Santamaría

Glad you find it helpful! :)

Collapse
 
rajmohanpdy profile image
rajmohan s

cool tricks. useful information regarding scripts.

Collapse
 
paulasantamaria profile image
Paula Santamaría

Great! I'm glad you found it useful :)

Collapse
 
jonton profile image
Jonathan Wong

Loved this article - definitely helped refresh some topics for me when I moved to a new codebase!

Collapse
 
paulasantamaria profile image
Paula Santamaría

Thanks, Jonathan! Writing it definitely helped me refresh some stuff and learn new tricks. Happy to see it helped others as well.

Collapse
 
activenode profile image
David Lorenz • Edited

I am seeing a lot of posts these days that I barely read cause it's always the same ish. But this one is a nice one. Really. Good job here

Collapse
 
paulasantamaria profile image
Paula Santamaría

Thank you, David! I'm glad you enjoyed it.

Collapse
 
bgrand_ch profile image
Benjamin Grand

Thanks, very helpful ☺️

Collapse
 
paulasantamaria profile image
Paula Santamaría

Thanks, I'm glad to hear that!

Collapse
 
xuhdev profile image
Hong Xu

Great article! I wrote a further expansion of this article on avoiding using system-specific commands in scripts: 8hob.io/posts/guides-creating-cros...

Collapse
 
vincenguyen94 profile image
VINCENGUYEN94

Thank you ! You made my day :)