How do I get WebStorm-added JavaScript imports to include a leading forward slash?

When I paste code or use WebStorm to add an import, it adds it relative to my project root, which is what I want, but I always have to manually add the leading slash. In other words, it'll add:

import { foo } from 'imports/api/foo.js' // error

when what I want is:

import { foo } from '/imports/api/foo.js' // leading slash 👍

My settings are pictured below. Is anyone aware of a solution for this?

My project is Meteor-based ("MeteorJS").

Thank you.

0
2 comments

With the Use paths relative to the project, resource or source root option enabled, all imports are generated with relative paths; there is no way to turn them into absolute URLs with leading slashes.

As a workaround, you can try defining slash as a path prefix in  jsconfig.json and disabling the Use paths relative to the project option, like:

 

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

Oh my god, that worked. Bless your heart. 🥲🥲🥲🥲

0

Please sign in to leave a comment.