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

Using ts-node with @ imports #1

Closed
robert-cronin opened this issue Jun 4, 2020 · 4 comments · Fixed by MatrixAI/Polykey#40
Closed

Using ts-node with @ imports #1

robert-cronin opened this issue Jun 4, 2020 · 4 comments · Fixed by MatrixAI/Polykey#40
Assignees
Labels
bug Something isn't working

Comments

@robert-cronin
Copy link
Contributor

robert-cronin commented Jun 4, 2020

Note: this issue is just for documentation and has a solution already:

The issue in question is an error that occurs when using imports that use the @ symbol to denote the module (e.g. import PolyKey from @polykey/PolyKey.ts). It turns out ts-node doesn't like this and throws an error. There is a whole issue about tsconfig.json/paths not working with ts-node. The solution is to use tsconfig-paths and then suffix all your calls with ts-node -r tsconfig-paths/register <file>. One can do this with a handy alias: alias ts-node='ts-node -r tsconfig-paths/register'.

For webpack, there is also a module for this: https://github.com/dividab/tsconfig-paths-webpack-plugin

@robert-cronin robert-cronin added the bug Something isn't working label Jun 4, 2020
@robert-cronin robert-cronin self-assigned this Jun 4, 2020
@robert-cronin
Copy link
Contributor Author

I've also been running into an issue with typescript not picking up the module level imports in the tests directory since it is outside of the src directory. The fix for this is to have the baseUrl set to '.' and the module path then start from src like so:

{
  "compilerOptions": {
    .....
    "baseUrl": ".",
    "paths": {
      "@polykey/*": ["src/*"]
    }
  },
  ....
}

@robert-cronin
Copy link
Contributor Author

robert-cronin commented Jun 4, 2020

I forgot to mention the tests directory should also be added into the glob wildcards patter in include:

{
  "compilerOptions": {
    .....
    "baseUrl": ".",
    "paths": {
      "@polykey/*": ["src/*"]
    }
  },
  "include": [
    "./src/**/*",
    "./tests/**/*"
  ]
}

There was mention of using jests moduleNameMapper but I have just opted to use standard paths mapping without the helper. For PolyKey, it looks like this in jest.config.js:

module.exports = {
  ...,
  moduleNameMapper: {
    '@polykey/(.*)$': '<rootDir>/src/$1'
  }
};

@robert-cronin
Copy link
Contributor Author

There is still the issue of the bin folder, commanderjs does not appear to have a plugin for tsconfig paths like webpack and jest do

@robert-cronin
Copy link
Contributor Author

robert-cronin commented Jun 15, 2020

I'm not sure what it is, but the relative imports show errors in VSCode when used in the test directory unless I include the tests directory as shown two commends above. This is fine until I try to generate typings since now tsc generates typings for the test directory as well which is undesirable.

I think the solution for now is to just to use the very common way of importing outside of the src directory:
import Polykey from '../src/Polykey'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Development

Successfully merging a pull request may close this issue.

1 participant