module-alias icon indicating copy to clipboard operation
module-alias copied to clipboard

Q: How to support vscode resolution?

Open fritx opened this issue 8 years ago • 18 comments

https://github.com/Microsoft/vscode/issues/14907

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

fritx avatar Nov 26 '17 08:11 fritx

Sorry for the duplication: #9

fritx avatar Nov 26 '17 08:11 fritx

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.

ilearnio avatar Nov 26 '17 14:11 ilearnio

Same question

Rush avatar Dec 26 '17 10:12 Rush

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

ilearnio avatar Dec 26 '17 11:12 ilearnio

No but I know it can't work. Also, I'm working on a project with a pre existing config.

Rush avatar Dec 26 '17 11:12 Rush

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

ilearnio avatar Dec 26 '17 12:12 ilearnio

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

hozjanmarko avatar Apr 09 '18 21:04 hozjanmarko

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.

Rush avatar Jun 09 '18 05:06 Rush

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"
  }
}

ilearnio avatar Jun 09 '18 06:06 ilearnio

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

AkdM avatar Jun 23 '18 12:06 AkdM

@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!

jplew avatar May 07 '19 14:05 jplew

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

Rush avatar May 07 '19 16:05 Rush

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.

TheRealPSV avatar Apr 01 '20 17:04 TheRealPSV

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

tamb avatar May 27 '20 01:05 tamb

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

AK47-dadada avatar Feb 14 '22 01:02 AK47-dadada

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 :(

SergioSuarezGil avatar Mar 22 '22 11:03 SergioSuarezGil

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

sescotti avatar Jul 09 '22 12:07 sescotti

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

youssoufdasilva avatar Aug 05 '22 12:08 youssoufdasilva