gpt4 book ai didi

node.js - 运行Navalia示例时出现 typescript 错误

转载 作者:太空宇宙 更新时间:2023-11-04 00:18:40 24 4
gpt4 key购买 nike

我正在尝试从 https://github.com/joelgriffith/navalia 运行此示例但就我而言,我无法让它正常工作:

navaliatest.ts

/// <reference path="typings.d.ts" />

import { Chrome } from 'navalia';
const chrome = new Chrome();

async function buyItOnAmazon() {
const url = await chrome.goto('https://amazon.com');
const typed = await chrome.type('input', 'Kindle');
const clicked = await chrome.click('.nav-search-submit input');

chrome.done();

console.log(url, typed, clicked); // 'https://www.amazon.com/', true, true
}

buyItOnAmazon();

tsconfig.json

{
"files": [
"navaliatest.ts"
],
"compilerOptions": {
"noImplicitAny": false,
"target": "es6",
"moduleResolution": "node",
"paths": {
"*" : ["/usr/local/lib/node_modules/*"]
}
}
}

typings.d.ts

/// <reference path="/usr/local/lib/node_modules/navalia/build/Chrome.d.ts" />

declare module 'navalia' {
var Chrome: any;
export = Chrome;
}

以下是版本:

MacBook-Pro:testcasperjs myusername$ node --version
v6.11.2MacBook-Pro:testcasperjs myusername$ npm --version
3.10.10
MacBook-Pro:testcasperjs myusername$ tsc --version
Version 2.4.2

这是我得到的错误,尽管我确实得到了 .js 文件输出:

MacBook-Pro:testcasperjs myusername$ tsc navaliatest.ts
../../../usr/local/lib/node_modules/navalia/node_modules/chrome-launcher/chrome-finder.ts(203,16): error TS2339: Property 'from' does not exist on type 'ArrayConstructor'.
../../../usr/local/lib/node_modules/navalia/node_modules/chrome-launcher/chrome-launcher.ts(99,15): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
navaliatest.ts(3,10): error TS2305: Module ''navalia'' has no exported member 'Chrome'.

我确信某个地方有一个愚蠢的错误,但请有人帮助我看看吗?谢谢。

最佳答案

您不需要重新声明navalia。已经为您完成了node_modules/navalia/build/index.d.ts鉴于 moduleResolution 设置为 Node

您需要将module设置为commonjs,以便可以在 Node 中运行它

tsconfig.json

{
"files": [
"navaliatest.ts"
],
"compilerOptions": {
"noImplicitAny": false,
"target": "es6",
"module": "commonjs",
"moduleResolution": "Node"
}
}

navaliatest.ts(无变化)

import { Chrome } from 'navalia';
const chrome = new Chrome();

async function buyItOnAmazon() {
const url = await chrome.goto('https://amazon.com');
const typed = await chrome.type('input', 'Kindle');
const clicked = await chrome.click('.nav-search-submit input');

chrome.done();

console.log(url, typed, clicked); // 'https://www.amazon.com/', true, true
}

buyItOnAmazon();

它将创建没有错误的navaliatest.js,可以在 Node 中运行。

关于node.js - 运行Navalia示例时出现 typescript 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45448060/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com