首页 > 解决方案 > 由于模块 ibm-watson/auth 无法运行代码

问题描述

以下代码片段来自 watson 音调分析器文档。问题:当我粘贴代码并运行代码时,它运行得非常好。但是,当我将它粘贴到另一个云函数中时,它表明找不到 ibm-watson/auth 模块。不知道为什么它的行为不同?

通常我保存在函数 main (params){} 下。但是,另一个云函数是异步函数,它具有 (require-promise) 的代码。不知道出了什么问题或问题是什么?

const ToneAnalyzerV3 = require('ibm-watson/tone-analyzer/v3');
const { IamAuthenticator } = require('ibm-watson/sdk');

const toneAnalyzer = new ToneAnalyzerV3({
  version: '2017-09-21',
  authenticator: new IamAuthenticator({
    apikey: 'apikey',
  }),
  serviceUrl: 'url',
});

const text = 'Team, I know that times are tough! Product '
  + 'sales have been disappointing for the past three '
  + 'quarters. We have a competitive product, but we '
  + 'need to do a better job of selling it!';

const toneParams = {
  toneInput: { 'text': text },
  contentType: 'application/json',
};

toneAnalyzer.tone(toneParams)
  .then(toneAnalysis => {
    console.log(JSON.stringify(toneAnalysis, null, 2));
  })
  .catch(err => {
    console.log('error:', err);
  });
    }

标签: node.jsibm-watsonibm-cloud-functionstone-analyzer

解决方案


使用 IBM Cloud Functions 时,请注意不同的运行时环境及其包含的模块和包。如果遇到找不到某个模块的错误,您

  • 要么必须使用具有更高版本的更新运行时环境
  • 或者必须自己包含包/模块

推荐阅读