DEV Community

Brian Hanna
Brian Hanna

Posted on

Import dependencies with Svelte and Rollup?

I'm trying out Svelte. I've followed the instructions on getting a starter project going. I've made some minor customizations, and added one library to help with making fetch requests (build-url).

Everything works great! I'm able to import build-url using this syntax in my Home.svelte file:

import buildUrl from '../node_modules/build-url';

However, I'm coming from Angular; I'm very, very used to omitting the path the node_modules from my code entirely; Angular just finds what it needs. My ideal end state would be the following:

import buildUrl from 'build-url';

Is there a Rollup configuration I can use to mimic this behavior? I'm not against using a path to the node_modules folder, but I can't help but feel like I'm doing it wrong. If I can't do it with Rollup, is it possible with another bundler like webpack or Parcel?

Top comments (1)

Collapse
 
richharris profile image
Rich Harris

If you're using the starter template, you should already have rollup-plugin-node-resolve and rollup-plugin-commonjs plugins installed, it should Just Work. Indeed, adding

import buildUrl from 'build-url';
Enter fullscreen mode Exit fullscreen mode

to a component in a fresh project works exactly as you'd expect. Are you getting any error messages? Have you done npm install build-url?

For future reference, questions like these are better asked over on Stack Overflow using the svelte tag, or in svelte.dev/chat, than on dev.to which is more for blog posts etc than support requests.