47

The devDependencies section of npm's package.json documentation says to list your test dependencies there so that users of your package don't have to pull down extra dependencies. Would it make sense to also add my test directory to .npmignore in that case?

Eric Bock
  • 1,732
  • 1
  • 18
  • 22

3 Answers3

49

Yes that's what most people do, here are some npmignore files for popular Node.js modules:

https://github.com/socketio/socket.io/blob/ab46351a8446516fb4eea3b8333f7c0f18afaac5/.npmignore

Other people allowlist what they want published in their package.json files setting:

https://github.com/senchalabs/connect/blob/master/package.json
https://github.com/strongloop/express/blob/master/package.json

Chris Woolum
  • 2,854
  • 20
  • 20
alessioalex
  • 62,577
  • 16
  • 155
  • 122
  • 3
    Some might find it useful to keep tests there so people can do `npm test module` if for some reason something isn't working and they want to test the modules they're using. – fent Jan 04 '12 at 17:01
  • 31
    I would suggest that if people want to run tests for my module/package, they would actually take the time to clone my repo and run `npm install` so they get all the actual development dependencies. It sucks big time when you start depending on some module that happens to have about 20 megabytes of autogenerated test code published to npm (e.g. moment-timezone <= 0.0.3), along with a bunch of test frameworks that I don't need in order to use the module. – Frost Jul 28 '14 at 14:04
  • 3
    first link is broken – realtebo Jul 02 '18 at 19:56
11

One thing that I haven't been able to find explicitly mentioned anywhere is the fact that the "files" entry in package.json supports using a ! prefix on an entry. So, for example, I have a "files" entry that looks like this:

{
  "files": [
    "lib/**/*",
    "!lib/**/*.map"
  ],
}

I do that because my lib directory includes .map files that I don't want to include in the package, and this includes everything but the *.map files.

tcobbs
  • 309
  • 2
  • 5
  • Is this documented somewhere? I can't get this to work (npm v8) and neither can I find any documentation about it. – fgblomqvist Jan 18 '23 at 12:01
  • The files entry is documented to to follow a similar syntax to .gitignore, but reversed. See: https://docs.npmjs.com/cli/v9/configuring-npm/package-json#files – tcobbs Jan 19 '23 at 18:30
  • It definitely worked for me in npm 6, but I'm not sure I've tested it in npm 8. However, even though the npm documentation link above is for v9, the v8 documentation says the same thing (replace v9 with v8 in the link). – tcobbs Jan 19 '23 at 18:34
  • Doesn't say anything about excluding files. Just says that you can include files by using patterns similar to the gitignore :/ – fgblomqvist Jan 20 '23 at 18:41
  • 1
    Yes, it says, "File patterns follow a similar syntax to .gitignore, but reversed..." Since .gitignore allows bang characters, I tried it out, and it worked for me. I don't know why it's not working for you. – tcobbs Jan 21 '23 at 22:35
  • `!/src/**/__tests__/*.test.ts` worked for me. – Philihp Busby Mar 22 '23 at 16:35
4

Another approach is to use a lib folder and store everything in there. Then you can configure your package.json to consider only that folder.

In order to work you need also to move your main file inside lib and specify it in the package.json. See example below:

{
  "name": "your-package",
  "main": "./lib/index.js",
  "files": [
    "/lib"
  ]
}

More info are available on this nice article

a.barbieri
  • 2,398
  • 3
  • 30
  • 58