npm publish only publish index.js and excluding everything else

2.7k views Asked by At

Problems

I'm trying to publish my own SDK in npm. When I'm trying to install my packages, my dist folder only shows index.js

Code:

Here is my package.json file. It already includes main and files, https://docs.npmjs.com/cli/v8/using-npm/scripts.

{
  "version": "0.1.4",
  "license": "MIT",
  "main": "dist/index.js",
  "typings": "dist/index.d.ts",
  "files": [
    "dist"
  ],
  "engines": {
    "node": ">=10"
  },
  "scripts": {
    "start": "tsdx watch",
    "build": "tsdx build",
    "test": "tsdx test --passWithNoTests",
    "lint": "tsdx lint",
    "prepare": "tsdx build",
    "prepublish": "tsdx build",
    "size": "size-limit",
    "analyze": "size-limit --why"
  },
  "peerDependencies": {
    "react": ">=16"
  },
  "husky": {
    "hooks": {
      "pre-commit": "tsdx lint"
    }
  },
  "name": "@grammable/sdk",
  "author": "peanut butter jelly",
  "homepage": "https://github.com/grammable/grammable-sdk",
  "repository": "https://github.com/grammable/grammable-sdk",
  "publishConfig": {
    "access": "public"
  },
  "module": "dist/grammable.esm.js",
  "size-limit": [
    {
      "path": "dist/grammable.cjs.production.min.js",
      "limit": "10 KB"
    },
    {
      "path": "dist/grammable.esm.js",
      "limit": "10 KB"
    }
  ],
}

I removed my devDependencies and dependencies to remove unnecessary lines. Can someone help me?

This is my folder structure. folder

Update: I have tried using npx npm-packlist. It has everything I need, but when I npm publish it, it only builds index.js

Edit 2:

npm notice   @grammable/[email protected]
npm notice === Tarball Contents === 
npm notice 35.1kB LICENSE      
npm notice 237B   README.md    
npm notice 184B   dist/index.js
npm notice 1.6kB  package.json 
npm notice === Tarball Details === 
1

There are 1 answers

10
kvooak On

tl;dr: When a .npmignore file is not specified, npm will use your .gitignore file setting, where dist and the files in it are likely to get ignored.

Your dist/index.js isn't ignored by this rule because you specified it in the main field of your package.json.

And there is this particular part in npm doc about the files field in package.json also explains more clearly why this behavior happens.

Some special files and directories are also included or excluded regardless of whether they exist in the files array.

According to this answer the issue can be solved when you specify your own .npmignore file.