【问题标题】:Error: Cannot find module 'nexmo' & error TS2307: Cannot find module nexmo错误:找不到模块“nexmo”和错误 TS2307:找不到模块 nexmo
【发布时间】:2020-08-19 13:05:26
【问题描述】:

所以我正在使用 NestJs 框架和 typescript

我被要求使用 Nexmo 的节点库添加两因素身份验证 (SMS)。 这是他们网站上的链接:https://dashboard.nexmo.com/getting-started/verify

在开发模式下一切正常。

但是当我尝试为生产构建时,我得到了这个错误:

Error: Cannot find module 'nexmo'

所以我开始搜索它。

我首先阅读了import vs require

我的 NestJs 项目中的几乎所有内容都可以使用导入。

但我记得有时使用 require 没有问题。 例如,当我不得不使用这两个时,我没有问题:

const axios = require('axios');
const xml2js = require('xml2js');

然后我遇到了遇到类似问题的人,他们能够通过稍微修改他们的 tsconfig.json 来解决这些问题。

有些人添加了 "moduleResolution": "node" 而不是 "moduleResolution": "classic",而其他人则更改了 "module": "commonjs" “模块”:“AMD”,或 “模块”:“ESNext”

我尝试了所有这些都无济于事。有时错误是从:

Error: Cannot find module 'nexmo' 

到:

error TS2307: Cannot find module nexmo

然后我开始阅读此处以了解有关此问题的更多信息: https://www.typescriptlang.org/docs/handbook/module-resolution.html

我再次找不到我的解决方案。

我的一个朋友告诉我要检查有关安装 typings 的信息,但 NestJs 已经使用 @types,据我阅读,这是一个更新版本的类型。 p>

除此之外,我没有运气。 我所理解的是项目必须从ts编译为js,并且由于某种原因NestJs在node_modules文件夹中找不到nexmo。

你能帮助或指导我正确的方式吗?

【问题讨论】:

    标签: javascript typescript requirejs nestjs vonage


    【解决方案1】:

    好的 - 从全新安装中尝试了一些东西,这就是我要工作的内容:

    //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();
      }
    }
    

    然后我跑了

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

    【讨论】:

    • 好的标注,我总是出于习惯在每个打字稿项目中设置 "allowSyntheticDefaultImports": true,"esModuleInterop": true ,甚至没有考虑提及这些设置。
    【解决方案2】:

    查看 Github 上的 Nexmo 包,我在这里看到它正在从其主模块导出一个默认值:https://github.com/Nexmo/nexmo-node/blob/master/src/Nexmo.js#L175

    这意味着在你的打字稿文件中你应该能够简单地说:

    import Nexmo from 'nexmo';
    

    npm 中的某些包对 commonjs 不友好(意味着它们对 node js 模块不友好),在这种情况下,您需要使用以下语法在 typescript 中导入它:

    import Nexmo = require('nexmo');
    

    试一试第一个,看看它是否适合你。

    【讨论】: