Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not using paths from tsconfig #219

Closed
Jontem opened this issue Oct 12, 2016 · 6 comments
Closed

Not using paths from tsconfig #219

Jontem opened this issue Oct 12, 2016 · 6 comments

Comments

@Jontem
Copy link
Contributor

Jontem commented Oct 12, 2016

Typescript 2.0 has added support for having modules in other places than node_modules with the paths property. More info can be found in the documentation.

When running with ts-node this does not work. It just gives me the error that the module cannot be found.

test1.ts

import * as foo from "foo";

console.log(foo.hello);

lib/foo/index.ts:

export const hello = "hello";

Then running

ts-node --compilerOptions '{"baseUrl": "./", "paths": {"foo": ["lib/foo"]}}' test1.ts

Same error with this tsconfig.json

{
    "compilerOptions": {
        "baseUrl": "./",
        "paths": {
            "foo": ["lib/foo"]
        }
    }
}

Running tsc works

@blakeembrey
Copy link
Member

blakeembrey commented Oct 12, 2016

This would be a runtime error. TypeScript does not rewrite the paths for you. Node does not know where to find your paths based on your configuration. Please see the existing issue: #138.

@MrCrimp
Copy link

MrCrimp commented Dec 5, 2016

For any search-engine travellers reading this,
This can to some extent be worked around by setting the NODE_PATH to your package.json dir, e.g


tsconfig.json


baseUrl: ".",

"paths": {
      "*": [
        "*"
      ]
    }

package.json

"test": "export NODE_PATH=./ && mocha"

Then imports like these works for ts-node/register runs as well as tools providing resolve paths (babel, webpack)

import * as log from 'src/utils/log'

@mnasyrov
Copy link

Guys, this is a very useful workaround with NODE_PATH should be somewhere in README file. I stuck in two hours with baseUrl/paths problem while not found it in closed issues :)

@jonaskello
Copy link
Contributor

For those interested I did a PR to enable execution of projects with tsconfig paths in #254.

@jonaskello
Copy link
Contributor

@MrCrimp @mnasyrov I also used the NODE_PATH work-around, but this can now be solved easier with the tsconfig-paths package. Just add it as a dev dependency and use the -r tsconfig-paths/register option of ts-node or mocha and all paths will work automatically without NODE_PATH :-).

@bennycode
Copy link

@MrCrimp @mnasyrov I also used the NODE_PATH work-around, but this can now be solved easier with the tsconfig-paths package. Just add it as a dev dependency and use the -r tsconfig-paths/register option of ts-node or mocha and all paths will work automatically without NODE_PATH :-).

Thanks for this hint! I guess to make it work through nyc all it takes is:

.nycrc.json

...
"require": [
  "ts-node/register",
  "tsconfig-paths/register"
],
...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants