4
votes

I've created npm package. In this package I'm using some modules which I put to node_modules to be able to require them as "modules", for example I have modules node_modules/my-module.js which I require in my code as require('my-module'). Now I do "npm publish" and then in another project I do "npm i" to install my module. It is installed but there are no my modules which I put to node_modules. I tried to add the next lines to .gitignore and to .npmignore, but it did not help:

node_modules/*

!node_modules/my-module.js

what do I do wrong?

3
Are these additional node_modules in your package.json file? - r0-
no, it is not modules from npm repository, it is my modules which I put to node_modules to be able to require them in modules style, not using relative or absolute paths - pavel06081991

3 Answers

4
votes

I believe that all files in node_modules/ are ignored by NPM, so setting rules for node_modules in .gitignore and .npmignore will have no effect.

Additionally, everything in node_modules is ignored, except for bundled dependencies. npm automatically handles this for you, so don't bother adding node_modules to .npmignore.

Source

3
votes

It's not clear to me why you want to have a package in the node_modules directory and not in the package.json file, but the short answer is that it's not possible, because that directory will be always ignored, as gauge said.

So there's two ways to go, you can put those modules in another directory and require it from his relative path or you publish those in NPM and then you require them from the package.json file.

2
votes

Have you tried using something like:

!node_modules/
node_modules/*
!node_modules/my-module.js

I have that in a gitignore of mine and it has allowed publishing.