Npm not running specified node version?

npm/node newbie, having trouble with configuration for this project: Glitch :・゚✧

I started with a static web project and added a package.json file. npm magically became available to run in the terminal – that was cool! Then I added jest using the package manager and set the test script to run jest. It gave me an error that makes me think it needs a newer node version. So I changed the test script to just run node --version instead of jest. So my package.json now contains the following:

...
"scripts": {
    "test": "node --version"
  },
  "devDependencies": {
    "jest": "^28.0.1",
    "jest-cli": "^28.0.3"
  },
  "engines": {
    "node": "16.x"
  },

but when I run npm test at the terminal it shows that it’s running the default v10 of node:

$ npm test

> lbl-jest-browser-example@0.0.1 test /app
> node --version

v10.24.1

I tried running enable-npm to restart things, but it didn’t help.

How can I get my test command to run under node v12 (as already specified in the engines section) instead of v10?

Running node --version checks for what version you have. So I’d remove that from the package.json. In the engines section is where you specify what version you want. The latest you can run on Glitch is (last I checked) Node 16.

Yes, I want node version 16 and so my engines section says:

"engines": {
    "node": "16.x"
  },

But when I run a regular test command, it fails with an error that make it look like jest is getting an old version of node. In order to confirm that, I changed the test script to just tell me what version of node it’s getting instead of actually running the tests. It is still getting version 10. Why isn’t it getting version 16?

You may not be wrong, but I’ve never heard of checking for the node version within the package.json. (I won’t be too surprised if someone on this forum chimes in just to tell me that it IS indeed a good idea or something they’ve seen though :slight_smile: )

Either way, at this point, I’d go to your terminal and type ‘refresh.’ This should refresh your project and install Node 16 as you specified.

refresh worked! After doing that, the node --version command returns v16, and when I change it back from that to the test command (which is jest) now the tests pass. Thank you!!

1 Like

The refresh command is for stopping and restarting the system that runs install + start with outputs going to the project’s logs panel. Changing the package.json, including the engines.node setting, usually restarts those automatically. But it’s not evident that you were running into a problem with that from your description.

Rather, you mention having run npm test from the terminal. That indeed can end up using the wrong node version in some circumstances. Glitch uses nvm to manage which node version is in use, and nvm controls what runs when you type node, npm, etc. by setting up environment variables. If you were already in a terminal session with a shell running, those variables won’t change when you edit the package.json to change the engines.node setting. As I understand it, running refresh doesn’t do anything to restart a terminal session, so it alone won’t make npm test start working.

What you’d have to do in that case is close out any terminals from before the change and open new ones. And supposing that the install + start stuff that Glitch runs did successfully pick up the change as expected, then you wouldn’t even need to run refresh.

I did an experiment. I changed my node version to v12 and typed refresh. Then I checked what version npm was running with and it still gave me v16. So then I typed exit and reconnected to a new terminal session. Now when I checked it gave me 12. So I guess closing the terminal session really is necessary. I think the reason it seemed like refresh had worked for me yesterday was that my terminal session had also timed out in the meanwhile!

1 Like