Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jest cannot find module from node_modules

I'm creating typescript base npm module, but i stuck with jest tests. I'm trying to include jsonld module from https://github.com/types/jsonld and everything seems work fine, but when i run command yarn test i have error message 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; });
    }
}

I have prediction that jsonld module is not in root directory but in js/ directory. But when i changed import to jsonld/js issues still the same.

like image 320
Vytautas Saulis Avatar asked Nov 07 '22 23:11

Vytautas Saulis


1 Answers

If someone has a problem with a package not resolved by jest, e. 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)

Then you should check the package.json main field inside the installed package folder (with that example, it's ./node_modules/@private-registry/private-package/package.json. If there's no main field or the main field points to a wrong (nonexistent) file, then that's the problem. It was a problem in my case at least.


If that doesn't help, you can also try to walk with a console.log through node_modules/jest-resolve/build/index.js and node_modules/jest-resolve/build/defaultResolver.js like I did.

like image 71
Crysknight Avatar answered Nov 15 '22 10:11

Crysknight