Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: translate is not a function #24

Open
flyeven opened this issue Mar 11, 2021 · 4 comments
Open

TypeError: translate is not a function #24

flyeven opened this issue Mar 11, 2021 · 4 comments

Comments

@flyeven
Copy link

flyeven commented Mar 11, 2021

const result = await translate(`I'm fine.`, {
                     ^

TypeError: translate is not a function

运行测试代码,出现如上问题。怎么解?

import translate from 'google-translate-open-api';
const result = await translate(`I'm fine.`, {
  tld: "cn",
  to: "zh-CN",
});
const data = result.data[0];
@flyeven
Copy link
Author

flyeven commented Mar 11, 2021

node 版本 14,运行 google bing api 都没为题。单独创建npm项目,只添加 google-translate-open-api ,也是上述问题。

$ node -v
v14.15.4

const translate = require('@vitalets/google-translate-api');

import pkg from 'bing-translate-api';
const { translate } = pkg;

@hua1995116
Copy link
Owner

我是用 ts-node 运行的

@flyeven
Copy link
Author

flyeven commented Mar 11, 2021

我这怎么都不行呢,gitbash 下 和 ps 下都是错误。

PS D:\Programming\translate> ts-node index.js
TypeError: translate is not a function
    at file:///D:/Programming/translate/index.js:32:22
    at ModuleJob.run (internal/modules/esm/module_job.js:152:23)
    at Loader.import (internal/modules/esm/loader.js:166:24)
    at Object.loadESM (internal/process/esm_loader.js:68:5)
PS D:\Programming\translate> node index.js
file:///D:/Programming/translate/index.js:32
const result = await translate(`I'm fine.`, {
                     ^

TypeError: translate is not a function
    at file:///D:/Programming/translate/index.js:32:22
    at ModuleJob.run (internal/modules/esm/module_job.js:152:23)
    at async Loader.import (internal/modules/esm/loader.js:166:24)
    at async Object.loadESM (internal/process/esm_loader.js:68:5)

或者就是:

$ ts-node index.mjs
ReferenceError: require is not defined
    at file:///D:/Programming/translate/index.mjs:1:19
    at ModuleJob.run (internal/modules/esm/module_job.js:152:23)
    at Loader.import (internal/modules/esm/loader.js:166:24)
    at Object.loadESM (internal/process/esm_loader.js:68:5)

@heliang0915
Copy link

我是将这句 const result = await translate(I'm fine., {
tld: "cn",
to: "zh-CN",
});
const data = result.data[0]; 包在一个async函数中 ,

然后调用这个函数执行并调用它的then方法

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants