简体   繁体   English

错误:找不到模块“nexmo”和错误 TS2307:找不到模块 nexmo

[英]Error: Cannot find module 'nexmo' & error TS2307: Cannot find module nexmo

so I'm working with the NestJs Framework and typescript .所以我正在使用NestJs框架和typescript

I was asked to add two factor authentication (SMS) using the node library of Nexmo .我被要求使用Nexmo的节点库添加双因素身份验证 (SMS)。 Here is a link from their website: https://dashboard.nexmo.com/getting-started/verify这是他们网站的链接: 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 .我首先阅读了import 与 require

Almost everything in my NestJs project works with import.我的 NestJs 项目中的几乎所有内容都可以使用导入。

But I remember using require sometimes with no problem.但我记得有时使用 require 没有问题。 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.然后我遇到了遇到类似问题的人,他们能够通过稍微修改一下他们的 tsconfig.json 来解决这些问题。

Some added the "moduleResolution": "node" instead of "moduleResolution": "classic" while others changed the "module": "commonjs" to "module": "AMD" , or "module": "ESNext"一些人添加了"moduleResolution": "node"而不是"moduleResolution": "classic"而其他人将"module": "commonjs"更改为"module": "AMD""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然后我开始阅读这里以了解更多关于这件事的信息: 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.我的一个朋友告诉我检查一些关于安装typings的东西,但是 NestJs 已经使用了@types ,从我读到的内容来看,它是一个更新版本的 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.我所了解的是,该项目必须从 ts 编译为 js,并且由于某种原因 NestJs 无法在 node_modules 文件夹中找到 nexmo。

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 //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 //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

Looking at the Nexmo package on Github, I see here that it is exporting a default value from its main module: https://github.com/Nexmo/nexmo-node/blob/master/src/Nexmo.js#L175查看 Github 上的 Nexmo package,我在这里看到它正在从其主模块导出默认值: https://github.com/Nexmo/nexmo-node/blob/master/src/Nexmo.js#L175

This means in your typescript file you should be able to simply say:这意味着在您的 typescript 文件中,您应该能够简单地说:

import Nexmo from 'nexmo';

Some packages in npm are not commonjs friendly (meaning they are not node.js module friendly) and in that case you would need to import that in typescript using this syntax: npm 中的某些包不是 commonjs 友好的(意味着它们不是 node.js 模块友好的),在这种情况下,您需要使用以下语法将其导入 typescript 中:

import Nexmo = require('nexmo');

Give that first one a shot though and see if it works for you.试一试第一个,看看它是否适合你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM