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

Q: How to support vscode resolution? #19

Open
fritx opened this issue Nov 26, 2017 · 18 comments
Open

Q: How to support vscode resolution? #19

fritx opened this issue Nov 26, 2017 · 18 comments

Comments

@fritx
Copy link

fritx commented Nov 26, 2017

microsoft/vscode#14907

A jsconfig.json won't work for me, any idea?

@fritx fritx changed the title How to support vscode resolution? Q: How to support vscode resolution? Nov 26, 2017
@fritx
Copy link
Author

fritx commented Nov 26, 2017

Sorry for the duplication: #9

@ilearnio
Copy link
Owner

You can try to use addAliases method, not sure if it will work though.
If you are working on a plugin for vscode then it is probably a bad idea to use module-alias since it modifies require behavior globally.

@Rush
Copy link

Rush commented Dec 26, 2017

Same question

@ilearnio
Copy link
Owner

@Rush Have you tried to use moduleAlias.addAlias('@alias', __dirname + '/path')?

@Rush
Copy link

Rush commented Dec 26, 2017 via email

@ilearnio
Copy link
Owner

ilearnio commented Dec 26, 2017

When in Node and when having the aliases set in package.json, it also runs addAlias behind the scenes.

No but I know it can't work

It can not work in vscode because there might be another require/CommonJS implementation, never tried, but you can try to do as I suggested

@hozjanmarko
Copy link

If you are writing typescript, then add your module aliases as short imports in tsconfig, no more complaining from vs code and also auto importing(auto complete) will work as expected

@Rush
Copy link

Rush commented Jun 9, 2018

Hey y'all. I built an alternative. https://www.npmjs.com/package/link-module-alias

I'm using it currently in a project alongside module-alias but we may switch fully to link-module-alias if no issues are found.

@ilearnio
Copy link
Owner

ilearnio commented Jun 9, 2018

One of my colleagues uses this to support aliases in vscode. Never tried myself but it works for him

// .vscode/settings.json

{
  "path-autocomplete.pathMappings": {
    "@": "${folder}/src",
    "assets": "${folder}/src/assets"
  }
}

@AkdM
Copy link

AkdM commented Jun 23, 2018

@Rush Thanks, I'm using your package instead! It works well and VSCode auto completes the folders and files.

@jplew
Copy link

jplew commented May 7, 2019

@Rush can confirm that your alternative addresses all of the difficulties I've been having with this package, as documented in #58 . I especially appreciate how you preserved the API, makes swapping them a breeze. Thanks!

@Rush
Copy link

Rush commented May 7, 2019

@jplew I'm glad it solves your issues. Please take note of this caveat but otherwise do enjoy.

@TheRealPSV
Copy link

Simple way to do this with a jsconfig.json file:

In your package.json:

{
...
    "_moduleAliases": {
        "@root": ".",
        "@handlers": "./js/handlers",
        "@utils": "./js/utils"
    },
...
}

In jsconfig.json:

{
    "compilerOptions": {
        "module": "commonjs",
        "target": "es6",
        "baseUrl": "./",
        "paths": {
            "@root/*": ["./*"],
            "@handlers/*": ["./js/handlers/*"],
            "@utils/*": ["./js/utils/*"]
        }
    },
    "exclude": ["node_modules", "**/node_modules/*"]
}

Basically, you can add the baseUrl as your local path, and copy over your aliases into the paths section. For each alias, you have to add /* to the end of the alias, and turn the path you're aliasing into an array containing that path, then add /* to the end of it as well.

It's a little extra work every time you want to add an alias, but it works really well, no extra extension required, and I'm using it now.

If there were a way to make this happen automatically (update the paths value when you update the aliases in package.json), that'd be fantastic.

@tamb
Copy link

tamb commented May 27, 2020

Hey y'all. I built an alternative. https://www.npmjs.com/package/link-module-alias

I'm using it currently in a project alongside module-alias but we may switch fully to link-module-alias if no issues are found.

Can confirm this works!
Using with Create React App with TypeScript in VSCode

@AK47-dadada
Copy link

Simple way to do this with a jsconfig.json file:

In your package.json:

{
...
    "_moduleAliases": {
        "@root": ".",
        "@handlers": "./js/handlers",
        "@utils": "./js/utils"
    },
...
}

In jsconfig.json:

{
    "compilerOptions": {
        "module": "commonjs",
        "target": "es6",
        "baseUrl": "./",
        "paths": {
            "@root/*": ["./*"],
            "@handlers/*": ["./js/handlers/*"],
            "@utils/*": ["./js/utils/*"]
        }
    },
    "exclude": ["node_modules", "**/node_modules/*"]
}

Basically, you can add the baseUrl as your local path, and copy over your aliases into the paths section. For each alias, you have to add /* to the end of the alias, and turn the path you're aliasing into an array containing that path, then add /* to the end of it as well.

It's a little extra work every time you want to add an alias, but it works really well, no extra extension required, and I'm using it now.

If there were a way to make this happen automatically (update the paths value when you update the aliases in package.json), that'd be fantastic.

Hey, I had this problem
it still can't navigate to the specified file by CTRL+ CLICK

@SergioSuarezDev
Copy link

Simple way to do this with a jsconfig.json file:
In your package.json:

{
...
    "_moduleAliases": {
        "@root": ".",
        "@handlers": "./js/handlers",
        "@utils": "./js/utils"
    },
...
}

In jsconfig.json:

{
    "compilerOptions": {
        "module": "commonjs",
        "target": "es6",
        "baseUrl": "./",
        "paths": {
            "@root/*": ["./*"],
            "@handlers/*": ["./js/handlers/*"],
            "@utils/*": ["./js/utils/*"]
        }
    },
    "exclude": ["node_modules", "**/node_modules/*"]
}

Basically, you can add the baseUrl as your local path, and copy over your aliases into the paths section. For each alias, you have to add /* to the end of the alias, and turn the path you're aliasing into an array containing that path, then add /* to the end of it as well.
It's a little extra work every time you want to add an alias, but it works really well, no extra extension required, and I'm using it now.
If there were a way to make this happen automatically (update the paths value when you update the aliases in package.json), that'd be fantastic.

Hey, I had this problem it still can't navigate to the specified file by CTRL+ CLICK

Same here, can't go to file using CTRL+CLICK on imports in viscose using module aliases :(

@sescotti
Copy link

sescotti commented Jul 9, 2022

In case someone still has issues with a typescript project, this what I did to make it work:

  • Add _module-alias to package.json:
  "_moduleAliases": {
    "@app": "src/app",
    "@commons": "src/commons"
  }
  • Add compilerOptions.paths to tsconfig.json:
{
  "compilerOptions": {
    "paths": {
      "@app/*": ["./src/app/*"],
      "@commons/*": ["./src/commons/*"],
    }
}
  • Go to Terminal > Run Task (⌘ + ⇧ + P on Mac) and write Restart TS server.

⚠️ This last step is necessary, otherwise VSCode might not pick up the changes on your package.json/tsconfig.json

@youssoufdasilva
Copy link

youssoufdasilva commented Aug 5, 2022

@SergioSuarezDev this worked for me. I just restarted the TS server like @sescotti mentioned

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

No branches or pull requests