Alexolo
Alexolo

Reputation: 318

When building a package, must i have a package.json file in my /src folder?

I have several packages where there is a package.json file in root, as well as a package.json file in the /src folder. When I build, it is the /src file that is copied to the /dist folder. (and later published to Nexus).

What I am wondering is if this is the correct way to go, as when developing, I only change the outermost /package.json file. This means the /src file is "deprecated". And updating two files is a hassle.

Am I doing it the "correct" way, must i keep two files up to date? Or can I just use one file, the "root" one.

package.json
src/
    index.ts
    package.json
dist/
    index.d.ts
    index.js
    package.json //from src

Upvotes: 2

Views: 2129

Answers (1)

ecraig12345
ecraig12345

Reputation: 2437

The way you have your project set up is a bit unusual. Typically, the project would look like this, with a single package.json at the root:

.npmignore
package.json
src/
    index.ts
dist/
    index.d.ts
    index.js

Instead of only publishing the contents of the dist folder, you'd run npm publish from the root of the project. The new file .npmignore tells npm what not to publish. If the only thing you want to exclude is src, your .npmignore would look like this:

src

Then in package.json, set the fields main and typings to tell Node and TS where to find your code:

{
  "main": "dist/index.js",
  "typings": "dist/index.d.ts"
  ...
}

(Alternatively, if you really just want to publish the contents of dist, you could set up another build step to copy your root package.json into dist. But the publishing strategy I described above is much more typical.)

Upvotes: 3

Related Questions