3

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/sdk@0.1.4
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 Answers1

4

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.

kvooak
  • 275
  • 1
  • 3
  • 9
  • so i just tried creating `.npmignore` in the same of my `.gitignore` directory, and both of them only contains `node_modules`. once i clicked that `npm publish`, all other files automatically get deleted. @kvooak – Edward Tanoto Mar 09 '22 at 09:48
  • This is my ["files" field declaration](https://imgur.com/a/uB4AEOd) in a `package.json` file of an npm package I published. In there instead of `dist` I put `dist/*` just to be sure, and it worked. You can try with other glob patterns like `**/*` and such to see which works for you. @EdwardTanoto – kvooak Mar 09 '22 at 09:57
  • hmm i already tried, still doesnt work though. i already deleted my package lock json and node modules as well, and changed my files into dist/*, I'll edit the details in the post @kvooak. – Edward Tanoto Mar 09 '22 at 10:12
  • @EdwardTanoto have you solved this issue? – kvooak Mar 09 '22 at 19:23
  • Hi @kvooak, may I contact you through discord or anything? There is a new development and it's almost solved. – Edward Tanoto Mar 10 '22 at 04:48
  • Hi, I am facing the same issue. Did we achieve any resolution on this? – MuKa Jun 17 '22 at 16:00
  • Hi @MuKa, unfortunately no, however I’m still interested and if you figure out something eventually please share. I’m also happy to look into this again with my own npm packages – kvooak Jun 17 '22 at 19:44
  • @kvooak, I think it started spitting out more files into the tgz when I removed the "files" attribute in packages.json. I am using a .npmignore to override the exclusions made in .gitignore. I initially had the "files" values as "dist", "src" and then "dist/*", "src" but neither combinations worked. – MuKa Jun 20 '22 at 10:51
  • This seems to be a new behavior with NPM 9. I haven't had any problem before upgrading from 8 to 9. However, changing files from "dist/*" to "dist" worked for me. – zirkelc Dec 05 '22 at 14:30
  • I was having same issue, and fixed with those patterns: "files": ["dist/**/**.js", "dist/**/**.d.ts", "!dist/tests/**" ] – Gilson Dec 13 '22 at 20:40