Another testing npm package used to learn how to do it
Made following this tutorial
Some of the things I want to remember about its content, thigs i've learned by myself, and from other sources:
npx npm-packlistto see the contents that will be included in the published version (DIDN'T WORK) -> use
npm publish --dry-runinstead.
- When no
filesoption is included in the
package.json, it will include some unnecesary files, so be careful.
- Even if it has a
filesoption, it will still always include these files (which are very important):
- If we are using scoped names (e.g. @something/name) for the package we need to run the following command to publish our package:
npm publish --access=public. We could also define that option inside our package.json (I don't remember how XD).
- If you don't want or don't need to declare types for your package, you could create a .d.ts file with the following content:
declare module '@your/package_name'. This way those using Typescript will still be able to use your library.
- As a side note, if you want to consume libraries without the types definition, you can create a .d.ts file to manually create the declaration, as stated by this stackoverflow answer
about the usage of commonJS an ESM for our package
- By default
.jsfiles are treated as commonJS by node (and I think they are interpreted like so by bundlers).
.mjsare always treated as ESM and
.cjsas commonJS no matter what's inside the nearest
- If we define
.jsfiles will be treated as ESM. If we want to treat them as CommonJS we must change the extension to
- If we define
type="commonjs"inside the package.json file or if we totally ignore that property at all
.jsfiles will be treated as CommonJS. If we want to treat them as ESM we must change the extension to
- The above means that if we want to deliver to both ESM and CommonJS we have three options:
.cjsfor thos commonJS files.
.mjsfor those ESM files.
- Not to define type property and use
.mjsfor those ESM files
- As I prefer to use ESM I will use the former option, although the tutorial I'm following uses the third one.
- The ESM files won't be directly imported by the browser. WE'll need a bundler to do so. If we want a way to use this library directly in the browser, we need to bundle the entire package.
Even though we have various testing libraries, the one I'm using here is vitest. I didn't find good documentation about it, but was led by the hype. However, it's working fine with my minimal setup, which is being commited at the same time of these words.