I just began using emacs a few days ago and I read somewhere that (require 'package-name)
is not needed when the said package is installed using package-install. But why, what does (require 'package-name)
actually do and how does it differ from load-file
or load
procedures ?

- 3,132
- 2
- 18
- 42
-
14One of the first things we all had to learn in Emacs is `M-x describe-function` to read the doc-string. aka `C-h f` Type `M-x describe-function RET require RET` and read the doc-string. Then type `M-x describe-function RET load-file RET` and read the doc-string. Then type `M-x describe-function RET load RET` and read the doc-string. After reading all three doc-strings, please edit your question to compare and contrast all three functions and clarify what it is that you still do not understand in relation thereto. We employ the same procedure for variables -- i.e., `M-x describe-variable`. – lawlist Jun 02 '16 at 05:52
1 Answers
If you load
a library repeatedly, that file will be read and its code evaluated repeatedly (each and every time you load it).
If you require
a library repeatedly, it will load
the library once (at most -- and not at all if the library had already been loaded).
require
provides this efficiency even if require
wasn't used to load the library initially, because (the vast majority of) libraries contain the code (provide 'FEATURE)
for their particular FEATURE
name. That code is evaluated when the library is loaded, regardless of how it was loaded, and at that time it updates the data which require
checks when deciding whether it needs to do anything.
Mostly you want to use require
in your own code, if you need to ensure that a given library has been loaded.
The reason you often don't need to do this with ELPA packages is that the package manager automatically processes any autoload cookies in the package, and generates a file of autoloads for that package. When the package system is initialised when you start Emacs, the autoload file for each package is evaluated, which identifies all of the autoloaded functions. When an autoloaded function is called, the library which contains the real definition of the function is automatically loaded.
Package authors can add an autoload cookie to each function which the user might be expected to call directly and so, provided that their expectations were correct, you will not need to call require
or otherwise explicitly load the package in order to use it.
Note that you can also define your own custom autoloads. See C-hig (elisp) Autoload
RET and also (elisp) Hooks for Loading
if you wish to use custom lazy-loading and deferred configuration of libraries in your own config. (The latter to say "don't evaluate this code until this library has been loaded", which ties in nicely with the "don't load this library until it's needed" autoload mechanism).

- 48,657
- 3
- 76
- 115
-
2Since you are already referencing the Elisp manual, I think another pertinent node is [`(elisp) Named Features`](https://www.gnu.org/software/emacs/manual/html_node/elisp/Named-Features.html). – Basil Sep 26 '18 at 13:09