Can a parent node project use packages from a nested node project? - node.js

I've got a firebase project with file structure like so:
node_modules/
package.json
functions/
node_modules/
package.json
If I npm install firebase in the parent project, everything works fine locally but the code fails on deployment because the child project is deployed without the parent project yet depends on firebase
If I install firebase in the child project, the parent app complains "Cannot find module 'firebase/storage'"
If I install firebase in both projects, the parent app complains "Cannot read 'path' of undefined" - debugging shows this is a firebase internal error
My ideal solution is to npm install firebase in the child project only, but somehow allow the parent project to access it? Is this possible? TIA
I've read about npm-link but not sure if this can help me or whether there is some other more suitable solution.
OPTIONAL READING: Why do I have a structure like this?
I ended up with this structure because the child project is a firebase project and firebase init created the functions folder and its contents. The child project is deployed to google cloud.
The parent project is a simple test bed app that I created and uses much of the code in the child project for testing purposes only, this code is not deployed.

Got the answer from here:
how to specify local modules as npm package dependencies
I ran this command in the parent project:
npm i --save ./functions/node_modules/firebase
Which adds an entry to the parent package.json as follows:
"firebase": "file:functions/node_modules/firebase"
Now everything works!
Obviously, this assumes that the child package is actually installed.
Using a preinstall script in the parent package.json to install the child packages first ensures this works smoothly.
My preinstall script entry looks like this:
"preinstall": "sh preinstall.sh"
And the script itself has the following contents:
#!/usr/bin/env bash
rm -rf node_modules
cd functions || exit
rm -rf node_modules
npm install --also=dev
When you run npm install the preinstall script is automatically executed before the actual install takes place.

Related

npm ERR! Could not install from as it does not contain a package.json file angular firebase functions

I am using "firebase-functions": "^3.6.0", "firebase-tools": "^8.0.0" to deploy my Angular: 10.0.14 app. All was working fine until I decided to move some components into a library created using the command ng generate library and made the package a submodule in my project under ./projects/my-lib
I build the lib using ng build my-lib
Then in my package.json installed the lib from file
"my-lib": "file:dist/my-lib"
I installed the package locally and it works fine when I test it locally but deploying using ng deploy raised the error
npm ERR! Could not install from "dist/mce-lib" as it does not contain a package.json file.
I have tried a couple of solutions I found online, but no luck yet
Try this
First, remove node_modules and package-lock.json file
rm -rf node_modules package-lock.json
Then, clean cache
npm cache clean --force
And last re-install modules
npm i
You need to create the library by using
ng generate library <your lib name>
This will setup your lib with the necessary files so that you can add it as a dependency.
Otherwise, if you want to do this manually it'll require more work.
UPDATE:
To be able to use the lib above locally without publishing you will need to do the following
From withing your app directory for which you generated the lib, go into ./projects/ as it was generated by the lib command above. Then run the following build command
ng build <your lib name> --prod
The prod tag is optional but it helps to run your code in that flag so that it is the closest to what it will look like once you publish
Then go back to your app root folder and install the application by referencing the dist folder. The build command above should have added your lib as its own folder withing dist
npm install ./dist/<your lib name>
NOTE: All of this is for dev testing purposes. Ideally, you will want to publish your lib to npmjs or github packages first and then install it properly in your app before you deploy your app to production
Another way of locally: though the use of npm link
cd ./dist/<your lib name>
npm link
Then going back to root folder of app, you should be able to run the installation as such
npm link <your lib name>
This creates a local link to your library in your app for testing purposes that doesn't modify your package.json file as installing the directory directly does. Once you are done, always make sure to run unlink
npm unlink <your lib name>
And withing your ./dist/
npm unlink
So after hours of trying to figure out how to solve this, I came across these resources that helped me to resolve the issue:
https://firebase.google.com/docs/functions/handle-dependencies#including_local_nodejs_modules_as_part_of_your_deployment_package
Note: The Firebase CLI ignores the local node_modules folder when deploying your function.
Firebase Functions local "file:" dependencies
So even if the app was working locally, the node_modules isn't deployed
Since I had installed python packages from git before, I thought why not make use of this? I am using Gitlab and this answer really helped me https://stackoverflow.com/a/50314604/3247914
I got it working by installing my package from git

npm link only dist folder

Is there a way to restrict npm link to create the symlink only to point at ./dist?
I am struggling so far as I want to test my library (react component) locally before publishing.
In my package.json I have included the main (entry point):
"main": "./dist/index.js"
But still when I run npm link it links to the parent folder which has node_modules and so on.
This is causing problems cause now I have 2 react apps installed.
I thought of a way to exclude node_modules but I couldn't even find a way to do that.
This is actually simpler than expected. First run ng build to build your library project, then go under the dist folder and run npm link from there instead of the library root project. The symlink will be created pointing to the dist folder instead.
Using "main" parameter doesn't make sense because npm link use 'npm prefix' for create symlink
see here
You can use next hack:
Cd to project directory
Copy package.json from package to ./dist/package.json directory
Cd to dist directory
Run 'npm link'
It looks dirty but seem to only way to do it with npm link

Why is Heroku telling me it cannot find a package.json in my module when I do a heroku push

I created my own npm package from a fork of react-coverflow. It appears to be working fine in my app locally using it this way: "npm install react-coverflow-mod" --save.
I can run my app using "run with debug (F5)" in VsCode and npm start on the client folder to start the React front end.
Then I do an npm run build on the client folder, and it works just fine.
When I do a heroku push it fails everytime with this error:
npm ERR! code ENOLOCAL
npm ERR! Could not install from "../../react-coverflow-mod"
as it does not contain a package.json file.
1. I know there is a package.json in the module because I can install it via "npm install react-coverflow-mod": https://www.npmjs.com/package/react-coverflow-mod
2. The installed module has a package.json file in it
3. My github repo has a package.json in it: https://github.com/leroyvaughan/react-coverflow
I'm not sure how I can fix this. Do I need a package.json to go into the /Dist folder? What is wrong here with Heroku.
It seams like heroku try to install a package from a relative path instead of the published name. That would perfectly explain why you can run locally but not on a production environment.
Open your project and search for the exact string displayed in your log: "../../react-coverflow-mod" and you should be able to find quickly where it is.
If you run on a unix system (don't know about windows) you can do a search using grep:
grep -rnw '/path/to/somewhere/' -e '../../react-coverflow-mod'
Make sure it includes your root folder which contain package.json, and might we wise to ignore node_modules which is always massive.

Different behaviour between 'npm run' command and windows console

Here's a weird problem I'm suffering for days.
I need to create an asar packed file, done with electron "asar" command, like this:
c:/asar pack app app.asar
That packs my "app" folder into a packed file "app.asar". Thats running OK.
The goal
I need to include this instruction inside my package.json script file in order to generate a build process, chainning other actions.
The problem:
Well, when I run this command, by package.json script like this c:/npm run create-asar or either with a gulp-asar process, it creates the app.asar file, but seems to be corrupted.
It can't be unpacked, process throws an error and can't be accessed by the electron app
I can't figure out why.
I've tried to run the exact same command from console, that in package.json, exactly the same, and both with the results above.
what's the difference?
versions info
npm: v3.10.6
node: v4.5.0
asar: v0.13.0
electron: v1.4.3
Install asar locally as a project dependency, cd to your project directory and run:
$ npm install asar --save-dev
Change your npm-script to the following:
"scripts": {
"create-asar": "node_modules/.bin/asar pack app app.asar"
},
Note: asar is being executed via the local node_modules/.bin folder.
Then run $ npm run create-asar and subsequently check whether it unpacks successfully.

Managing node packages in a optimized way

I am newbie in Node.js.
Whenever I want to install a package, I do it like
"npm install -g package-name"
What I have seen inside my node applications there is a dir "node_modules" been created and all the installed modules are there.
Then I want to use "grunt" for automating my tests for frontend javascirpt unit tests.
And I ran the command for installing "npm install -g grunt" but when I go inside my test directory and run grunt I get "Fatal error: Unable to find local grunt." But if I install it inside the "test" directory it works fine.
My project structure like below:
-backend
-tests
-model
-node_modules
-package.json
-others
-frontend
-tests
-js
-package.json
-node_modules
-others
How I can manage node packages from a single pacakge.json and run the tests separately in frontend and backend? What are the optimized way of doing this stuff?
Thanks in advance.
The -g flag in npm will install the module globally -- this is how things like grunt work as a CLI tool, but for your dependencies, you probably just want them to be installed in your node_modules folder, defined in your package.json.
Are you browserifying your front-end modules? You most likely do not want both your front and back end to have the same set of dependencies, best to keep them separate package.json manifests. Are your frontend modules just grunt tasks? Running grunt in either frontend or backend directories will call the gruntfile only there.
Keep these two directories separate -- will save a lot of headache in the future.

Resources