【问题标题】:Cannot find type definition file for 'ramda'找不到“ramda”的类型定义文件
【发布时间】:2018-07-31 23:15:24
【问题描述】:

我有一个Ionic 2 & Angular 2 的项目。

我在我的代码中使用 RamdaJS,这与这些命令完美配合:

  • ionic serve
  • ionic cordova build android
  • ionic cordova run android

但是当我尝试执行这个命令时:ionic cordova build android --prod --release,我得到一个关于 Ramda 和 Type 的错误。

查看我的package.json的一部分:

"dependencies": {
    "@angular/common": "2.4.8",
    "@angular/compiler": "2.4.8",
    "@angular/compiler-cli": "2.4.8",
    "@angular/core": "2.4.8",
    "@angular/flex-layout": "^2.0.0-rc.1",
    "@angular/forms": "2.4.8",
    "@angular/http": "2.4.8",
    "@angular/material": "2.0.0-beta.2",
    "@angular/platform-browser": "2.4.8",
    "@angular/platform-browser-dynamic": "2.4.8",
    "@angular/platform-server": "2.4.8",
    "@angular/router": "3.4.8",
    "@ionic-native/camera": "3.7.0",
    "@ionic-native/core": "3.7.0",
    "@ionic-native/network": "3.7.0",
    "@ionic-native/splash-screen": "3.7.0",
    "@ionic-native/status-bar": "3.7.0",
    "@ionic/storage": "2.0.0",
    "cordova-plugin-device": "^2.0.1",
    "cordova-plugin-ionic-keyboard": "^2.0.5",
    "cordova-plugin-ionic-webview": "^1.1.16",
    "cordova-plugin-splashscreen": "^5.0.2",
    "cordova-plugin-whitelist": "^1.3.3",
    "hammerjs": "2.0.8",
    "ionic-angular": "2.3.0",
    "ionicons": "3.0.0",
    "material-design-icons": "3.0.1",
    "moment": "2.18.1",
    "moment-duration-format": "1.3.0",
    "ng2-translate": "5.0.0",
    "ng2-webstorage": "1.5.1",
    "ngx-pipes": "1.5.7",
    "ramda": "0.23.0",
    "rxjs": "5.0.1",
    "sw-toolbox": "3.4.0",
    "zone.js": "0.7.2",
    "cordova-android": "~7.0.0"
  },
  "devDependencies": {
    "@ionic/app-scripts": "1.2.2",
    "@types/ramda": "github:types/npm-ramda",
    "@types/moment-duration-format": "1.3.1",
    "typescript": "2.2.1"
  }

tsconfig.json

{
  "compilerOptions": {
    "baseUrl": "./",
    "paths" : {
       "ramda": [
         "location-of-types/npm-ramda-package/index"
       ]
     },
    "allowSyntheticDefaultImports": true,
    "declaration": false,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "lib": [
      "dom",
      "es2015"
    ],
    "module": "es2015",
    "moduleResolution": "node",
    "sourceMap": true,
    "target": "es5"
  },
  "include": [
    "src/**/*.ts"
  ],
  "exclude": [
    "node_modules"
  ],
  "compileOnSave": false,
  "atom": {
    "rewriteTsconfig": false
  }
}

尝试构建生产时出错:

[12:26:31]  typescript error
            Cannot find type definition file for 'ramda'.

Error: Failed to transpile TypeScript

【问题讨论】:

  • 不确定这是否是问题,但您只需要在 dev 中键入 defs .. 尝试重新安装为 dev-dependencies 并尝试
  • @SurajRao 关于这一点,你是对的!我已经在开发依赖项中移动了类型 dep。不幸的是,它没有解决构建问题
  • 在您发布的 tsconfig 文件中,它显示 location-of-types/npm-ramda-package/index。愚蠢的问题,但是您是否在真实代码中将其替换为正确的类型文件夹?
  • 这不是一个愚蠢的问题,我没有。正确的路径是node_modules/@types ?

标签: angular cordova typescript ionic-framework ramda.js


【解决方案1】:

按照此处的安装说明进行操作:https://github.com/types/npm-ramda

# using npm
npm install --save-dev types/npm-ramda#dist

如果不使用 npm/yarn,您可能需要将这些类型添加到路径中 tsconfig.json:

完整包装:

{
  "compilerOptions": {
    "baseUrl": "./",
    "paths" : {
       "ramda": [
         "location-of-types/npm-ramda-package/index"
       ]
     }
  }
}

【讨论】:

  • 嗨!感谢您的回答。不幸的是,我遇到了同样的错误。使用 tsconfig 和 package.json 的一部分更新我的答案
  • location-of-types/npm-ramda-package/index 是占位符,你应该尝试使用@types/ramda/index
  • 这不起作用,是否已从 npm 注册表中删除?
  • 我不知道。也许您可以通过在 GitHub 上提交问题来询问库维护者。 github.com/types/npm-ramda
【解决方案2】:

按照DefinitlyTyped 的建议通过@types/packageName 安装类型。

对于 NPM 做:

npm install @types/ramda --save-dev

对于 YARN 使用:

yarn add @types/ramda --dev

Typescript 默认在 node_modules 中查找名为 @types 的目录以查找类型定义:

默认情况下,所有可见的“@types”包都包含在您的编译中。任何封闭文件夹的 node_modules/@types 中的包都被认为是可见的;具体来说,这意味着 ./node_modules/@types/、../node_modules/@types/、../../node_modules/@types/ 等中的包。

如果指定了 typeRoots,则只包含 typeRoots 下的包。

tsconfig.json docs

【讨论】:

  • 嗨!感谢您的回答。不幸的是,我遇到了同样的错误。使用 tsconfig 和 package.json 的一部分更新我的答案
  • DefinitelyTyped 版本是否与 npm-ramda 相同?有一个关于它的github问题,但我不知道这是否已解决。 github.com/types/npm-ramda/issues/163
猜你喜欢
  • 2018-07-20
  • 2021-08-15
  • 2017-06-18
  • 1970-01-01
  • 2022-11-03
  • 2019-05-15
  • 2017-03-13
  • 2017-06-09
  • 1970-01-01
相关资源
最近更新 更多