Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Creating Your Own Arbitrary Tasks

Does npm tasks accept variables?

For instance, imagine I want to build a task to remove all the content of a folder:

"remove": "rm <folder>/*"

Then, on the command line do something like

npm run remove <folder>

GREGORY ASSASIE
GREGORY ASSASIE
3,898 Points

I believe you can only use NODE environment variables. Look up one of those task runners (gulp, grunt). You can automates tasks like that easily. i recommend gulp but if you like configuration, look up grunt.

1 Answer

Tom Geraghty
Tom Geraghty
24,174 Points

Yes.

See https://docs.npmjs.com/cli/run-script

As of npm@2.0.0, you can use custom arguments when executing scripts. The special option -- is used by getopt to delimit the end of the options. npm will pass all the arguments after the -- directly to your script:

npm run test -- --<your arguments>