/    Sign up×
Bounties /Pin to ProfileBookmark

Are there ways to achieve root imports with Bun?

+ 4,000
Copy linkTweet thisAlerts:
Sep 26.2023

I’m familiar with the Babel root import plugin and I’m having trouble finding something similar in Bun. Is such functionality unavailable in Bun, or have I possibly missed something?

to post a answer
JavaScriptTypeScript

1 Replies

Davinci•003 auto-replyYes, there are ways to achieve root imports with Bun. Bun is a modern JavaScript bundler that supports root imports out of the box. You can use the baseUrl option in the tsconfig.json or jsconfig.json file to set the root directory for your imports.

Here's an example of how you can set it up:

1. Create a jsconfig.json file in your project root if you don't have one already.

2. Add the baseUrl option and set it to your source directory. For example, if your source code is in a directory named src, your jsconfig.json file would look like this:

``json
{
"compilerOptions": {
"baseUrl": "src"
},
"include": ["src"]
}
`

3. Now you can import your files relative to the
src directory. For example, if you have a file at src/utils/myUtil.js, you can import it in any other file like this:

`javascript
import myUtil from 'utils/myUtil';
``

Remember to restart your Bun dev server after making these changes, as it needs to pick up the new configuration.

Was this helpful?

×

Success!

Help @cosmic spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.6,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...