Never ever forget to install a dependency
How many times have you published version 1.0.0
of your new fancy package and it didn't work when you installed it as a dependency?
It happened to me a lot! One of the frequent issues is that I started to use a dev dependency but didn’t change it to a prod dependency. This is even a bigger issue when you use npm or Yarn because they create flat node_module
. In a flat node_modules
, your code has access even to sub-dependencies of your dev dependencies! The later cannot happen when you use pnpm because pnpm creates a strict, non-flat node_modules folder. However, even when you use pnpm, your code will have access to dev dependencies during development.
Workaround
This issue bothered me a lot and my first solution was to create a separate package for my tests. So there was a package.json
file in the test/
folder that contained all the dependencies needed by tests. Test dependencies were installed to test/node_modules
and they were not accessible by prod code.
However, there are also other issues that may be detected only after a package has been published:
- a file needed by the package is not added to the files field of
package.json
. - installation lifecycle scripts fail
- bins are incorrectly declared
- the main file is not specified correctly
package-preview
So I created a package that helps to detect these issues during running the tests of your project: package-preview 🎉✨
package-preview packs your project and installs it the way it’s going to be installed as a dependency, so you can test the exact same package content that is going to be installed by Node.js package managers.
All you have to do is to install package-preview
as a dev dependency:
pnpm i -D package-preview
…and run preview
before running your tests
{
"name": "awesome",
"version": "1.0.0",
"scripts": {
"test": "preview && tape test.js"
}
}
package-preview
is going to create the preview version of your package and link it to your project's node_modules
. So in your tests, you can require awesome
and test the production version of your package:
// Instead of require('.')
const awesome = require('awesome')
assert(awesome() === 'Awesome stuff!')
P.S.
Under the hood, package-preview
uses pnpm, so as a bonus, you will also catch errors that you would miss with npm/Yarn’s flat node_modules
. If you did not yet try pnpm, check it out, you won’t be disappointed. It is fast, reliable and saves you lots of disk space!