【问题标题】:React gives Error: not supported error when I try and import local module当我尝试导入本地模块时,React 给出错误:不支持的错误
【发布时间】:2019-07-04 17:54:34
【问题描述】:

我的 create-react-app src 文件夹中有一个本地模块 (speech.js),它是他们网站上的 google 文本到语音代码。我将其调整为箭头函数并使用特定的导出语法。

const textToSpeech = require('@google-cloud/text-to-speech');

// Import other required libraries
const fs = require('fs');
const util = require('util');
export const main = async () => {
  // Creates a client
  const client = new textToSpeech.TextToSpeechClient();

  // The text to synthesize
  const text = "Hello world";

  // Construct the request
  const request = {
    input: {text: text},
    // Select the language and SSML Voice Gender (optional)
    voice: {languageCode: 'en-US', ssmlGender: 'NEUTRAL'},
    // Select the type of audio encoding
    audioConfig: {audioEncoding: 'MP3'},
  };

  // Performs the Text-to-Speech request
  const [response] = await client.synthesizeSpeech(request);
  // Write the binary audio content to a local file
  const writeFile = util.promisify(fs.writeFile);
  await writeFile('output.mp3', response.audioContent, 'binary');
  console.log('Audio content written to file: output.mp3');
};

我不明白为什么这种语法在 App.js 中不起作用。 import {main} from './speech';

我收到错误消息,Error: not support 和“4 个堆栈帧已折叠”。信息量很大!

有谁知道这里的错误可能是什么?我以为只要我使用 es6 样式的导入和导出,我就不会收到错误。这可能是由于 Speech.js 的第一个 require() 语句造成的吗?任何帮助,将不胜感激。在过去的 40 分钟里,我一直想把头撞在墙上。

【问题讨论】:

  • 检查 babel 预设。您可以在根目录下创建 .babelrc 并在其中添加预设。
  • @SameerRezaKhan 感谢您的回复。预设应该是什么?
  • 如果你的项目中有最新的 babel,preset 应该是 @babel/preset-react 和 @babel/preset-env
  • @SameerRezaKhan 我应该对这个文件做些什么吗?我在项目根目录中创建它,但我不确定如何在我的项目中有效地使用它。

标签: node.js reactjs import export


【解决方案1】:

可能不是正确的答案,但我相信它很有可能是正确的。我相信,由于 node 只是一个运行时环境,而不是实际浏览器的一部分,因此您无法将 node 模块与 react(前端框架)一起使用。解决这个难题的方法是使用电子之类的东西。

【讨论】:

  • 堆栈跟踪说了什么?不支持模块 fs?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-07
  • 1970-01-01
  • 1970-01-01
  • 2022-12-07
相关资源
最近更新 更多