so I'm working with the NestJs Framework and typescript.
I was asked to add two factor authentication (SMS) using the node library of Nexmo. Here is a link from their website: https://dashboard.nexmo.com/getting-started/verify
Everything worked as promised while in development mode.
But when I tried to build for production I got this error:
Error: Cannot find module 'nexmo'
So I started googling about it.
I first read about import vs require.
Almost everything in my NestJs project works with import.
But I remember using require sometimes with no problem. For example when I had to use these two I had no problem:
const axios = require('axios');
const xml2js = require('xml2js');
Then I came across people that were having similar problems and they were able to solve them by modifying a bit their tsconfig.json.
Some added the "moduleResolution": "node" instead of "moduleResolution": "classic" while others changed the "module": "commonjs" to "module": "AMD", or "module": "ESNext"
I tried all of these with no avail. Sometimes the error was changing from:
Error: Cannot find module 'nexmo'
to:
error TS2307: Cannot find module nexmo
Then I started reading here to understand more about this matter: https://www.typescriptlang.org/docs/handbook/module-resolution.html
Again I couldn't find my solution.
A friend of mine told me to check something about installing typings but NestJs already uses @types which from what I read is a more updated version of typings.
Other than that I had no luck. All I understand is that the project must be compiled to js from ts and that for some reason NestJs can't find the nexmo in the node_modules folder.
Can you help or direct me to the right way?
Ok - tried a few things from a fresh install, and here is what I got to work:
//tsconfig.json
{
"compilerOptions": {
"module": "commonjs",
"declaration": true,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"esModuleInterop": true, <- this seems to be the important one here
"target": "es2017",
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./",
"incremental": true
}
}
//app.controller.ts
import { Controller, Get } from '@nestjs/common';
import { AppService } from './app.service';
import Nexmo from 'nexmo';
const nexmo = new Nexmo({
apiKey: '',
apiSecret: '',
});
@Controller()
export class AppController {
constructor(private readonly appService: AppService) {}
@Get()
getHello(): string {
console.log(nexmo.verify);
return this.appService.getHello();
}
}
I then ran
~/G/A/test-nest> master > nest build
~/G/A/test-nest >master >node ./dist/main
[Nest] 21347 - 08/19/2020, 11:36:27 AM [NestFactory] Starting Nest application...
[Nest] 21347 - 08/19/2020, 11:36:27 AM [InstanceLoader] AppModule dependencies initialized +18ms
[Nest] 21347 - 08/19/2020, 11:36:27 AM [RoutesResolver] AppController {}: +7ms
[Nest] 21347 - 08/19/2020, 11:36:27 AM [RouterExplorer] Mapped {, GET} route +3ms
[Nest] 21347 - 08/19/2020, 11:36:27 AM [NestApplication] Nest application successfully started +3ms