In project electrum, we need
to ship not only the ES2015 source code, but also its transpiled version.
The way to go is to add a compile and a prebuild step for npm:
1"scripts": {
2 "compile": "babel -d lib/ src/",
3 "prepublish": "npm run compile"
4},
- The
compilestep invokes Babel and outputs the transpiled files found in foldersrcto folderlib. Invoking thecompilestep is done withnpm run compile. - The
prepublishstep launchescompile. It will be automatically started as a result of doing annpm publish.
In order for the consumers of the electrum package to get the transpiled
version of the code, the main property must be updated to point to lib:
1"main": "lib/index.js"
I also modified my .gitignore file to exclude lib from being part of the
git repository.
1lib/
Having done that, I published an updated version of electrum using:
1npm publish
…and voilà, here it is: https://www.npmjs.com/package/electrum
So npm install - where’s my lib?
Trying to consume the new electrum package did not succeed. I kept
getting error messages from WebPack:
Module not found: Error: Cannot resolve module ’electrum’ in S:\git\foo\bar
Why? Because node_modules/electrum does not contain a lib
folder and lib/index.js cannot be found.
Here is the folder structure (use npm install electrum@1.1.2
if you want to have a look at the misbehaving package):
1src
2test
3.babelrc
4.npmignore
5...
Do you see that .npmignore file? I did not define any in my package
source, but there it is. And it contains:
1lib/
2node_modules/
This .npmignore is being synthesized by npm when running npm publish.
Why does lib/ get ignored?
Here is what the documentation
says about .npmignore:
Use a
.npmignorefile to keep stuff out of your package. If there’s no.npmignorefile, but there is a.gitignorefile, then npm will ignore the stuff matched by the.gitignorefile.
So npm is trying to be smart here: it sees that I don’t want lib/ to get
included in my git repository, and it assumes that I don’t want it to be
included it in the resulting package either.
The solution is simple:
If you want to include something that is excluded by your
.gitignorefile, you can create an empty.npmignorefile to override it.
Version 1.1.4 of the Electrum package finally got it right…