DEV Community

Ramesh
Ramesh

Posted on

1 1

Export vs Main in package.json

Exports vs Main in package.json

Main field: This is the older way to define the entry point of a package. When you require or import a package, Node.js looks at main to know which file to load. It’s straightforward but doesn’t support modern JavaScript module systems like ESM.

Exports field: This was added in Node.js 12+ and is much more flexible. With exports, you can control exactly which files are accessible when someone imports your package. It supports different formats like ESM and CommonJS and even allows you to expose specific files.

Differences:

Flexibility: exports is more powerful and customizable, while main is simpler but limited.

Modules: exports works with both ESM and CommonJS, but main doesn’t handle both.

Priority: If both are used, exports takes priority over main.

Best Practice:

Use exports for better control and modern compatibility. Keep main only if you need to support older system.

Image description

Top comments (0)

typescript

11 Tips That Make You a Better Typescript Programmer

1 Think in {Set}

Type is an everyday concept to programmers, but it’s surprisingly difficult to define it succinctly. I find it helpful to use Set as a conceptual model instead.

#2 Understand declared type and narrowed type

One extremely powerful typescript feature is automatic type narrowing based on control flow. This means a variable has two types associated with it at any specific point of code location: a declaration type and a narrowed type.

#3 Use discriminated union instead of optional fields

...

Read the whole post now!