Why is the node_modules folder not committed to Git? - angularjs

When I create an AngularJS project with
yo angular
it creates a node_modules/ directory but puts that directory in .gitignore. When I clone the project elsewhere and run
grunt server
I get
Fatal error: Unable to find local grunt.
If you're seeing this message, either a Gruntfile wasn't found or
grunt hasn't been installed locally to your project. For more
information about installing and configuring grunt, please see the
Getting Started guide:
The getting started guide doesn't say anything about how to handle the missing node_modules/ directory however.
The node_modules/ directory is missing deliberately because yeoman put it in .gitignore.
What's the right way to use Yeoman and Grunt in this case?
Is there some better documentation for Yeoman and Grunt?
Thanks.

That is the correct behaviour. The contents of node_modules is not meant to be committed to source control. The idea behind npm is that it tracks all required node dependencies in a file called package.json. This file should be committed to source control. Then on a different machine, you can check out the code and run
npm install
This looks at the package.json file and downloads all required files from the cloud and puts them all into a new node_modules directory.

If you do enough searching on the topic you'll eventually run across the following article which states that if you're building an application that you should check-in your dependencies. Reliance on package.json can cause issues as module authors publish updates, a better solution is to use
npm shrinkwrap
which creates a file locking down your dependencies and your dependencies dependencies but even this can be brittle as it is possible for a module author to re-publish the same version with different code.
Since yo angular is creating an application IMHO node_modules should not be included in .gitignore, however as I just rudely discovered if you're on Windows there's a significant chance that you can't check-in the modules in that folder due to path lengths...sigh.

Related

I'm using Yarnberry, but is the node_module file originally created when I proceed with "Yarn start"?

I'm using Yarnberry, but is the node_module file originally created when I proceed with "Yarn start"?
I would like to proceed with the package management through Yarn-berry in the react project.
However, running 'yarn start' creates a node module file.
As far as I know, the package is managed in a zip archive form without a nodemodule file.
Is this normal?
It's normal. Some packages (like Babel, which will be used to transpile newer language features to what the host runtime supports) use node_modules as a place to store temporary files they need to create. The .cache subfolder is a sort of defacto standard for library authors. Essentially, it is nothing to worry about.
It looks like the project uses pnp which is why there's nothing else in node_modules. With PNP, the third-party packages themselves are not copied into node_modules like in the classical approach to NPM package management. But caches are an exception to this rule, which is probably why it looks strange it's just that there. But it's not an issue.
You should ignore the whole node_modules folder from your version control system (e.g. GIT).

What is the purpose of each file in the React Native file architecture?

I started to use React Native recently and, following the oficial docs, I initialized a project using npx react-native init ProjectName.
I'm not sure if the tools versions matters (probably yes), but i'm using npm version 6.13.7, react-native-cli version 2.0.1 and react-native 0.62.2. With that config, the file architecture i that get is the following:
I seached about it, but i not found an answer. So, can someone please explain to me what is the purpose of each file in this file architecture and which of these files can i remove?
Thank you in advance :D
Package.json
This file holds all of the dependencies of modules that your app is using and needed to install for running your app.
yarn.lock files yarn and package-lock.json
These two files hold the version of your dependencies yarn.lock package-lock.json is automatically generated for any operations where npm or yarn modifies either the node_modules tree or package.json. It describes the exact tree that was generated, such that subsequent installs are able to generate identical trees, regardless of intermediate dependency updates.
app.json
This file holds your application name etc.
babel.config.js
This file holds configs related to babel, Babels is a transpiler that transpile ES6 to ES5.
index.js
This is the entry point of your application form where your react-native code start executing.
EsLint and Prettier
These file related to maintaining the code indentation, Unused imports, extra, spacing, these files holds configs related to these things(EsLint and prettier are used to avoid above-mentioned things).
.watchMan
watchman watches the code changes when the packager is running, so this file have configs about this.
.Flow
Flow is been used for type checking so it holds the configs related to this.
node_modules
This folder holds all of the modules that your app is been using also lited down in your package.json.
And then there is Android(which holds native android code), IOS(which holds native ios code), and other JS files which holds code react-native js code.

Taking over React site. Is there a precompiler?

I have been tasked to maintain an existing React site. It appears that all .js and .jsx files are being compiled into one react-application.js file. I have made some changes to the code and need to move the code to staging. However I am not sure how to proceed. Being fairly new to React I am sure I have missed something here. Can someone please point me in the right direction?
Image of the root folder as requested:
https://i.stack.imgur.com/dcIh2.png
Image of the resources/assets/js folder:
https://i.stack.imgur.com/IaHew.png
So, there's two important files I see in the root directory:
package.json
gulpfile.js
package.json
If you're not familiar with npm, you're going to need to be. package.json lists all the npm packages that are required to build and run this project. Once you have npm installed globally, you're going to want to run npm install on the command line from the root of your project.
gulpfile.js
gulpfile.js is a script that should be executed by the task runner gulp. You're also going to want to have gulp installed and review the documentation to familiarize yourself with it. Your gulpfile.js will define one or more tasks. One of these most will likely transpile and bundle your .jsx files into a single browser friendly script. It most likely makes use of other npm packages to do so, which you also may need to familiarize yourself with.
You can run these various tasks by calling gulp [taskname] from the command line in the root directory. Quite likely there is a master task set to default which will run by simply calling gulp.
There's a pretty good chance gulp is also set to compile Sass and handle other tasks as well.
One more thing. The root directory also contains a .bowerrc. This would have been created by Bower, a package manager that was popular before npm. However there is no bower.json file, which I would expect to find.
This is a bit of a red flag for me. Hopefully, all of your dependencies are now included via package.json and whoever removed Bower just neglected to clean up the .bowerrc. But if your build process still depends on Bower assets, not having a bower.json is going to be a problem.

.gitignore added to project and now it won't run

So I have ReactJS project that is on GitHub and I'm new to this so I didn't have a .gitignore file so someone did a pull request adding it and I accepted and merged. I pulled that back to my local version and now I can't npm start my project bc there aren't any node modules anymore. I am sure there is an easy way of handling this, but what are you supposed to do? It seems like the node modules have been deleted so how can the app run?
Run npm install. Afterwards all modules will be installed. This requires a valid package.json file. It is best practise not to push the node modules to your repository since they consume a lot of space.
Just to add on to #PurplePanda's answer. Since you didn't have a .gitignore file initially, your node_modules folder would have been committed as well. In order for the .gitignore file to now correctly ignore the node_modules, it had to first be removed. You just need to run npm install locally and you should be good to go.

Installing npm package from fork with yarn + webpack - Can't resolve './dist/

I want to contribute to an open source React Component and I'd like to use a fork of the project in my webpack bundle.
I am using yarn and I tried to install my fork using
yarn add github:Startouf/react-coverflow
However, when webpack tries to compile my bundle, it raises weird errors
ERROR in ./~/react-coverflow/main.js
Module not found: Error: Can't resolve './dist/react-coverflow' in '/Users/Cyril/dev/MyApp/client/node_modules/react-coverflow'
Did I miss something ?
EDIT : when I use the released package from npm, the node module folder contains
LICENSE README.md dist main.js package.json
When I use my fork, it seems like the project isn't compiled and contains
LICENSE README.md package.json src webpack.config.js
Makefile main.js site test
Seems like I'm missing a step... I though doing yarn add with a github fork would automatically make a release but seems like I'm wrong ?
Unfortunately, using a repository directly as source can result in execution error. This is because it's not bundled at all, while the package expects an prebuilt version existing in dist. The bundling scripts are often executed before publishing releases to npm.
Some workarounds are:
execute the prepublish step in the target directory (this depends on
what the project uses)
of course, using the published version is the best. create your own package on npm and upload it.
References: npm issue
The package should be updated to include a prepare step.
A prepare step does exactly what you want in all cases.
https://stackoverflow.com/a/57503862/4612476
You can add the prepare script in package.json#scripts yourself that runs the build. Npm and Yarn will then automatically run the prepare script on install directly from GitHub. You can then treat it like any other package and it will always just work™.
Don't forget the package.json#files section. See the linked answer for more details.

Resources