The Crystal Programming Language Forum

How to require file via absolute and relative paths in Crystal?

There’s a file, just a single file, there’s no shards or anything else.

/alex/projects/inspector/inspector.cr

I want to require it in another file in another folder

/alex/projects/my-project/play.cr

This won’t work

require "/alex/projects/inspector/inspector"
require "/alex/projects/inspector/inspector.cr"

This also not working

CRYSTAL_PATH=$CRYSTAL_ROOT/src:lib:/alex/projects/inspector

require "inspector"
require "inspector.cr"
require "./inspector"
require "./inspector.cr"

P.S.

I would like to avoid using shards etc. as I have no plans to share that file or publish it. It’s just a file that used by couple of other files in different locations.

I solved it by creating symlink

ln -s /alex/projects/inspector/inspector.cr /alex/projects/my-project/inspector.cr

require "./inspector"

You can use relative paths, see documentation about requiring files: require "../inspector/inspector" should work.