Skip to content

tsconfig.json/paths not working with ts-node #138

Closed
rootstrap/node-ts-api-base
#1
@unional

Description

@unional
Contributor

One neat trick to use tsconfig.json/paths is to pointing the module name to your source file:

// tsconfig.json of blue-tape-fixture
{
  ...
  "paths": {
    "blue-tape-fixture": [ "src/" ]
  }
}

This way, your test files can reference the source as if it is a proper module:
image
You can see it is working in vscode. But it does not when I run test with ts-node:

// package.json
{
  "scripts": {
    "test": "ts-node -P tsconfig.build.json node_modules/blue-tape/bin/blue-tape \"test/**/*.ts\" | tap-spec"
  }
}

The error is:

Error: Cannot find module 'blue-tape-fixture'
    at Function.Module._resolveFilename (module.js:325:15)
    at Function.Module._load (module.js:276:25)
    at Module.require (module.js:353:17)

You can see it here: https://github.com/unional/blue-tape-fixture/tree/ts-node

# clone
npm install
npm test

Activity

blakeembrey

blakeembrey commented on Jun 19, 2016

@blakeembrey
Member

@unional That's the node.js error - TypeScript is working fine, but node will never be able to resolve an alias like that unless TypeScript emits code that rewrites paths. What do you think is the solution here?

unional

unional commented on Jun 19, 2016

@unional
ContributorAuthor

I see. In order for it to work, a module-path-rewriter need to be written and loaded by ts-node.
Alternatively is to load the transpiled code dist/*, which kind of defeat the purpose of ts-node (I said kind of because the test files, in this use case, can remain as ts files).

blakeembrey

blakeembrey commented on Jun 19, 2016

@blakeembrey
Member

This module uses the emitted code though - it's TypeScript itself that doesn't do any rewriting right?

unional

unional commented on Jun 20, 2016

@unional
ContributorAuthor

Just check. Nope it does not do any rewriting. Seems like the baseUrl and paths is only used during compilation. By design or bug on TS side?

unional

unional commented on Jun 20, 2016

@unional
ContributorAuthor

Updated https://github.com/unional/blue-tape-fixture/tree/ts-node to demo the issue.
run npm run build-test and see under dist/test/index.ts. It is still requiring blue-tape-fixture

unional

unional commented on Jun 20, 2016

@unional
ContributorAuthor

Confirm above statement: microsoft/TypeScript#5039 (comment)

blakeembrey

blakeembrey commented on Jun 20, 2016

@blakeembrey
Member

@unional Yeah, it's tricky. I can understand TypeScript's stance, if they don't have a solution for it built-in I think we can investigate adding a plugin once they implement the pluggable transforms in TypeScript 2.0. This is definitely more of a user-land feature.

added
researchNeeds design work, investigation, or prototyping. Implementation uncertain.
on Jul 23, 2016
sanex3339

sanex3339 commented on Oct 2, 2016

@sanex3339

any news? i want to use 'paths' option in my tests

blakeembrey

blakeembrey commented on Oct 2, 2016

@blakeembrey
Member

No, it does not work and is not expected to work. Someone can still implement a plugin (which would be very valuable to others too!), but I am not currently working on it.

jonaskello

jonaskello commented on Dec 26, 2016

@jonaskello
Contributor

Maybe this could be solved by augmenting node's loading rather than having typescript re-write the paths. There are some alternatives mentioned here. In particular I'm thinking that wrapping the global require and just changing the path according to tsconfig would be the simplest solution.

64 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    externalresearchNeeds design work, investigation, or prototyping. Implementation uncertain.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @cspotcode@xvilo@bennycode@jonaskello@blakeembrey

      Issue actions

        tsconfig.json/paths not working with ts-node · Issue #138 · TypeStrong/ts-node