运行navalia示例的打字稿错误

时间:2017-08-01 21:37:20

标签: node.js google-chrome typescript ecmascript-6 google-chrome-headless

我正试图从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'.

我确定某处有一个愚蠢的错误,但有人可以帮我看看吗?感谢。

1 个答案:

答案 0 :(得分:1)

您无需重新声明navalia。鉴于moduleResolution设置为Node

node_modules/navalia/build/index.d.ts已为您完成此操作

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

<强> 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,可以在节点中运行。