JavaScript: Packages
As a program grows, it is not only the number of lines of code that increases, but also the number of modules. In small projects you can keep dozens of files side by side, but real applications can have hundreds and thousands of files. On top of that, useful code is something you want to reuse across different projects.
In Python, packages-as-directories are used to group modules. In the JavaScript ecosystem, the unit of reuse and distribution is a package (an npm package). A package is a set of modules that is published and installed as a whole.
What a package is
A package is described by a package.json file and installed with the npm install command into the node_modules directory. That is where its modules are imported from. One such package is hexlet-basics, the one we import functions from in this course.
Importing by package name
When a package is installed, its contents are imported by the package name rather than by a file path:
import { round } from 'hexlet-basics/math';
console.log(round(3.14159, 2)); // => 3.14One package, several modules
A package usually contains more than one module. To pick a specific module inside a package, you add a path after its name with /:
import { reverse } from 'hexlet-basics/string'; // the string module of the hexlet-basics package
import { round } from 'hexlet-basics/math'; // the math module of the same packageThe part before / is the package name, and the part after it is the module inside it. This way a single package can bundle many modules grouped by task.
package.json
A package describes itself in a package.json file. It specifies the name, the dependencies, and which modules the package exposes:
{
"name": "hexlet-basics",
"dependencies": {},
"exports": {
"./string": "./src/hexlet/string.js",
"./math": "./src/hexlet/math.js"
}
}The exports field defines which paths are available on import. Thanks to it, writing hexlet-basics/string resolves to the right file inside the package.
node_modules and the npm registry
Installed packages end up in the node_modules directory. You don't write them by hand — the main source is the npm registry, where hundreds of thousands of ready-made packages are published: for working with dates, networking, testing, and almost any other task.
Node.js built-in modules (node:fs, node:path, and others) stand apart. You don't need to install them — they ship with Node.js, like a standard library.
Instructions
Implement the formatPrice() function. It takes a number and returns its string representation rounded to two decimal places.
Use the round() function from the hexlet-basics/math module — this is another module of the same hexlet-basics package you worked with in the previous lesson. You need to write the import yourself.
formatPrice(12.3456); // => '12.35'
formatPrice(2.5); // => '2.50'
formatPrice(10); // => '10.00'Hint
- At the beginning of the file, import the function:
import { round } from 'hexlet-basics/math'; - The second argument of
round()is the number of decimal places
Tips
If you've reached a deadlock it's time to ask your question in the «Discussions». How ask a question correctly:
- Be sure to attach the test output, without it it's almost impossible to figure out what went wrong, even if you show your code. It's complicated for developers to execute code in their heads, but having a mistake before their eyes most probably will be helpful.
Tests are designed so that they test the solution in different ways and against different data. Often the solution works with one kind of input data but doesn't work with others. Check the «Tests» tab to figure this out, you can find hints at the error output.
It's fine. 🙆 One task in programming can be solved in many different ways. If your code passed all tests, it complies with the task conditions.
In some rare cases, the solution may be adjusted to the tests, but this can be seen immediately.
It's hard to make educational materials that will suit everyone. We do our best but there is always something to improve. If you see a material that is not clear to you, describe the problem in “Discussions”. It will be great if you'll write unclear points in the question form. Usually, we need a few days for corrections.
By the way, you can participate in courses improvement. There is a link below to the lessons course code which you can edit right in your browser.
Создавать обучающие материалы, понятные для всех без исключения, довольно сложно. Мы очень стараемся, но всегда есть что улучшать. Если вы встретили материал, который вам непонятен, опишите проблему в обратной связи нашего сообщества
Your exercise will be checked with these tests:
// @ts-check
import { expect, test } from 'vitest';
import formatPrice from './index.js';
test('formatPrice', () => {
expect(formatPrice(12.3456)).toBe('12.35');
expect(formatPrice(2.5)).toBe('2.50');
expect(formatPrice(10)).toBe('10.00');
});Teacher's solution will be available in:
20:00
