1

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

海军测试.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/*"]
    }
  }
}

打字.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'.

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

4

1 回答 1

1

您无需重新声明navalia. 它已经为你完成了node_modules/navalia/build/index.d.tsmoduleResolution设置为Node

您需要设置modulecommonjs以便可以在节点中运行它

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没有错误,可以在节点中运行。

于 2017-08-07T17:09:37.687 回答