3
votes

I'm working on a NestJs backend on multiple machines. One machine is not able to deal with the @hapi/joi package. When running the NestJs application in development mode I get this error

PS C:\Users\mhermsen\Desktop\joi-test> npm run start:dev

> [email protected] start:dev C:\Users\mhermsen\Desktop\joi-test
> nest start --watch

8:32:49 AM - Starting compilation in watch mode...


8:32:51 AM - Found 0 errors. Watching for file changes.
C:\Users\mhermsen\Desktop\joi-test\node_modules\@hapi\joi\lib\errors.js:246
    isJoi = true;
          ^

SyntaxError: Unexpected token =
    at Module._compile (internal/modules/cjs/loader.js:723:23)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Module.require (internal/modules/cjs/loader.js:692:17)
    at require (internal/modules/cjs/helpers.js:25:18)
    at Object.<anonymous> (C:\Users\mhermsen\Desktop\joi-test\node_modules\@hapi\joi\lib\index.js:9:16)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)

I tried to reproduce the problem. First, I created a new Nest application via nest new joi-test. Next I installed joi via

$ npm install --save @hapi/joi
$ npm install --save-dev @types/hapi__joi

I removed most of the code. The src directory only contains a main.ts file with

import * as Joi from "@hapi/joi";

Joi.object({
  num: Joi.number()
}).validate({
  port: "abc"
});

There is no Nest related code anymore so I think this has nothing to do with Nest. When removing the validation and only import the package, the code works fine.

import * as Joi from "@hapi/joi";

console.log("This is fine now");

Other machines work fine, this only appears on this machine. Any ideas how to fix it?


Update: I tested it on another company machine and this machine fails too. So my private Linux machines seem to work and my company Windows 10 machines seem to throw this error.

2

2 Answers

5
votes

I had the same problem. I think you use joi v17 with node < v12

Joi 17 works with node >= 12. So you have to use node 12 or downgrade to Joi-v.17

1
votes

If you are using nvm, in your terminal, upgrade your node version like this

$ nvm install 12
$ nvm use 12

and restart your node app.

This is a nice tutorial on how to install nvm https://dev.to/jlouiss/how-to-use-nvm-and-why-4e05