【问题标题】:Protractor with typescript cannot find module 'protractor' after successfully resolved成功解决后,带有打字稿的量角器找不到模块“量角器”
【发布时间】:2017-04-09 13:32:09
【问题描述】:

这个问题要了我的命。请帮忙。 我有以下文件结构:

node_modules
    protractor
    typescript
package.json
register-popup.ts

package.json的内容

{
    "name": "typescriptProba",
    "version": "1.0.0",
    "description": "",
    "main": "index.js",
    "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1"
    },
    "keywords": [],
    "author": "",
    "license": "ISC",
    "dependencies": {
         "protractor": "^4.0.11",
         "typescript": "^2.0.10"
    }
}

register-popup.ts的内容:

import { ElementFinder, element, by } from "protractor";

export class RegisterPopup {
    public registerWithFacebookButton: ElementFinder = element(by.css('li.facebook a'));
    public registerWithGoogleButton: ElementFinder = element(by.css('li.google a'));
    public registerWithEmailButton: ElementFinder = element(by.css('li.email a'));
    public registerWithMobileButton: ElementFinder = element(by.css('li.natel a'));

    constructor () {}

    openPopup() {
        element(by.css('.account.user')).click();
        element(by.id('openRegister')).click();
    }

    openRegisterByEmailPopup() {
        this.registerWithEmailButton.click();
    }

    openRegisterByPhonePopup() {
        this.registerWithMobileButton.click();
    }
}

为了将 ts 文件编译为 js 文件,我使用以下命令:

./node_modules/typescript/bin/tsc "./register-popup.ts" --module commonjs --noResolve --traceResolution

执行命令后出现以下错误:

error TS2307: Cannot find module 'protractor'.

但是我的模块跟踪解析是这样的:

======== Resolving module 'protractor' from '/Users/predraglazarevic/www/typescriptProba/register-popup.ts'. ========
Module resolution kind is not specified, using 'NodeJs'.
Loading module 'protractor' from 'node_modules' folder.
File '/Users/predraglazarevic/www/typescriptProba/node_modules/protractor.ts' does not exist.
File '/Users/predraglazarevic/www/typescriptProba/node_modules/protractor.tsx' does not exist.
File '/Users/predraglazarevic/www/typescriptProba/node_modules/protractor.d.ts' does not exist.
Found 'package.json' at '/Users/predraglazarevic/www/typescriptProba/node_modules/protractor/package.json'.
'package.json' has 'typings' field 'built/index.d.ts' that references '/Users/predraglazarevic/www/typescriptProba/node_modules/protractor/built/index.d.ts'.
File '/Users/predraglazarevic/www/typescriptProba/node_modules/protractor/built/index.d.ts' exist - use it as a name resolution result.
Resolving real path for '/Users/predraglazarevic/www/typescriptProba/node_modules/protractor/built/index.d.ts', result '/Users/predraglazarevic/www/typescriptProba/node_modules/protractor/built/index.d.ts'
======== Module name 'protractor' was successfully resolved to '/Users/predraglazarevic/www/typescriptProba/node_modules/protractor/built/index.d.ts'. ========

所以我有

模块名称“量角器”已成功解析

但仍有错误

错误 TS2307:找不到模块“量角器”。

为什么?

回答

当我省略 --noResolve 标志时,它正在工作。所以命令应该是

./node_modules/typescript/bin/tsc "./register-popup.ts" --module commonjs

【问题讨论】:

  • 你能分享你的tsconfig文件吗?
  • 没有tsconfig文件
  • 我不明白没有 tsconfig 文件你是如何编译的?
  • 我想你可以。其实我的ts文件是编译成js文件的,但是上面有错误
  • 当我省略 --noResolve 标志时,它正在工作。所以命令应该是 ./node_modules/typescript/bin/tsc "./register-popup.ts" --module commonjs

标签: angularjs typescript protractor


【解决方案1】:

首先你需要检查你的 tsconfig.json,你会看到 typeRoots

{
  "compileOnSave": false,
  "compilerOptions": {
    "declaration": false,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "lib": [
      "es2016"
    ],
    "module": "commonjs",
    "moduleResolution": "node",
    "outDir": "path/to/transpiled/files",
    "sourceMap": true,
    "target": "es6",
    "typeRoots": [
      "node_modules/@types"
    ]
  }
}

如果您有任何其他的definitelyTyped 模块,它们将被本地路径中的typeRoots 目录拾取:node_modules/@types

【讨论】:

  • 更新上面的答案。 @types/protractor 已弃用。
【解决方案2】:

尝试运行:

yarn install && yarn webpack:build 

应该下载所有缺失的模块并重建 UI。

【讨论】:

    【解决方案3】:

    我必须在我的 tsconfig.json 文件中将 moduleResolution 设置设置为 node

    "moduleResolution": "node"
    

    之后,我就可以从量角器中导入浏览器等了。

    import { browser } from 'protractor'
    

    【讨论】:

      【解决方案4】:

      您没有在本地安装量角器模块/库。所以,

      运行这个命令:npm install --save protractor。如果它不起作用。 试试这个:

      1. 打开 VSCode 并点击查看 -> 终端

      2. 导航到要安装量角器的文件夹。

      3. 使用以下命令安装量角器。

      npm 安装量角器 -g

      4.重启VSCode。它会在你的本地添加 protractor.conf.js 文件。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-11-19
        • 1970-01-01
        • 1970-01-01
        • 2019-08-28
        • 1970-01-01
        • 1970-01-01
        • 2016-09-04
        相关资源
        最近更新 更多