"npm run build" Command in an Express App Colliding on Imports with Typescript and React Native - node.js

I have an Express app that I build and run using npm run serve which I define in my package.json scripts serve section as: 'npm run build && node lib/index.js'. Recently, I installed React Native for another project, however, when I return to my express project and attempt to build it with npm run build, I recieve multiple duplicate declaration errors in the terminal:
React Native is not defined anywhere in my Express project, when I examine my node modules, there is no react-native package even installed. Before installing React Native in another project this command worked fine, the Express project has not been changed since it last compiled succefully.

Thanks #TheGreatZab for testing it out.
Post the answer here to help anyone who are suffering from this problem.
The reason is an extra node_modules/package.json exist in grand parent directories which trigger the butterfly effects.
Solution:
Remove the node_modules, package.json from parent/grand parent directories

Related

Vue3 app breaks when you install anything from npm

I'm starting a new Vue3 project using vue create delete-me. This seems to work fine, and I can run npm run serve and the app starts up as intended.
As soon as I install any library with NPM, it seems to delete all node_modules and the project no longer works.
Now install anything and this is the result:
I've tried running npm install afterwords but the project does not seem to have all the dependencies. The project is just useless at this point.
It seems like vue create does not create a valid package.json but not sure. I'm new to Node and Vue.

NPM not installing node module

I have an angular project that comes with a package.json file and all the dependencies listed within, but each time I run "npm install", in the terminal, the packages appear to be downloading properly and a node module folder is created in my root folder, but at a certain stage the installation stops without an error message and the generated node module folder disappear from my project. please what do I do?
I can't reproduce your situation, somehow this may not fitable for you.
But I suggest re-install node.js to make sure npm install work correctly.
This link will help to find what version of node.js for you, based on you angular version.
Compatibility list for Angular/Angular-CLI and Node.js
Also, It would be good to manage node.js via NVM(Node Version Manager).

How does scaffolding functionality works in some of the Node Packages?

For some Node packages, (Eg - create-react-app) when we run npx create-react-app app-name or npm create-react-app app-name (if we have npm installed globally) in cli, then the command line automatically creates a physical folder with basic files to run a React app. What technology, or modules does Node packages as such use to allow this kind of scaffolding of project?
I saw that there is something called Yeoman which helps users to do such thing, or a module called fs which I am assuming could be used to help to generate the scaffolding. Does create-react-app or any other Node Packages that does scaffolding uses similar technology?

React Native: Unable to resolve a local module. Exists in Node Modules

I'm working on an app that both has a website (React) and mobile app (React Native). In one repository I have my web, mobile and redux files which is my local module/package (so that they can be shared). I have not had any problems with installing my local package when working on web. However with React Native I am running into issues with the package.
I ran
npm install --save '../store/sagas
Which gave me
Unable to resolve "redux-saga-store/sagas/index.js" from "App.js"
Fair. App.js is where I am calling my rootSaga via
import rootSaga from 'redux-saga-store/sagas/index.js';
Which is identical to how web is calling the same file.
I checked my node_modules and the package and all the expected files are there. I tried
npm list
Which returned (as expected)
redux-saga-store#1.0.0 -> /Users/{myname}/Repos/{repositoryname}/shared/store
I have triple-checked the paths and they are correct.
I tried npm install with the global flag. I went on to nuking my node_modules and package-lock.json so I could try re-installing. I created a brand new react-native app using create-react-native-app which also did not fix the issue. I would really appreciate any suggestions or help

cannot find module express, how to install it to make it globally available?

I want to experiment with some node.js stuff and I installed it yesterday following someone's instructions on the web which got it up and running, and I got the standard Hello World web page up on the screen.
I now went to move onto another example, but in order to not clutter my home directory, I created a directory off of it (~/node) and created the files I needed in there. Low and behold, when it came time to run the service, I got no joy stating the express module couldn't be found.
The instructions told me to install express using the -g flag, but that didn't help. I even ran it again without any luck.
Now I've found this:
Cannot find module `express` | socket.io [node.js]
and it appears I have to install it again under the current directory. I have done that and it works. Is it the case it has to be installed under each directory that I want services running from? It seems an unnecessary duplication.
edit:
Not knowing much about js I thought I would go digging and found
app.use(express['static'](__dirname ));
and have realised this is probably the cause of my problem. Further research has found this: http://nodejs.org/api/modules.html#modules_loading_from_node_modules_folders and if I install it once in a higher level directory, that should 'solve' my problem. I'm not going to bother about uninstalling the module, but for my next project I will try it and see how it goes.
I don't know why the original author suggested the -g flag when installing express, since it hasn't seemed to work for me.
NPM is a really nice tool, allowing you to install node.js modules locally and globally.
Local module installation
If you want to use a module for your project, you have to install it locally.
That's why npm creates a subdirectory called node_modules inside your project directory.
If you use the same module for two different projects, npm will download the module and install it twice. That's perfectly normal, it helps you manage different versions of the same dependency.
The best way to manage dependencies and install modules for a specific project is to fill the package.json with your dependencies and install them using
npm install
inside your project directory.
To access your modules in your code, use the require() function.
For example, with expressjs :
var express = require('express');
var app = express();
...
Global module installation
npm allows you to install modules globally as well. But remember that installing a module globally only provides more commands in your terminal, as with expressjs and express(1).
In order to install expressjs globally, run this in your terminal
npm install -g express
If you want to use a globally installed module in a specific project, you also have to install it locally (in your project directory, without -g).
I hope this answers clearly your question.
Express is capable of generating a simple app structure when installed globally. See this link and scroll to Using express(1) to generate an app section. It's a good way to get you started easily.
Take a look into package.json, package.json in nodejitsu
All npm packages contain a file, usually in the project root, called package.json - this file holds various metadata relevant to the project. This file is used to give information to npm that allows it to identify the project as well as handle the project's dependencies
a package.json example:
{
"name": "CRUD",
"description": "A simple CRUD",
"author": "Only for learn",
"dependencies": {
"express": "*",
},
}
so for install the dependencies go to level that package.json is, and run npm install this one will install all the dependencies you need for the project.
EDIT
a package.json interactive guide
I have found that when setting up node.js projects and dependencies, using Grunt [http://gruntjs.com/] has a lot of advantages. Although there are lots of different ways to setup a node and express project there is a lot to be said for using the Douglas Crockford approach and 'going with the grain'. In this case Grunt is the grain as it is becoming the de-facto standard for setting up a node project and there are existing templates for the most common types of node.js projects. You can find Grunt Express here [https://github.com/blai/grunt-express].
In this case Grunt would provide you with a project structure consistent with others, setup dependencies file for the node package manager and auto generate the express project for you. Packages are kept in a node_modules directory. If you are familiar with maven you might recognize the 'convention over configuration approach'.

Resources