Any project has at least two ways it can be used. They are commonly referred to as environments. For example, when a developer works on a project on their computer, they run it in a development environment. When the project gets to the place where it is used, then the environment is called production.
On the one hand, developers are constantly trying to make these environments similar to avoid mistakes associated with the peculiarities of a particular environment, on the other hand, they still have different requirements. For example, development often needs additional packages that help the development process itself, such as packages for automated code testing. These packages are useless in a production environment, but they take up time and disk space. And this is a hindrance because it's important for the production environment not to pull unnecessary things. This greatly affects the speed of the delivery of code to production.
To solve this problem, projects in JavaScript have added the ability to explicitly specify dependencies that are only needed during development. This is done by using a special flag during installation:
# jest is a test framework
# which is used to write automated code tests
npm install --save-dev jest
The installation procedure here is almost the same as for normal packages. The only difference is that the description of the dependency is not in dependencies, but rather in devDependencies.
"devDependencies": {
"jest": "^26.4.1"
}
From a usage point of view, these packages are no different from packages installed in dependencies. The only difference is where they are imported. As a rule, a separate directory is created for code testing, inside which is the test code, which is run only in the development environment. Therefore, imports of this code do not overlap with imports of the code of the project itself.
You can see all this with your own eyes in a special package created by Hexlet as an example of a standard design.
Development dependencies are always installed when npm install
is started. Here, the developers assumed that this command is most often called during development, so the shortest option was left for the development environment. You need to add a special flag for production:
# Now the dependencies from devDependencies will not be installed
npm install --production
# Production mode can also be set using an environment variable
NODE_ENV=production npm install
When the project is built to be deployed to a server (e.g., through GitHub Actions), the flag must be applied with npm ci
:
npm ci --production
Although you'll get acquainted with production a little later, you should know about devDependencies right away because they're found in the source files of the vast majority of projects and it should not be a surprise.
bin/nodejs-package.js 10
. You should see a number as a resultnpm test
. Tests should work correctly--production
flag, and check the new size and contentsThe Hexlet support team or other students will answer you.
A professional subscription will give you full access to all Hexlet courses, projects and lifetime access to the theory of lessons learned. You can cancel your subscription at any time.
Programming courses for beginners and experienced developers. Start training for free
Our graduates work in companies:
From zero to a developer. Refunds in case you won't get a job
Sign up or sign in
Ask questions if you want to discuss a theory or an exercise. Hexlet Support Team and experienced community members can help find answers and solve a problem.