Use npx to run commands with different Node.js versions

Elijah Manor
InstructorElijah Manor
Share this video with your friends

Social Share Links

Send Tweet
Published 6 years ago
Updated 5 years ago

In this lesson we will use npx to run a package using different versions of Node.js. This can become valuable when testing the various new features that are introduced in versions of Node.js.

Instructor: [00:01] Let's make an NPX Node folder, and add some Javascript. Here, we'll create an object literal with two properties, one and two. Then we'll console a new object, spreading the contents of A, and adding a three property. Then we'll redirect this to an index.js file.

[00:21] If we use Node at this point to execute our Javascript, we'll get an object with properties one, two, and three. Now, I am using a newer version of Node that understands the object spread feature, but we can use NPX to alter the version of Node that we use when running our script.

[00:38] Let's run npx -p node@8.2.1, and tell it to execute our index.js file. Now, Node version 8.2.1 did not yet have support for object spread. It makes sense why we would get an unexpected token error at the ...A location.

[00:55] However, we could try that command again, but this time, we'll update the Node version to 8.3.0which is when object spread was added. Sure, indeed, it works as expected. This particular trick could come in handy if you need to try out code with various versions of Node.

egghead
egghead
~ 10 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today