-
-
Notifications
You must be signed in to change notification settings - Fork 534
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
tsconfig.json/paths not working with ts-node #138
Comments
@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? |
I see. In order for it to work, a |
This module uses the emitted code though - it's TypeScript itself that doesn't do any rewriting right? |
Just check. Nope it does not do any rewriting. Seems like the |
Updated https://github.com/unional/blue-tape-fixture/tree/ts-node to demo the issue. |
Confirm above statement: microsoft/TypeScript#5039 (comment) |
@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. |
any news? i want to use 'paths' option in my tests |
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. |
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. |
When I looked closer I realised that wrapping global require is not possible in a way that replaces it (the example above installs a separate require function). However it is possible to patch |
For those interested I did a PR to enable execution of projects with tsconfig paths in #254. |
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. |
Note to all using @jonaskello 's tsconfig-paths: The errror "
Hope this helps another overwhelmed soul. |
@shirakaba Just a note, you can set your "paths": {
"*" : ["./*"]
} I will be the same, but you are able to add more paths with the same root more easily. |
I've tried nearly all the above solutions and I'm still getting What's the solution here? I have updated my
|
@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. |
@ashok-sc As @jonaskello said, there is no plan to support path, as you need to replace the path call with the proper full path in order to be understandable for Webpack. You could use Awesome Typescript Loader that have a plugin out-the-box for that, but still, it needs to be a plugin. |
Hey @jonaskello and @michaeljota thanks for the help! I am in fact using awesome-typescript-loader with webpack. However, the instructions are quite poor. I understand that you may not have the answers here, but I thought I'd give it a shot anyway. The instructions state to add the TsConfigPathsPlugin, but it's not clear as to what it needs in the constructor. Here's what I tried:
This doesn't seem to work though and I still get module not found. Anyway, if you don't have the answer of the top of your head feel free to ignore. |
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.
|
@ashok-sc If it does not work, you can try version 0.2.0 of tsconfig-paths-webpack-plugin. I have added code that prints the path that it tries to load the tsconfig.json from. Note, |
@alexeychikk this thread confirms that the maintainer is not interested in any implementation of TS paths support, but he suggests external implementations instead as you have enumerated. Sure, if the people wants to run TS code directly via ts-node is dividab/tsconfig-paths an alternative . I use it for development. My solution is pretty simple: replace TS path aliases using a |
Oh boy. This looks like it's going to be fun to figure out. |
|
@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. |
Thanks @jonaskello! It also seems to work when using nyc.config.js module.exports = {
all: false,
exclude: ['**/*.d.ts', '**/*.test*.ts', '**/index.ts'],
extension: ['.ts'],
include: ['src/**/*.ts'],
reporter: ['text-summary'],
require: ['tsconfig-paths/register', 'ts-node/register'],
}; |
See TypeStrong/ts-node#138 This allows us to use custom type declarations and have the compilers work consistently between tsc, ts-node, mocha and cucumber It does mean we'll need to remember to add the require for tsconfig-paths in a couple of other places, probably, like where we use mocha. Closes #151
Thank you to janaskello for I'd also like to say that on newer versions of node you get a I would vote for ts-node refusing to run if paths is set given that this error is a little cryptic (I didn't find this page until I guessed that the problem might be because ts-node is ignoring paths and googled this - working in someone elses repo). |
I was running on the same issue when running webpack with ts-node (i'm using Typescript language webpack configuration) but already followed the webpack documentation and installed Just putting Hope this help |
Apparently I've been through this before... lol. This really probably should become default behavior. |
Okay, so I found a solution that works reasonably well for my use-case. I needed this just for Knex's Constraints:
Codeimport moduleAlias from 'module-alias';
import * as path from 'path';
import { compilerOptions } from './tsconfig.json';
const root = path.join(__dirname, compilerOptions.baseUrl || '');
for (const [key, paths] of Object.entries(compilerOptions.paths)) {
const target = path.join(root, paths[0]);
moduleAlias.addAlias(key, target);
} |
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 |
maybe my workaround will help:
My conclusion: I hope this solution will help, |
Does anyone have an idea on how to work around this issue when using ESM? I am normally using |
@fatihaziz you answer saved my life, thanks! |
@puchm Did you ever manage to find a solution to this? I'm having the same issue |
One neat trick to use
tsconfig.json/paths
is to pointing the module name to your source file: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:
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
The text was updated successfully, but these errors were encountered: