Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Use npx to run commands with different Node.js versions

    Elijah ManorElijah Manor

    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.



    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson


    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.