Including #firebase/app in firebase functions package.json - node.js

After updating to the newest firebase SDK and tools
npm install firebase-functions#latest firebase-admin#latest --save
npm install -g firebase-tools
I started getting the following error when trying to deploy my firebase functions:
TypeError: instance.INTERNAL.registerComponent is not a function
at registerDatabase (/Users/jr/projects/docavea/functions/node_modules/#firebase/database/dist/index.node.cjs.js:15168:39)
at Object.<anonymous> (/Users/jr/projects/docavea/functions/node_modules/#firebase/database/dist/index.node.cjs.js:15200:5)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Module.require (internal/modules/cjs/loader.js:692:17)
at require (internal/modules/cjs/helpers.js:25:18)
at FirebaseNamespace.get [as database] (/Users/jr/projects/docavea/functions/node_modules/firebase-admin/lib/firebase-namespace.js:282:38)
I have found posts on https://github.com/firebase/firebase-admin-node/issues/714 and
https://github.com/firebase/firebase-admin-node/issues/717 and https://twitter.com/plane1113/status/1203009025232654336 that suggests various solutions for this problem, but none of them worked for me, except to include #firebase/app. Some suggested that this was cause by having #firebase/app somewhere in my project but when I ran
npm ls #firebase/app I got
functions# /Users/jr/projects/docavea/functions
└── (empty)
So my question is: What is #firebase/app and is it a problem to include it in my firebase functions package.json
{
"name": "functions",
"scripts": {
"lint": "tslint --project tsconfig.json",
"build": "tsc",
"serve": "npm run build && firebase serve --only functions",
"shell": "npm run build && firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"main": "lib/src/index.js",
"dependencies": {
"#firebase/app": "^0.5.0",
"#sendgrid/mail": "^6.4.0",
"cors": "^2.8.5",
"firebase-admin": "^8.9.0",
"firebase-functions": "^3.3.0",
"lodash": "^4.17.15",
"mailchimp-api-v3": "^1.13.1"
},
"peerDependencies": {
"#google-cloud/firestore": "^3.3.2"
},
"engines": {
"node": "8"
},
"devDependencies": {
"tslint": "^5.20.1",
"typescript": "^3.7.4"
},
"private": true
}

First, to answer the question #firebase/app is a module that is part of the Firebase Web SDK (you get it by npm install firebase) and never needs to be manually installed, and should not be installed at all for firebase-admin.
So in a nutshell, here is what's going on with this error, in general: firebase-admin has a dependency called #firebase/database, which is shared with the Firebase Web SDK. A recent release of #firebase/database had some changes that caused it to not work if used with an older version of #firebase/app.
So basically, it should work if you have either (1) no #firebase/app or (2) a current #firebase/app. Your error sounds most likely to be an existing outdated #firebase/app.
I know you said you don't seem to have #firebase/app in your project but since it seems like the most likely cause, I'm wondering if you have it in a parent directory. (It's installed as part of the Firebase Web SDK, as in npm install firebase.) Most firebase functions (if you use firebase init) are set up in a /functions subdirectory of the main project repo, with its own package.json, and the rest of the project is one level up and has another package.json. And that looks like it might be your dir structure?
In that case, running npm ls inside /functions will turn up an empty result, but if you go up to the parent dir (that contains the other package.json), it will show up. Node will go up a level if it doesn't find a package at the current one. See if you can ensure both levels are clean from any #firebase/app, delete both node_modules dirs, reinstall. If you do need it in the top level (for Firebase Web SDK), make sure it is upgraded the most current firebase (7.6.1).
As a last resort, maybe check to see if firebase is npm installed globally somehow.
If you definitely don't have #firebase/app at any level of your project and it's still failing, this is a bug we should look into.
Sorry that's a lot of info! Hope that wasn't too much. TLDR: I think you do probably have #firebase/app in your project root dir? If not, please file an issue on Github.

Based on #Christina Holland answer I investigated a bit further. When I ran npm ls #firebase/app in my Angular folder (functions is a subfolder of that) I got:
└─┬ UNMET PEER DEPENDENCY firebase#7.6.1
└── #firebase/app#0.5.0
npm ERR! peer dep missing: firebase#>= 5.5.7 <7, required by #angular/fire#5.2.1
I then updated #angular/fire to the newest version. It didn't change the #firebase/app version, but it removed the error:
└─┬ firebase#7.6.1
└── #firebase/app#0.5.0
After that I could remove #firebase/app from my a functions package.json and I could deploy without any errors.
I don't understand what happens here, but the problem is solved and maybe it can help some other developer with this problem.

Related

node: how to avoid installing global packages

I'm looking for a pattern to avoid the need of global packages when working with node, I'd like to install everything I need with npm install and then just running every command with npm run xxx, without any global package installed.
For example, I have jest configured to run my tests.
These are the dependencies in my package.json:
[...]
},
"author": "",
"license": "ISC",
"dependencies": {
"#types/express": "^4.16.1",
"#types/node": "^11.10.5",
"express": "^4.16.4",
"ts-node-dev": "^1.0.0-pre.32",
"typescript": "^3.3.3333"
},
"devDependencies": {
"#types/jest": "^24.0.9",
"#types/supertest": "^2.0.7",
"jest": "^24.3.1",
"nodemon": "^1.18.10",
"supertest": "^4.0.0",
"ts-jest": "^24.0.0"
}
[...]
and these are some scripts I have configured:
[...]
"scripts": {
"test": "jest --coverage",
"tsc": "tsc",
"watch": "nodemon --watch 'src/**/*.ts' --exec 'ts-node' src/server.ts"
},
[...]
But when I issue npm run test I get this error:
$ npm run test
> ci-test#0.0.1 test /home/sas/devel/apps/vue/ci-test
> jest --coverage
sh: 1: jest: not found
npm ERR! file sh
[...]
If I install jest globally with npm install -g jest everything runs fine, but that's precisely what I'm trying to avoid.
A few assumptions I made that might be wrong:
when running scripts node first looks for commands in node_modules/.bin (in order to use locally installed packages)
when I issue npm install every command line command is installed to node_modules/.bin
This last one is not working, because even though I have jest in my devDependencies there's no node_modules/.bin/jest file in my project
$ ls node_modules/.bin/
acorn cdl esgenerate esvalidate is-ci json5 loose-envify mime nodetouch parser semver sshpk-sign strip-indent watch
atob escodegen esparse import-local-fixture jsesc js-yaml marked mkdirp nopt rc sshpk-conv sshpk-verify uglifyjs
On the other hand, as a workaround, the following seems to work:
"scripts": {
"test": "npx jest --coverage",
But it takes more than 10 seconds for npx to install jest everytime I run npm run test
So, what would be the correct way to achieve it? O how can I tell npm to install jest to node_modules/.bin and use it whe I reference it in my scripts?
It seems like it was easier than expected, I just had to issue:
npm install --only=dev
seems like by default npm won't install dev dependencies
I did a couple more tests, playing with the NODE_ENV var, and after unsetting it npm install seems to install also devDependencies, along with jest in node_modules/.bin/jest. It seems like somehow it was assuming I was in production mode.
Another trick I learned to avoid installing global dependencies is to install it with --save-dev, and then adding a script to run it with npm run. For example, to avoid installing jest globally but still be able to use it from the command line you should:
npm install jest --save-dev
Then add the following to your package.json
scripts: {
"jest": "jest"
}
And then you can run it from the command line with npm run jest. To pass params from the command line you have to add a '--' before the params, like this: npm run jest -- --coverage. Or you could just issue npx jest --coverage, if installed, npx will use jest from node_modules/.bin. (check this for more info)
BTW, this answer to a similar question might be useful

Webpack not working in my node.js project. fsevents

I'm currently trying to implement this tutorial:
https://www.typescriptlang.org/docs/handbook/react-&-webpack.html
I'm supposed to install react and react-dom and also webpack + typescript + awesome-typescript-loader + source-map-loader, and that's what I did. I also installed webpack-cli accordingly to instructions that I got from the command line.
I installed all of them locally (the react and react-dom as PROD and the rest as DEV dependencies).Currently I don't have any packages installed globally.
After this, that's my package.json file:
{
"name": "reactandwebpack-tutorial",
"version": "1.0.0",
"description": "",
"main": "webpack.config.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"devDependencies": {
"awesome-typescript-loader": "^5.2.0",
"source-map-loader": "^0.2.3",
"typescript": "^2.7.2",
"webpack": "^4.16.4",
"webpack-cli": "^3.1.0"
},
"dependencies": {
"#types/react": "^16.4.7",
"#types/react-dom": "^16.0.6",
"react": "^16.4.2",
"react-dom": "^16.4.2"
}
}
At this point, when I run npm ls I get a bunch of errors, because of some optional dependency of webpack that apparently is missing (all the errors in the tree are inside webpack and below fsevents as following):
webpack#4.16.4
(...)watchpack#1.6.0
(...)chokidar#2.0.4
(...)fsevents#1.2.4 -> UNMET OPTIONAL DEPENDENCY
And everything below fsevents is also marked with UNMET DEPENDENCY
And when I run webpack command, I get a "webpack not recognized error".
Anyone can help? I've been trying to wrap my head around this for a while.
PS:
Npm -v 5.6.0
Node -v 8.11.3 //(that's what I get from the terminal,
//VSCode "About" tells me something different, I don't know why).
Using Visual Code
Version 1.24.0
Date 2018-06-06T17:35:40.560Z
Shell 1.7.12
Renderer 58.0.3029.110
Node 7.9.0
Architecture x64
The reason is because it was not linked to your env. When you install something globally, you have access to it everywhere, hence it works just by doing webpack. Since you installed everything locally, the binaries are located inside node_modules/.bin.
You have two options when you install something locallly.
Use npm scripts (npm run build, watch... whatever).
./node_modules/.bin/moduleName --flags
It is easier to create a npm script and add all the commands there.
SOLVED
Not sure the reason but it had something to do with the ./bin folder with the webpack-cli "ambient variable" not being available (I don't know it that would be the most accurate description).
When I try to run webpack, I get "not recognized error".
But when I run nodemodules\.bin\webpack-cli it works normally.
Everything is installed locally.
I can also run it with options, like nodemodules\.bin\webpack-cli --help
If you run "webpack", The CLI will find global webpack with is installed by (npm install webpack -g). To use webpack from local project. you should it to npm script.
package.json
{
"script": {
"start": "webpack"
}
}
By doing this, you can run npm start to run webpack.

webpack error on build run. 'Error: Cannot find module '#webassemblyjs/ast''

I' am currently working in React, and I'm trying to setup webpack and webpack-cli. I am currently following the tutorial on webpack 4 tutorial site https://www.valentinog.com/blog/webpack-4-tutorial/ using my command line I've been trying to install webpack and webpack-cli as my dependencies using nodejs.
I've been entering npm i webpack --save-dev and npm i webpack-cli --save-dev
Both these modules install correctly and are added to my package.json as dependencies.
{
"name": "webpack-4-quickstart",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "webpack"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"webpack": "^4.10.1",
"webpack-cli": "^2.1.4"
}
}
I also have a scripts section that has a "build": "webpack" which should initialize my webpack. But when I type npm run build i get this error. Error: Cannot find module '#webassemblyjs/ast'. I've checked my node_modules and #webassemblyjs is there.
I've tried uninstalling my node_modules, reinstalling webpack and webpack-cli and even moved to a different directory and repository to see if I could change my outcome that way. However, I'm still getting this cannot find module #webassemblyjs/ast. I'm currently running MacOs High Sierra, and am not sure if maybe it's just not agreeing with my operating system. If anything I haven't noticed any other Stackoverflow posts dealing with this specific error, so I thought I'd make a post asking for help.
Thankyou for your time!
I had the same problem. After trying many variations of version, deleting node modules a couple times, trying to manually install the library, I finally solved it by running
npm ci
~1 day hours of debugging, 6 letter command. Computers are so fun sometimes
try this
update "webpack": "^4.10.1" to "webpack": "^3.11.0"
No issues on macOS High Sierra with:
"devDependencies": {
"webpack": "^4.12.0",
"webpack-cli": "^3.0.8"
}
Try first upgrading to those versions and see if that resolves the issue.
Also, are you using a private registry? If so, you might be getting something like this if #webassemblyjs can't be found in it:
npm ERR! code E404
npm ERR! 404 Not Found: #webassemblyjs/ast#1.5.12
You could quickly fix this by adding #webassemblyjs:registry=https://registry.npmjs.org in your .npmrc file.
Otherwise, you could try running webpack --display-error-detail and see if you get additional details about the error.

Why does not the global npm package kick in for module dependency?

I have already installed my typescript and ts-node globally.
I have a package.json file like the following, and when I run npm test, everything works (NOTE: as you can see, ts-node and typescript are installed as local modules in this situation)
{
"name": "two_sum",
"version": "1.0.0",
"description": "two sum problem solver",
"main": "main.js",
"scripts": {
"test": "./node_modules/.bin/mocha --compilers ts:ts-node/register ./test/*.spec.ts"
},
"devDependencies": {
"chai": "^4.1.2",
"mocha": "^4.1.0",
"ts-node": "^4.1.0",
"typescript": "^2.6.2"
}
}
However, if I take two two modules out of the package.json (and delete the two packages from my ./mode_modules), i.e, ts-node and typescript are not installed locally, I got errors like this:
Error: Cannot find module 'typescript'
at Function.Module._resolveFilename (module.js:470:15)
....
at Module._compile (module.js:571:32)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:427:7)
at startup (bootstrap_node.js:151:9)
at bootstrap_node.js:542:3
npm ERR! Test failed. See above for more details.
Here is my question: why would I need to have those two package installed locally? Why can't the global package kick in to resolve the dependency?
Globally installed packages are not involved in the resolution when your code runs.
This is because the project (denoted by the presence of your package.json) have no knowledge of what has been installed globally.
Globally installed packages ties to the environment, not the project.
If the globally installed packages are used in the resolution, then the project may work in your environment but not others.
So that will make the executability of your project not reliable (or put in another word, coupled or depended on your environment).
Nowadays, install global packages for CLI tools only.

cross-env/dist/index.js:42 unexpected token }

A little background: I'm developing a Laravel 5 application. I'm developing locally on Windows 7 using Homestead (using Vagrant/VirtualBox) and deploying to an Amazon EC2 instance through CodePipeline.
While trying to get Laravel Mix working locally, I kept running into a cacophony of errors that eventually led me to the solution of installing cross-env globally rather than including it in the devDependencies in package.json. Of course, this also means that my Amazon deployment needed to be updated to that setup as well. None of this is really relevant except to explain that cross-env is installed globally on my deployment, in case that's relevant.
My package.json:
{
"private": true,
"scripts": {
...
"production": "cross-env NODE_ENV=production node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js"
},
"devDependencies": {
"axios": "^0.16.2",
"bootstrap-sass": "^3.3.7",
"jquery": "^3.1.1",
"laravel-mix": "^1.0",
"lodash": "^4.17.4",
"vue": "^2.1.10"
}
}
My deployment to AWS fails on npm run production with the following error:
[stderr]/usr/lib/node_modules/cross-env/dist/index.js:42
[stderr] });
[stderr] ^
[stderr]SyntaxError: Unexpected token }
What could be causing this issue? And how can I resolve it? I highly doubt that there's actually a syntax error in cross-env.
I had the same problem and i could fix it by updating node and re-installing npm.
Steps that worked for me:
Update Node using a package manager:
sudo npm cache clean -f
sudo npm install -g n
sudo n stable
Then fresh install:
rm -rf node_modules && npm cache clean --force && npm install
Hope it helps
In my case I had to upgrade both nodejs and npm:
wget https://nodejs.org/dist/v6.9.2/node-v6.9.2-linux-x64.tar.xz
tar -C /usr/local --strip-components 1 -xJf node-v6.9.2-linux-x64.tar.xz
Then I run npm install without any errors.

Resources