Error in deploy Cloud Functions .json not found - json

I can not deploy my functions when I try to import a .json credential file.
When running firebase init functions I selected the TypeScript
option.
Then I put my functions in TypeScript and tried loading the .json
file with the credentials of my project in order to use
firebase-admin.
/functions (Directory)
tsconfig.json
{
"compilerOptions": {
"lib": ["es6"],
"module": "commonjs",
"noImplicitReturns": true,
"outDir": "lib",
"sourceMap": true,
"target": "es6",
"types" : [ "node" ],
"esModuleInterop": true,
"resolveJsonModule": true,
},
"compileOnSave": true,
"include": [
"src",
"./typings.d.ts"
]
}
typings.d.ts:
declare module "*.json" {
const value: any;
export default value;
}
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/index.js",
"dependencies": {
"firebase-admin": "~6.0.0",
"firebase-functions": "^2.0.3"
},
"devDependencies": {
"tslint": "~5.8.0",
"typescript": "~2.8.3"
},
"private": true
}
functions/src/
-- serviceAccountKey.json
-- index.ts
index.ts:
import * as admin from 'firebase-admin';
import * as serviceAccount from './serviceAccountKey.json';
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "DATABASE-URL"
});
If I remove the import works normally, I can not deploy when I import the file.
Error:
i deploying functions
Running command: npm --prefix "functions" run lint
> functions# lint Z:\functions
> tslint --project tsconfig.json
Running command: npm --prefix "functions" run build
> functions# build Z:\functions
> tsc
tsconfig.json(11,5): error TS5023: Unknown compiler option 'resolveJsonModule'.
+ functions: Finished running predeploy script.
i functions: ensuring necessary APIs are enabled...
+ functions: all necessary APIs are enabled
i functions: preparing functions directory for uploading...
Error: Error parsing triggers: Cannot find module './serviceAccountKey.json'
Try running "npm install" in your functions directory before deploying.

You should use require() (not import) to pull in the contents of a JSON file. If you really must use import (I don't recommend it), read this.

If I remove the import works normally, I can not deploy when I import the file.
A few tsconfig options are missing:
"esModuleInterop": true,
"resolveJsonModule": true,

Related

Firebase Functions Cannot find name 'BigUint64Array', ''BigInt', ''BigInt64Array'

Dependencies:
"dependencies": {
"cors": "^2.8.5",
"firebase-admin": "^8.6.0",
"firebase-functions": "^3.11.0",
"nodemailer": "^6.4.11"
},
"devDependencies": {
"firebase-functions-test": "^0.1.6",
"tslint": "^5.12.0",
"typescript": "^3.2.2"
},
Description:
Since updating to firebase-functions 3.11.0, I get following error when trying to deploy my functions with firebase deploy --only functions:
node_modules/#types/jsdom/ts3.5/index.d.ts:8:24 - error TS2304: Cannot find name 'BigInt'.
8 BigInt: typeof BigInt;
~~~~~~
node_modules/#types/jsdom/ts3.5/index.d.ts:9:31 - error TS2304: Cannot find name 'BigInt64Array'.
9 BigInt64Array: typeof BigInt64Array;
~~~~~~~~~~~~~
node_modules/#types/jsdom/ts3.5/index.d.ts:10:32 - error TS2304: Cannot find name
'BigUint64Array'.
10 BigUint64Array: typeof BigUint64Array;
~~~~~~~~~~~~~~
Found 3 errors.
Even going back to the previous version doesn´t seem to solve the issue, so I´m not sure if the update was the trigger.
#edit 1 (full 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"
},
"engines": {
"node": "10"
},
"main": "lib/index.js",
"dependencies": {
"cors": "^2.8.5",
"firebase-admin": "^8.6.0",
"firebase-functions": "^3.11.0",
"nodemailer": "^6.4.11"
},
"devDependencies": {
"firebase-functions-test": "^0.1.6",
"tslint": "^5.12.0",
"typescript": "^3.2.2"
},
"private": true
}
I think I have it. From npm #types/jsdom I went to package homepage where I found this part.
It seems that you need to install additional package:
npm install --save-dev #types/node
I have replicated this issue with firebase init and replacing package.json with yours. I got the same issue than. After package install with above command my function was deployed with no errors.
One remark: you do not have #types/jsdom in package.json, which according to my understanding should be there. I have installed this package before replacing the file, to have similar situation.

npm run build > not creating dist folder in NestJs project

I tried to start my project with npm run start:prod command but got
Error: Cannot find module {path to my project}\dist\main.js'.
I have tried to rename the path to all my files in the project from src/myController to ../myController .
My package.json (scripts)
"scripts": {
"build": "tsc -p tsconfig.build.json",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"start": "ts-node -r tsconfig-paths/register src/main.ts",
"start:dev": "concurrently \"wait-on dist/main.js && nodemon\" \"tsc -w -p tsconfig.json\" ",
"start:debug": "nodemon --config nodemon-debug.json",
"prestart:prod": "rimraf dist && npm run build",
"start:prod": "node dist/main.js"
My tsconfig.json
{
"compilerOptions": {
"module": "commonjs",
"declaration": true,
"noImplicitAny": false,
"removeComments": true,
"allowSyntheticDefaultImports": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es6",
"sourceMap": true,
"rootDir": ".",
"outDir": "../dist",
"baseUrl": "./src"
},
"include": [ "./src/**/*.ts", ],
"exclude": ["node_modules", "dist", "src/**/*.spec.ts", "src/**/__test__/*"]
}
The actual output:
nest-typescript-starter#1.0.0 prestart:prod
{path to my project}
rimraf dist && npm run build
nest-typescript-starter#1.0.0 build
{path to my project}
tsc -p tsconfig.build.json
nest-typescript-starter#1.0.0 start:prod
{path to my project}
node dist/main.js
internal/modules/cjs/loader.js:584
throw err;
^
Error: Cannot find module
{path to my project}\dist\main.js'
Okeey, I was a little dumb. The answer is very easy. In package.json you need change line from "start:prod": "node dist/main.js" to "start:prod": "node dist/src/main.js". In tsconfig.json you need change "outDir": "../dist" to "outDir": "./dist"
you can just add a new command:
under the scripts in package.json :
"start:live": "rimraf dist && nest build && node dist/src/main",
then you can run:
npm run start:live

Unexpected token import, can not set up ES6 in my JS project with Mocha, Chai and Sinon

I am trying to set up a simple ES6 project to play with mocha, chai and sinon in the Terminal. But whenever I try to npm run test, I keep getting an error:
/Users/UserName/workspace/UnitTesting/mocha-sinon-chai/src/App.spec.js:1
(function (exports, require, module, __filename, __dirname) { import should from "chai";
^^^^^^
SyntaxError: Unexpected token import
Here are my:
1. Package.json:
{
"name": "mocha-sinon-chai",
"version": "1.0.0",
"description": "",
"main": "index.js",
"directories": {
"test": "tests"
},
"dependencies": {
"chai": "^4.1.2",
"chai-as-promised": "^7.1.1",
"sinon": "^4.4.1"
},
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-preset-env": "^1.6.1",
"mocha": "^5.0.1"
},
"scripts": {
"build": "babel src -d lib",
"test": "mocha ./tests/*.spec.js"
},
"author": "",
"license": "ISC"
}
2. .babelrc:
{
"presets": ["env"]
}
3. Root folder structure:
4. App.spec.js:
import should from "chai";
import { findOne } from "../src/App.js";
describe("App.js", function() {
it("should return true if predicate exists in the array", function() {
findOne([1, 2, 3], 3).should().be.true;
});
});
5. index.js is empty
6. App.js simply contains a normal JS function
I have checked many other similar issues here on Stackoverflow and GitHub and none of those solved my problem.
Adding this --require babel-core/register to test script inside package.json solved the problem:
"scripts": {
"build": "babel src -d lib",
"test": "mocha ./tests/*.spec.js --require babel-core/register"
},

sample path mismatching in AOT sample

I am trying to create Angular 2 sample ngc and JIT compiler. With JIT compiler i can run the sample but with ngc compiler am facing issue with component.html file path.
npm run ngc
ejangular2-systemjs-starter#1.0.0 ngc E:\AOT\seed-application-changes\sample-AOT
"node" copy-dist-files.js && rollup -c rollup-config.js && ngc -p tsconfig-aot.json
Error: Compilation failed. Resource file not found: E:/AOT/seed-application-changes/sample-AOT/src/src/app.
component.html
at ModuleResolutionHostAdapter.readResource (E:\AOT\seed-application-changes\sample-AOT\node_modules\#a
ngular\compiler-cli\src\compiler_host.js:291:19)
at CompilerHost.loadResource (E:\AOT\seed-application-changes\sample-AOT\node_modules\#angular\compiler
-cli\src\compiler_host.js:230:85)
at Object.get (E:\AOT\seed-application-changes\sample-AOT\node_modules\#angular\compiler\bundles\compil
er.umd.js:26365:111)
at DirectiveNormalizer._fetch (E:\AOT\seed-application-changes\sample-AOT\node_modules\#angular\compile
r\bundles\compiler.umd.js:13744:47)
at DirectiveNormalizer.normalizeTemplateAsync (E:\AOT\seed-application-changes\sample-AOT\node_modules\
#angular\compiler\bundles\compiler.umd.js:13790:25)
Below are my configuration.
sample structure
rollup.config.js
import nodeResolve from 'rollup-plugin-node-resolve'
import commonjs from 'rollup-plugin-commonjs';
import uglify from 'rollup-plugin-uglify'
export default {
entry: 'src/main.js',
dest: 'aot/build.js', // output a single application bundle
sourceMap: false,
format: 'iife',
onwarn: function(warning) {
// Skip certain warnings
// should intercept ... but doesn't in some rollup versions
if ( warning.code === 'THIS_IS_UNDEFINED' ) { return; }
// intercepts in some rollup versions
if ( warning.indexOf("The 'this' keyword is equivalent to 'undefined'") > -1 ) { return; }
// console.warn everything else
console.warn( warning.message );
},
plugins: [
nodeResolve({jsnext: true, module: true}),
commonjs({
include: 'node_modules/rxjs/**',
}),
uglify()
]
}
tsconfig-aot.json
{
"compilerOptions": {
"target": "es5",
"module": "es2015",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"lib": ["es2015", "dom"],
"noImplicitAny": true,
"suppressImplicitAnyIndexErrors": true
},
"files": [
"src/app.module.ts",
"src/main-aot.ts"
],
"angularCompilerOptions": {
"genDir": "aot",
"skipMetadataEmit" : true
}
}
package.json
"scripts": {
"start": "concurrently \"npm run tsc:w\" \"npm run lite\" ",
"lite": "lite-server",
"tsc": "tsc",
"tsc:w": "tsc -w",
"ngc": "\"node\" copy-dist-files.js && rollup -c rollup-config.js && ngc -p tsconfig-aot.json",
"serve": "lite-server -c bs-config.aot.json"
},

"Invalid configuration object" after creating a package.json using "npm init"

I had a running webpack configuration. But after using npm init to create the following package.json file:
{
"name": "y",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"devDependencies": {
"babel-core": "^6.24.0",
"babel-loader": "^6.4.0",
"babel-preset-es2015": "^6.24.0",
"babel-preset-react": "^6.23.0",
"jshint": "^2.9.4",
"jshint-loader": "^0.8.4",
"node-libs-browser": "^2.0.0",
"webpack": "^2.2.1"
}
}
and installing the following modules:
npm install babel-core babel-loader jshint jshint-loader node-libs-browser
babel-preset-es2015 babel-preset-react webpack --save-dev
I got the error message
Invalid configuration object. Webpack has been initialised using a configuration
object that does not match the API schema.
- configuration.resolve.extensions[0] should not be empty.
When I tried to restart the dev-server using
webpack-dev-server
The webpack.config.json looks like this:
module.exports = {
entry: ["./global.js", "./app.js"],
output: {
filename: "bundle.js"
},
module: {
loaders: [
{
test: /\.es6$/,
exclude: /node_modules/,
loader: 'babel-loader',
query: {
presets: ['react', 'es2015']
}
}
]
},
resolve: {
extensions: ['', '.js', '.es6']
},
}
What could be the problem?
You installed the latest version of webpack with:
npm install --save-dev webpack
Webpack 2 does not allow an empty string in resolve.extensions anymore. Simply remove the empty string from the array of extensions:
resolve: {
extensions: ['.js', '.es6']
},
You should also read the official Migration Guide.