ts-node: tsconfig.json/paths not working with ts-node
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:
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
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 153
- Comments: 48 (16 by maintainers)
Commits related to this issue
- Enable TypeScript paths to be used in tests Related link: * https://github.com/TypeStrong/ts-node/issues/138#issuecomment-296765802 — committed to DimiTech/node-typescript-boilerplate by deleted user 5 years ago
- Add package tsconfig-paths and integrate it into ts-test script Ts-node does not load the paths from tsconfig.json by itself. Thus in the tests run with ts-node the custom types for external packages... — committed to thenativeweb/wolkenkit by yeldiRium 5 years ago
- Switch to TypeScript, and start to implement wolkenkit as a monorepo. (#183) * WIP * WIP * WIP * Clean up dependencies. * Add worker messaging. * Add CircleCI configuration. * Fix C... — committed to thenativeweb/wolkenkit by goloroden 5 years ago
- Use allow ts-node to use tsconfig paths See https://github.com/TypeStrong/ts-node/issues/138 This allows us to use custom type declarations and have the compilers work consistently between tsc, ts-n... — committed to SmartBear/git-en-boite by mattwynne 4 years ago
Instead of the PR, I have now created the tsconfig-paths package that makes it possible to automatically resolve paths during execution with ts-node, node, or mocha. Just follow the instructions in the readme.
@jonaskello You saved my ass here!
Is there any reason why this shouldn’t become a default built in part of ts-node? It seems like it would make more sense to include this by default, than to not.
Note to all using @jonaskello 's tsconfig-paths:
The errror “
Missing paths in compilerOptions. tsconfig-paths will be skipped
” means that you don’t have anypaths
property specified in your tsconfig.json. I had been able to use imports relative to mysrc
directory just by specifying"baseUrl": "."
, but to get ts-node/node/mocha to understand the same imports, I also needed to specify apaths
property in my tsconfig.json, eg:Hope this helps another overwhelmed soul.
maybe my workaround will help:
install required plugins:
npm i -D ttypescript typescript-transform-paths ts-node tsconfig-paths
. these are packages that will help us to transform the paths.then, on tsconfig.json, i put:
then we can use this on our codes everywhere:
then to compile, we need to run
npx ttsc -p tsconfig.json
, we use npx so we dont need to install ttypescript globallyand to run on ts-node, we can simply run
npx ts-node -p tsconfig.json src/app.ts
, also npx for ts-node.My conclusion: To workaround with ts-node paths, we need tsconfig-paths required on ts-node runtime. but on production we just need to compile them into absolute path, so we use ttypescript and typescript-transform-paths to compile and transform the module paths to absolute path. then the program can work fine with
node
without registering tsconfig-paths or any plugins likenode lib/index.js
I hope this solution will help, also sorry if my word kinda messed up, i try to explain it as simple as possible.
@ashok-sc ts-node has no support for paths in tsconfig.json, and no plans to add it. I think your best bet to get it working is using tsconfig-paths. If you have trouble using it feel free to file an issue at that repo.
If you are using webpack the scenario is quite different. If you are using ts-loader you might want to try tsconfig-paths-webpack-plugin altough it is still early in development. If you are using awesome typescript loader then it has this type of plug-in built-in.
Clearly I’m a weirdo. This is my fourth time finding this thread, but it doesn’t appear to get much activity. It’s probably because I use mono-repos.
For future me coming back here, this is the link: https://www.npmjs.com/package/tsconfig-paths
@joseluisq That doesn’t address the issue in any possible way. People here want to run typescript code directly with a support of
paths
aliases from tsconfig.json for the dev purposes and not only. Specifically I am facing the issue of wrong paths resolution whenoutDir
is specified in tsconfig.json.By the way, there is already a proven solution to the problem you referenced https://github.com/ilearnio/module-alias or https://github.com/dividab/tsconfig-paths or even for webpack https://github.com/dividab/tsconfig-paths-webpack-plugin
Apparently I’ve been through this before… lol.
This really probably should become default behavior.
Does anyone have an idea on how to work around this issue when using ESM? I am normally using
node --loader ts-node/esm <path>
to start scripts but I could not get it to work using any of the solutions proposed here.Oh boy. This looks like it’s going to be fun to figure out.
with baseUrl enabled it seems @jonaskello’s plugin will also use this for npm_modules that otherwise would load normally. When removing baseUrl and paths all together and using relative paths the project compiles again.
and with baseUrl and paths disabled:
part of my package.json
Okay, so I found a solution that works reasonably well for my use-case. I needed this just for Knex’s
knexfile.ts
and couldn’t find a solutionConstraints:
module-alias
as a dev dependencyfix-paths.ts
file works if it is is imported first"resolveJsonModule": true
Code
For a solution you can use today, see #1450
Path mapping is being added as a built-in feature of ts-node in an upcoming release. To follow progress, see #1585
Thanks @jonaskello!
It also seems to work when using
ts-node
throughnyc
:nyc.config.js
Here’s my tsconfig.json:
Here’s a gist of what a webpack config should look like with awesome-typescript-loader’s tsconfig paths plugin. I don’t pass anything in the constructor in my config, but maybe you need it if your tsconfig.json is in a non-standard location.
@shirakaba Just a note, you can set your
"baseUrl"
to"./src/"
and"paths"
like thisI will be the same, but you are able to add more paths with the same root more easily.