2

I'm trying to build my simple npm package, and all is well. However when I'm trying to build it it goes a bit wrong.

I get the following:

 - dist
   - package.json
   - src
      - index.js
      - index.d.ts
      - ...

But this is not what I expected to get, I'm pretty sure I've done this before and gotten, the following:

 - dist
   - index.js
   - index.d.ts
   - ...

This is what I want to end up with, but so far nothing has worked.

My tsconfig.json looks like this:

{
    "compilerOptions": {
        "target": "es5",
        "module": "commonjs",
        "lib": ["es6", "dom"],
        "resolveJsonModule": true,
        "declaration": true,
        "declarationMap": true,
        "sourceMap": true,
        "outDir": "dist",
        "strict": true,
        "esModuleInterop": true
    },
    "include": ["src"],
    "exclude": ["node_modules", "build", "dist"]
}

I haven't been able to find a reason for it to include the package.json or the src path. I'm using typescript@3.6.4.

Allan Kimmer Jensen
  • 4,333
  • 2
  • 31
  • 53
  • @Bauke I did, and that seems to have been the hidden problem. Used it to include a version number. If you know of a way that I can include it but bundle it, that would be a even better solution. – Allan Kimmer Jensen Nov 05 '19 at 13:44

1 Answers1

5

Are you importing your package.json anywhere in your code? If you are, it's getting copied because of resolveJsonModule.

If you want to read JSON files without them being copied over, you can do a readFile and JSON.parse.

  • Will this work for browser packages? I haven't tried it yet, but seems to me like that would be a problem. – Allan Kimmer Jensen Nov 05 '19 at 13:47
  • 1
    Ah, no. The `readFile` approach won't work for browsers. If you want to include that there I'd suggest just sticking with either having a variable in your code for the version or [using `require` or something similar](https://stackoverflow.com/a/10855054/12251171). –  Nov 05 '19 at 13:53