Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Relative Paths in require | Understanding and Using Node.js Modules
Working with Modules and Packages in Node.js

bookRelative Paths in require

When you use require with relative paths in Node.js, you tell Node.js exactly where to find your custom modules. Always start a relative path with ./ for the current directory or ../ to move up one directory. This makes your code clear and avoids accidental conflicts with built-in modules or installed packages.

Folder structure.

project/
│
├── app.js
├── utils.js
└── helpers/
    └── math.js
utils.js

utils.js

helpers/math.js

helpers/math.js

app.js

app.js

copy

You should avoid omitting the ./ or ../ when requiring your own files. If you write require('utils') instead of require('./utils'), Node.js will look for a core module or a package in node_modules, not your file. This can lead to errors that are hard to debug.

Keep your project structure organized and use clear, explicit relative paths. If your project grows, consider using absolute paths with tools like path.join or setting up module aliasing, but always be consistent and double-check your paths when moving files around. Remember, relative paths are resolved from the file where require is called, not from where you run your script.

question mark

Which require statement correctly loads a local file named utils.js located in the same directory as the current file, and avoids accidental conflicts with Node.js core modules or installed packages?

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 1. Розділ 5

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

Suggested prompts:

Can you explain how to use absolute paths with `require` in Node.js?

What are some common mistakes when using relative paths in Node.js?

Can you show an example of using `require` with a file in a subfolder?

Awesome!

Completion rate improved to 7.14

bookRelative Paths in require

Свайпніть щоб показати меню

When you use require with relative paths in Node.js, you tell Node.js exactly where to find your custom modules. Always start a relative path with ./ for the current directory or ../ to move up one directory. This makes your code clear and avoids accidental conflicts with built-in modules or installed packages.

Folder structure.

project/
│
├── app.js
├── utils.js
└── helpers/
    └── math.js
utils.js

utils.js

helpers/math.js

helpers/math.js

app.js

app.js

copy

You should avoid omitting the ./ or ../ when requiring your own files. If you write require('utils') instead of require('./utils'), Node.js will look for a core module or a package in node_modules, not your file. This can lead to errors that are hard to debug.

Keep your project structure organized and use clear, explicit relative paths. If your project grows, consider using absolute paths with tools like path.join or setting up module aliasing, but always be consistent and double-check your paths when moving files around. Remember, relative paths are resolved from the file where require is called, not from where you run your script.

question mark

Which require statement correctly loads a local file named utils.js located in the same directory as the current file, and avoids accidental conflicts with Node.js core modules or installed packages?

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 1. Розділ 5
some-alt