Javascript 错误:找不到模块';nexmo&x27&;错误TS2307:找不到模块nexmo
因此,我正在使用NestJs框架和typescript 我被要求使用Nexmo的节点库添加双因素身份验证(SMS)。 以下是他们网站上的链接: 在开发模式下,一切都按承诺进行 但当我尝试为生产构建时,我遇到了以下错误:Javascript 错误:找不到模块';nexmo&x27&;错误TS2307:找不到模块nexmo,javascript,typescript,requirejs,nestjs,nexmo,Javascript,Typescript,Requirejs,Nestjs,Nexmo,因此,我正在使用NestJs框架和typescript 我被要求使用Nexmo的节点库添加双因素身份验证(SMS)。 以下是他们网站上的链接: 在开发模式下,一切都按承诺进行 但当我尝试为生产构建时,我遇到了以下错误: Error: Cannot find module 'nexmo' 所以我开始在谷歌上搜索 我第一次读到有关导入与要求的内容 我的NestJs项目中的几乎所有内容都与导入一起工作 但我记得使用require有时没有问题。 例如,当我不得不使用这两个时,我没有问题: const
Error: Cannot find module 'nexmo'
所以我开始在谷歌上搜索
我第一次读到有关导入与要求的内容
我的NestJs项目中的几乎所有内容都与导入一起工作
但我记得使用require有时没有问题。
例如,当我不得不使用这两个时,我没有问题:
const axios = require('axios');
const xml2js = require('xml2js');
然后我遇到了有类似问题的人,他们可以通过修改tsconfig.json来解决这些问题
一些人添加了“moduleResolution”:“node”而不是“moduleResolution”:“classic”,而另一些人将“module”:“commonjs”更改为“module”:“AMD”,或“module”:“ESNext”
所有这些我都试过了,但都没有用。有时错误会从以下方面发生变化:
Error: Cannot find module 'nexmo'
致:
然后我开始在这里阅读,以了解更多关于这件事的信息:
我又一次找不到解决办法
我的一位朋友让我检查一下有关安装打字的一些信息,但是NestJs已经使用了@types,从我所读到的内容来看,这是一个更为更新的打字版本
除此之外,我没有运气。
我所理解的是,该项目必须从ts编译为js,并且由于某些原因,NestJs无法在node_modules文件夹中找到nexmo
您能帮我找到正确的方法吗?查看Github上的Nexmo软件包,我在这里看到它正在从主模块导出默认值: 这意味着在typescript文件中,您应该能够简单地说:
import Nexmo from 'nexmo';
npm中的某些包不支持commonjs(这意味着它们不支持node js模块),在这种情况下,您需要使用以下语法在typescript中导入这些包:
import Nexmo = require('nexmo');
试一试第一个,看看它是否适合您。查看Github上的Nexmo包,我在这里看到它正在从主模块导出默认值: 这意味着在typescript文件中,您应该能够简单地说:
import Nexmo from 'nexmo';
npm中的某些包不支持commonjs(这意味着它们不支持node js模块),在这种情况下,您需要使用以下语法在typescript中导入这些包:
import Nexmo = require('nexmo');
尝试一下第一个,看看它是否适合您。好的-尝试了一些新安装的东西,下面是我要做的工作: //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
}
}
然后我跑了
~/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
好的-尝试了一些新安装的东西,下面是我的工作: //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
}
}
然后我跑了
~/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
很好的标注,出于习惯,我总是在每个typescript项目中设置“allowSyntheticDefaultImports”:true,“esModuleInterop”:true,甚至没有想到提及这些设置。很好的标注,我总是设置“allowSyntheticDefaultImports”:true,“esModuleInterop”:在每个typescript项目中都是这样,这是出于习惯,甚至没有想到提及这些设置。