dynamically add folder to ignore when using npm publish - node.js

I have a static .npmignore file with
foo
bar
baz
in it. When I publish to NPM, the contents in those 3 dirs will be ignored.
My question is, is there a way to dynamically add a folder to ignore when using npm publish at the command line?
Something like:
npm publish --ignore=.r2g
here we can ignore a folder called .r2g
Here are the npm-publish docs

First, a static solution:
You can include a .npmignore file per directory that you want to exclude.
As the doc states:
Like git, npm looks for .npmignore and .gitignore files in all subdirectories of your package, not only the root directory.
I created an example of this in https://github.com/dral/npmignore-example (and the corresponding package npmignore-example).
file structure
index.js
.npmignore
bar/index.js
baz/index.js
foo/index.js
/.npmignore
the root .npmignore removes only the baz directory. the nested foo/.npmignore removes all content (from this point on).
The installed package includes only
index.js
bar/index.js
In order to do so dynamically you can use a script that adds and then removes a simple .npmignore file in the selected folder(s).
echo '*' > .r2g/.npmignore && npm publish && rm .r2g/.npmignore
Then if you need to automate this, consider using prepublish and postpublish scripts that take into account an env variable so you can use something like NPM_IGNORE=.r2g npm publish.
Hope this helps.

Related

How to ignore a sub package.json file when publishing to npm

So I want to publish my package to npm. It is a cli that can create custom project for my company.
I have a subfolder containing the template of that project, and this includes a package.json file (which btw has a files property).
Now the problem is when I publish it on npm, when going threw the files in the project template, it doesn’t add the files because it follows the specific package.json instead of the root one.
So I would like npm publish to consider all files in my template, ignoring the files property inside that folder’s package.json
Thank you all :)

git status is not showing my changed files

I have a Node.js project and have imported a few Node modules. The git_status command shows the files that I changed in the project. I have also changed some files under the node_modules directory but those are not shown. The node_modules directory is shown as untracked.
Untracked files:
(use "git add <file>..." to include in what will be committed)
node_modules/
src/js/main-release-paths.json
I have changed only one file in node_modules:
node_modules/#oracle/grunt-oraclejet/node_modules/#oracle/oraclejet-tooling/lib/serve/connect.js
How can I track this file?
You shouldn't add the node_modules folder to git then it's managed bei the npm command. So remove that folder and use the npm install command.
When you have added something in that folder it's shown as changed, if you had first added some files. So i hop that folder is in your .gitignore file and ignored. You should check that.
Assuming you have git repo, Please check the .gitignore files inside your project. Entries in .gitignore will be ignored by Git and will not show up when you do git add.
Adding the file to track
This is most likely not a suitable approach (read below). However , if you really want to do it:
In order to track a file nested under node_modules, you should add the directory. All of its content will be staged in git
git add node_modules
Recommended approach
You probably don't want to track the contents of node_modules directory, because:
running npm install will wipe your changes
hundreds or thousand files are likely to end up present in that directory
tracking package.json and package-lock.json in git is sufficient, then populate node_modules using npm install command.
You are not supposed to modify files located in node_modules directory directly. Instead, fork the module in question, modify it, and:
either publish your own version in npm
or reference it as a git repository inside package.json (how-to)
Then, also add node_modules directory to the .gitignore file.
If the whole directory is untracked, git status will show only the directory. Because it's untracked, there is no meaningful difference to git between changed and unchanged files in there.
Other answers have already addressed that tracking node_modules is nota good idea. Of you want to track your changes to modules, consider cloning the module repo and including it as a git submodule.
It should be inside .gitignore file. A .gitignore file should be committed into your repository, in order to share the ignore rules with any other users that clone the repository.

When using a private git url for an npm module, how can I configure the consuming application to only use files from the module's dist folder?

I am using angular-cli for my angular application, but because angular-cli currently does not support use for creating a library, I used the seemingly most widely used git project to create my library: https://github.com/jvandemo/generator-angular2-library
My issue is that I don't want to publish my npm module library to the public directory. Instead I want to use the git url directly in my dependencies. During development, this works fine. I can run the build locally and run an npm link inside the "dist" folder and everything is dandy. However when I push my npm module code to git, and then run an npm install in the consuming project, I'm not sure how to set it so that my consuming project just looks inside the dist folder of the module and treats it as if it was the root of the module.
For example, in node_modules/my_private_module, my file structure looks like:
dist/
-- component1
-- compoennt2
-- my_module.metadata.json
-- my_module.d.ts
-- my_module.umd.js
-- index.d.ts
-- index.js
-- package.json
-- README.MD
package.json
README.md
All the files that my application is using are in the /dist folder, but I DO NOT want to specify "dist" in all my imports like
import { myComponent1 } from 'my_private_module/dist';
I want to be able to just specify
import { myComponent } from 'my_private_module";
As I do in development when I run an npm link in the dist folder.
Is there a way I can achieve this?
Thanks
In package.json for your module, in the root folder:
typings: 'dist/index.d.ts',
main: 'dist/index.js'
Remove the package.json in your dist folder.
When the package is resolved from import {...} from 'my_private_module', the module loader will look for a folder called my_private_module under node_modules, and look either for index.js which defines the exports, or within package.json for the main property - which in your case also points to index.js from the dist folder.
It is good practice to put package.json where you want your module to be found, and have main and typings point to index.js and index.d.ts.
I answered a similar question here and it seems relevant.
Basically, treat the generated library in the dist folder as it's own repo. In order to keep the git init files and folders, you tell ng-packagr to not destroy the destination when building. Then you push the changes to the library specific repo and use that as your package url in other projects.

npm install is partially cloning the source from git repo

When I run npm i command it partially clones the code from bitbucket repo. Here are some dependencies in package.json
"dependencies": {
"bluerain-app-hello-world": "git+ssh://git#bitbucket.org/projects/bluerain-app-hello-world.git#build_issue",
"graphql-tag": "0.1.16" }
And this is the source of my module
But when i list down cloned rope from node_module by
ls node_modules/bluerain-app-hello-world/
It gives this list
index.js node_modules package.json README.md src
There is a dist folder in my source but it didnt clone properly. I tried to remove my node modules and reinstall it but it didnt work. Any suggestions please?
I started working on a boilerplate who had added the files array field in package.json and specified only the src folder. This is why when my node module installed, it didn't contain the dist folder.
According to official documentation
The "files" field is an array of files to include in your project. If you name a folder in the array, then it will also include the files inside that folder. (Unless they would be ignored by another rule.)
You can also provide a ".npmignore" file in the root of your package or in subdirectories, which will keep files from being included, even if they would be picked up by the files array. The .npmignore file works just like a .gitignore.

Does NPM ignore files listed in .gitignore?

I have a file that is generated by npm install command (using preinstall task). I don't want to add it in the git repository, nor in the NPM project.
Supposing the file name is foo.json, I added it in .gitignore file as foo.json.
Is this enough to avoid uploading it on NPM registry?
I know I can add .npmignore file that will surely ignore the file, but I won't add it if .gitignore already does this.
If a project has both an .npmignore and .gitignore file, npm will only use the .npmignore file.
From the documentation:
Use a .npmignore file to keep stuff out of your package. If there's no .npmignore file, but there is a .gitignore file, then npm will ignore the stuff matched by the .gitignore file. If you want to include something that is excluded by your .gitignore file, you can create an empty .npmignore file to override it.
In simpler terms, npm prefers the .npmignore file if it is there, but will fall back to the .gitignore file.
In many cases, both Git and npm can ignore the same files, so it makes sense to just use a .gitignore file on its own. If there's ever a discrepancy (i.e. npm and Git need to ignore different files), then you need to maintain separate .gitignore and .npmignore files.
More information on what to put in .npmignore files: Should I .npmignore my tests?
For anyone reading this trying to ignore a file/dir from git but wish to include it in npm publish and have tried using an empty .npmignore file with no luck. This works.
In your .gitignore file, add the file/dir you wish to exclude **/build for example and in your .npmignore file make sure you specify the same file/dir but with the ! prefix so for the build example you would include !**/build

Resources