node16 and Jest with ES6+ modules

I’ve run into this a few times, so leaving some notes for myself for next time,

By default, attempting to use “import x from ‘y’ ” style ES6 module imports with Jest will give you this error:

SyntaxError: Cannot use import statement outside a module

With node16, support for modules is added with the experiemental-vm option, so update your package.json to add a script executing jest with this flag:

"test": "node --experimental-vm-modules node_modules/.bin/jest"

and then also in your package.json, add:

  "type": "module",

If you search for the ‘cannot use import statement outside a module error there are plenty of recommendations, including more elaborate babel transpile ES6 modules to CommonJS style requires.

Depending what you’re doing maybe you’ll need some of the other approaches too, but I found the above 2 steps to be the bare minimum

Quick tip of the day: While learning Java, avoid defining anything as static

If you’re just starting out and learning Java, avoid the temptation of defining anything as static until you understand what this modifier does and why/when you need to use it: no static Class properties, no static methods.

The only use of static while you’re starting out is in your main method. You don’t need it anywhere else. You’ll avoid a lot of unexpected and unexplainable behavior as a result.