我已经使用"npm install ibm-watson“命令安装了ibm-watson,我可以在node_modules文件夹中看到该文件夹及其文件,但仍然显示此错误。节点版本- v10.15.3
const watson = require('ibm-watson');
const { IamAuthenticator } = require('ibm-watson/auth');
const { BasicAuthenticator } = require('ibm-watson/auth');
// to get an IAM Access Token
const authorization = new watson.AuthorizationV1({
authenticator: new IamAuthenticator({ apikey: 'fakekey-1234' }),
});
authorization.getToken(function (err, token) {
if (!token) {
console.log('error: ', err);
} else {
// Use your token here
}
});
其他模块都可以导入,只有这个模块不导入。
internal/modules/cjs/loader.js:584
throw err;
^
Error: Cannot find module 'ibm-watson'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:582:15)
at Function.Module._load (internal/modules/cjs/loader.js:508:25)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:22:18)
at Object.<anonymous>
发布于 2020-07-02 03:35:52
我刚刚面对了这个问题。我没有安装正确版本的软件包。请检查Node的apidocs,以查看所需的IBM Watson npm包的正确版本。对于我来说,我需要5.6.0。
您可以使用以下命令进行安装:
npm install ibm-watson@^5.6.0
发布于 2020-02-07 01:32:55
当您获得令牌时,我将猜测您正在使用语音转文本。正如注释所暗示的,失败的行是const watson = require('ibm-watson');
,因为它没有被导出。相反,根据API文档-https://cloud.ibm.com/apidocs/speech-to-text/speech-to-text?code=node#authentication,您将使用:
const SpeechToTextV1 = require('ibm-watson/speech-to-text/v1');
const { IamAuthenticator } = require('ibm-watson/auth');
const { IamTokenManager } = require('ibm-watson/auth');
如果您使用的不是STT,那么当需要ibm-watson
时,其他服务的工作方式是相同的。可在此处找到API文档的链接- https://cloud.ibm.com/apidocs
发布于 2020-02-20 17:06:48
我也面临着同样的问题。
读完代码后,我明白了。
只有sdk.ts
文件,没有index.ts
文件。
https://github.com/watson-developer-cloud/node-sdk
// const watson = require('ibm-watson');
const watson = require('ibm-watson/sdk');
但我还是得到了错误。
如果我写下下面的代码,它最终会起作用
import AuthorizationV1 from 'ibm-watson/authorization/v1'
import { IamAuthenticator } from 'ibm-watson/auth'
const apikey = '********'
const authorization = new AuthorizationV1({
url: 'https://iam.cloud.ibm.com/identity/token',
authenticator: new IamAuthenticator({ apikey }),
})
authorization.getToken(function (err, token) {
if (!token) {
console.log('error: ', err);
} else {
// Use your token here
}
});
但是CORS存在一个问题。我不知道更多了。
答案写在这里。我需要在服务器端做这件事
https://github.com/watson-developer-cloud/node-sdk/issues/884#issuecomment-515050023
https://stackoverflow.com/questions/60099163
复制相似问题