Ts-node, how to use tsconfig-paths with node_modules?

I have been stuck on this for some time. Perhaps someone has an eye for where lies the problem?

ts-node -P tsconfig_test.json -r tsconfig-paths/register assets/js/test/unit_tests/tests/test_poseidon_navigator.ts
error TS2307: Cannot find module ‘enzyme’.

tsconfig_test.json:

{
    "compilerOptions": {
        "target": "es6",
        "module": "es6",
        "noResolve": false,
        "noImplicitAny": false,
        "removeComments": true,
        "preserveConstEnums": true,
        "allowJs": true,
        "sourceMap": true,
        "baseUrl": "./",
        "paths": {
            "*": [
                "node_modules/*"
            ]
        }
    },
    "include": [
        "assets/js/**/*.ts"
    ]
}

I progressed. If I use the following path, the error disappears. But not shorter path can be used.

            "enzyme": [
                "node_modules/enzyme/src/index.js"
            ]

This means that the standard way node_modules is resolved by node is clever. Or maybe it is because the extension is in .js and not in .ts.

I don’t want to have to pass the details “/src/index.js” since different packages may have a different sub-path.

Any advice?

I think now it is not related to tsconfig-paths.

Because in the test file, I can write the following and it works:
import “enzyme/src/index.js”

But not a shorter import “enzyme”.

Any advice?

I suspect it is linked to the .js extensions.

And also we can use node -r ts-node/register as an alternative. I will try.

It seems to me that it is in fact expected behaviour. Here they use full paths for jquery.

1 Like

So this is solved. As said in the doc of typescript, one must specify paths in the config. And the build will not be clever. Hard-coded paths are needed.

        "paths": {
            "*": [
                "node_modules/*/index.js",
                "node_modules/*/src/index.js",
                "node_modules/*/lib/sinon.js"
            ],
            "AssetsJsAlias/*": [
                "assets/js/*.js"
            ]
        }
1 Like

this is how to run mocha with typescript and babel (for plugins like rewire, async, etc) for running tests:

TS_NODE_PROJECT=tsconfig_test.json BABEL_ENV=test mocha --require ts-node/register --require tsconfig-paths/register --require babel-core/register assets/js/test/unit_tests/tests/test_poseidon_navigator.ts

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.