N: How to use `n` to run a package.json script?

Created on 24 Jan 2020  路  4Comments  路  Source: tj/n

Problem

Short Version

There is one app in which has dependencies that only work with latest node v13. We want to use node v13 for that app.

I have a script in package.json called "test": "ava".

But I can't seem to run my tests with n.

Long Version

My steps:

  1. npm i -g n
  2. sudo n latest
  3. sudo n then select the latest version

This is logged:

installed : v13.7.0 to /usr/local/bin/node
active : v12.13.1 at /usr/local/opt/node@12/bin/node

then I go to my project dir and try this:

n run 13.7.0 npm run test

Error: Cannot find module '/my-project-dir/npm'

If I try to run ava directly, I get the same error that it can't find ava.

Configuration Details

$ n --version
? 6.1.3

$ command -v node
? /usr/local/opt/node@12/bin/node

$ node -p process.platform
? darwin

Most helpful comment

Two tips:

1) You have a version of node installed earlier in your PATH than the location n is using, so changing the version using n won't be changing what gets run when you type node. This message is warning you that although n did install a node version, the active version is different.

installed : v13.7.0 to /usr/local/bin/node
active : v12.13.1 at /usr/local/opt/node@12/bin/node

(I don't recognise the path style and not sure what installed: /usr/local/opt/node@12/bin/node.)

2) n run is actually running a particular version of node. To run other things that use node (like npm or ava) try n exec instead. exec was added specifically for this sort of situation.

All 4 comments

Two tips:

1) You have a version of node installed earlier in your PATH than the location n is using, so changing the version using n won't be changing what gets run when you type node. This message is warning you that although n did install a node version, the active version is different.

installed : v13.7.0 to /usr/local/bin/node
active : v12.13.1 at /usr/local/opt/node@12/bin/node

(I don't recognise the path style and not sure what installed: /usr/local/opt/node@12/bin/node.)

2) n run is actually running a particular version of node. To run other things that use node (like npm or ava) try n exec instead. exec was added specifically for this sort of situation.

Hi @shadowspawn Thanks for the detailed information!

Two small follow-up questions.

I originally installed node via homebrew like so:
brew install node@12

  1. in this case, what is the recommended way to install and use n ?

  2. is n supposed to be used on a execute basis ? or are we supposed to switch node version repeatedly any time we want to set the global node version to something else?
    Eg. if I want to keep using node version 12 by default, but only use n to use node v13 for that one project

I originally installed node via homebrew ...
in this case, what is the recommended way to install and use n

I think it is personal preference whether you use npm or brew to install and update n. I personally use npm. (An initial advantage in using brew is bypassing the bootstrapping problem of npm being a convenient way to install n!)

I recommend only having one node on your PATH, so I suggest you try uninstalling the homebrew copy of node. (Make a list of your global npm modules first as you will probably want to reinstall them in the new location, npm list -g --depth=0.)

is n supposed to be used on a execute basis ? or are we supposed to switch node version repeatedly any time we want to set the global node version to something else?

The normal use case for n is changing the globally installed node version.

The execute basis is intended for more limited use to test a specific version without changing the globally installed version, like in scripts in your one special project.

thank you very much

Was this page helpful?
0 / 5 - 0 ratings

Related issues

shadowspawn picture shadowspawn  路  8Comments

mafischer picture mafischer  路  7Comments

shadowspawn picture shadowspawn  路  6Comments

samholmes picture samholmes  路  5Comments

bo01ean picture bo01ean  路  4Comments