Because I am an Emacs user I wrote a simple script to automatically generate the import statment with the relative file path to the file. The scripts uses ido to let the user select the js-file in the projectile project, and then calculates the relative path and inserts it wherever the cursor currently is.
I am just going to leave the script here: (EDIT: I created js-import if you want to use this!)
(require 'f) (require 'json) (require 'subr-x) (require 'projectile) (defun js/get-project-dependencies () (let ((json-object-type 'hash-table)) (hash-table-keys (gethash "dependencies" (json-read-from-string (f-read-text (concat (projectile-project-root) "package.json") 'utf-8)))))) (defun string-ends-with-p (string suffix) "Return t if STRING ends with SUFFIX." (and (string-match (rx-to-string `(: ,suffix eos) t) string) t)) (defun js/is-js-file (file) (or (string-ends-with-p file ".js") (string-ends-with-p file ".jsx"))) (defun js/import () (interactive) (let* ((filtered-project-files (-filter 'js/is-js-file (projectile-current-project-files))) (all (append (js/get-project-dependencies) filtered-project-files)) (selected-file (ido-completing-read "Select a file to import: " all)) (selected-file-name (f-filename (f-no-ext selected-file))) (selected-file-relative-path (f-relative (concat (projectile-project-root) (f-no-ext selected-file)) (file-name-directory (buffer-file-name))))) (insert (concat "import " selected-file-name " from \"" (if (js/is-js-file selected-file) (concat "./" selected-file-relative-path) selected-file-name) "\";"))))