【问题标题】:Jest cannot find module from node_modulesJest 无法从 node_modules 中找到模块
【发布时间】:2019-04-27 06:09:49
【问题描述】:

我正在创建 typescript 基础 npm 模块,但我坚持玩笑话测试。我正在尝试包含来自https://github.com/types/jsonld 的 jsonld 模块,一切似乎都正常,但是当我运行命令yarn test 时,我收到错误消息Cannot find module 'jsonld' from 'DidDocument.ts'

package.json

{
    "name": "defined-id",
    "version": "1.0.0",
    "description": "Defined ID",
    "main": "lib/index.js",
    "types": "lib/index.d.ts",
    "scripts": {
        "test": "jest --config jestconfig.json",
        "build": "tsc",
        "format": "prettier --write \"src/**/*.ts\" \"src/**/*.js\"",
        "lint": "tslint -p tsconfig.json",
        "prepare": "yarn run build",
        "prepublishOnly": "yarn test && yarn run lint",
        "preversion": "yarn run lint",
        "version": "yarn run format && git add -A src",
        "postversion": "git push && git push --tags"
    },
    "repository": {
        "type": "git",
        "url": "git+https://github.com/VSaulis/defined-id.git"
    },
    "keywords": [
        "DID",
        "DID document"
    ],
    "author": "Vytautas Saulis",
    "license": "ISC",
    "bugs": {
        "url": "https://github.com/VSaulis/defined-id/issues"
    },
    "homepage": "https://github.com/VSaulis/defined-id#readme",
    "devDependencies": {
        "@types/jest": "^23.3.9",
        "jest": "^23.6.0",
        "prettier": "^1.15.2",
        "ts-jest": "^23.10.5",
        "tslint": "^5.11.0",
        "tslint-config-prettier": "^1.16.0",
        "typescript": "^3.1.6"
    },
    "dependencies": {
        "@types/jsonld": "github:types/jsonld",
        "@types/uuid": "^3.4.4"
    }
}

jestconfig.json

{
    "transform": {
        "^.+\\.(t|j)sx?$": "ts-jest"
    },
    "testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
    "moduleFileExtensions": ["ts", "tsx", "js", "jsx", "json", "node"],
    "moduleDirectories": ["node_modules", "src"]
}

DidDocument.ts

import * as jsonld from 'jsonld';
import { Did } from './Did';
import { PublicKey } from './PublicKey';

export class DidDocument {
    private context: string[] = ["https://w3id.org/did/v1", "https://w3id.org/security/v1"];
    private did: Did;
    private publicKeys: PublicKey[];

    constructor(did: Did, publicKeys: PublicKey[]) {
        this.did = did;
        this.publicKeys = publicKeys;
    }

    public format(): void {

        const doc = {
            "http://schema.org/id": this.did.format(),
            "publicKey": this.publicKeys.map((publicKey: PublicKey) => JSON.stringify(publicKey.format()))
        };

        return jsonld.compact(doc, JSON.stringify(this.context), () => { return; });
    }
}

我预测 jsonld 模块不在根目录中,而是在 js/ 目录中。但是当我将导入更改为jsonld/js 时,问题仍然相同。

【问题讨论】:

  • 您找到解决方案了吗?我也面临着类似的问题。
  • 这里也一样,找不到解决办法。

标签: typescript types jestjs json-ld


【解决方案1】:

如果有人对无法通过开玩笑解决的软件包有问题,例如。 g.

    Cannot find module '@private-registry/private-package' from 'user_block.vue'

  13 |     >
  14 |       <template v-slot:activator="{ on }">
> 15 |         <v-avatar
     |                    ^
  16 |           :color="white ? '#f2f2f2' : color"
  17 |           :class="[
  18 |             'mp-user-avatar',

  at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:259:17)
  at src/components/lib/components/user_block.vue:15:20
  at Object.<anonymous> (src/components/lib/components/index.ts:75:42)

然后您应该检查已安装的包文件夹中的 package.json main 字段(在该示例中,它是 ./node_modules/@private-registry/private-package/package.json。如果没有主字段或主字段指向错误(不存在)的文件,那就是问题。至少在我的情况下是个问题。


如果这没有帮助,您也可以像我一样尝试使用console.log 穿过node_modules/jest-resolve/build/index.js 和node_modules/jest-resolve/build/defaultResolver.js。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-04
    • 2023-01-13
    • 1970-01-01
    • 1970-01-01
    • 2020-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多