I am using the firebase-admin library in my node application.
My index.js is empty, with the exception of the require statement for firebase-admin...
var admin = require('firebase-admin');
The exception being thrown from the require statement occurs at following line in firebase-admin/lib/default-namespace.js
const firebase_namespace_1 = require("./app/firebase-namespace");
The exception message is as follows...
Exception has occurred: SyntaxError: Unexpected token ?
at new Script (vm.js:83:7)
at createScript (vm.js:267:10)
at Object.runInThisContext (vm.js:319:10)
at Module._compile (internal/modules/cjs/loader.js:686:28)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:734:10)
at Module.load (internal/modules/cjs/loader.js:620:32)
at tryModuleLoad (internal/modules/cjs/loader.js:560:12)
at Function.Module._load (internal/modules/cjs/loader.js:552:3)
at Module.require (internal/modules/cjs/loader.js:659:17)
at require (internal/modules/cjs/helpers.js:22:18)
My package.json is as follows...
{
"name": "myapp",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"firebase-admin": "^11.0.1"
}
}
I am running node version v16.16.0
Related
The problem is the same as this question: Firebase Cloud Functions Deploy Error- SyntaxError: Unexpected token '?'
and I've tried all the answers but nothing seems to work.
This is what I get when I try and deploy functions (even the ones that were deploying fine a week ago):
Error: Error occurred while parsing your function triggers.
/Users/islamariful/node_modules/firebase-admin/lib/app/firebase-namespace.js:84
this.INTERNAL = new FirebaseNamespaceInternals(appStore ?? new lifecycle_1.AppStore());
^
SyntaxError: Unexpected token '?'
at wrapSafe (internal/modules/cjs/loader.js:1052:16)
at Module._compile (internal/modules/cjs/loader.js:1100:27)
at Module._compile (pkg/prelude/bootstrap.js:1394:32)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1156:10)
at Module.load (internal/modules/cjs/loader.js:984:32)
at Function.Module._load (internal/modules/cjs/loader.js:877:14)
at Module.require (internal/modules/cjs/loader.js:1024:19)
at Module.require (pkg/prelude/bootstrap.js:1338:31)
at require (internal/modules/cjs/helpers.js:72:18)
at Object.<anonymous> (/Users/islamariful/node_modules/firebase-admin/lib/default-namespace.js:19:30)
This is my package.json file:
{
"name": "functions",
"description": "Cloud Functions for Firebase",
"scripts": {
"lint": "eslint .",
"serve": "firebase emulators:start --only functions",
"shell": "firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"engines": {
"node": "16"
},
"main": "index.js",
"dependencies": {
"axios": "^0.21.1",
"firebase-admin": "^9.8.0",
"firebase-functions": "^3.14.1",
"pdfmake": "^0.2.2"
},
"devDependencies": {
"eslint": "^7.6.0",
"eslint-config-google": "^0.14.0",
"eslint-plugin-jsdoc": "^36.0.7",
"firebase-functions-test": "^0.2.0"
},
"private": true
}
I've tried multiple things but still can't get this to work. Any ideas on how I can fix this?
Please tell me how to set up the preact-render-to-string on the express.
https://github.com/preactjs/preact-render-to-string#render-jsx--preact--whatever-via-express
https://expressjs.com/en/starter/installing.html
I built it reading the above links. The source code is on there, but I'm not used to node
I don't know how to execute it(Or I don't know if I'm failing to build the environment.).
I believe my installation procedure can be found in package.json(dependencies and devDependencies). So, below is my package.json.
My package.json:
{
"name": "y",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.17.1"
},
"devDependencies": {
"preact": "^10.5.14",
"preact-render-to-string": "^5.1.19"
}
}
My app.js (Same as Document):
I didn't know what to do with the file name, so I used app.js.
import express from 'express';
import { h } from 'preact';
import render from 'preact-render-to-string';
/** #jsx h */
// silly example component:
const Fox = ({ name }) => (
<div class="fox">
<h5>{ name }</h5>
<p>This page is all about {name}.</p>
</div>
);
// basic HTTP server via express:
const app = express();
app.listen(8080);
// on each request, render and return a component:
app.get('/:fox', (req, res) => {
let html = render(<Fox name={req.params.fox} />);
// send it back wrapped up as an HTML5 document:
res.send(`<!DOCTYPE html><html><body>${html}</body></html>`);
});
Run and Error:
$ node app.js
src/pr/ex/app.js:1
import express from 'express';
^^^^^^
SyntaxError: Cannot use import statement outside a module
at Object.compileFunction (node:vm:352:18)
at wrapSafe (node:internal/modules/cjs/loader:1031:15)
at Module._compile (node:internal/modules/cjs/loader:1065:27)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:79:12)
at node:internal/main/run_main_module:17:47
I think I am running it wrong or failed to build the environment, what can I do to make it run successfully?
node is installed with nodebrew. The current status is as follows.
$ nodebrew list
v16.6.2
current: v16.6.2
Edit:
I tried the answer.
add the top-level "type" field with a value of "module"
The error SyntaxError: Cannot use import statement outside a module is gone and different error occurred.
$ node app.js
file:///src/pr/ex/app.js:8
<div class="fox">
^
SyntaxError: Unexpected token '<'
at Loader.moduleStrategy (node:internal/modules/esm/translators:146:18)
at async link (node:internal/modules/esm/module_job:67:21)
Since I am using preact (I must be using htm internally), it is odd that Unexpected token '<' would be an error.
package.json after editing:
{
"name": "y",
"version": "1.0.0",
"type": "module",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.17.1"
},
"devDependencies": {
"preact": "^10.5.14",
"preact-render-to-string": "^5.1.19"
}
}
Possible solutions:
Use require:
const express = require('express')
const { h } = require('preact')
Save your app.js file as a .mjs file (Node13+ only)
Add { type: 'module' } in your package.json
I run npm init npm i -D jest like in this tutorial
I am getting this error after running commend nmp test
This is not an error from creatures.js or creatures.test.js because without this file error happening. How can I fix this ? I tried already reinstating mode_modules. I don't know it have a matter but I'm using node 8.17.0 because I'm working with Firebase cloud functions
PS C:\Users\Pawel\Desktop\HerosIIIJS> npm test
> heros_iii_js#1.0.0 test C:\Users\Pawel\Desktop\HerosIIIJS
> jest
C:\Users\Pawel\Desktop\HerosIIIJS\node_modules\jest\node_modules\jest-cli\build\cli\index.js:227
} catch {
^
SyntaxError: Unexpected token {
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Module._compile (module.js:617:28)
at Object.Module._extensions..js (module.js:664:10)
at Module.load (module.js:566:32)
at tryModuleLoad (module.js:506:12)
at Function.Module._load (module.js:498:3)
at Module.require (module.js:597:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (C:\Users\Pawel\Desktop\HerosIIIJS\node_modules\jest\node_modules\jest-cli\bin\jest.js:16:3)
npm ERR! Test failed. See above for more details.
const getProjectListFromCLIArgs = (argv, project) => {
const projects = argv.projects ? argv.projects : [];
if (project) {
projects.push(project);
}
if (!projects.length && process.platform === 'win32') {
try {
projects.push((0, _jestUtil().tryRealpath)(process.cwd()));
} catch { // <= error
// do nothing, just catch error
// process.binding('fs').realpath can throw, e.g. on mapped drives
}
}
if (!projects.length) {
projects.push(process.cwd());
}
return projects;
};
package.json
{
"name": "heros_iii_js",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "jest"
},
"author": "",
"license": "ISC",
"devDependencies": {
"jest": "^26.6.2"
}
}
file structure:
Upgrade node.js
In old version of node they were not handled the catch block of jest file.
It will work from node -v9.11.2 to so on.
I'm using Heroku and I get this error..
2016-10-10T03:34:11.188366+00:00 app[web.1]: Error: Cannot find module 'module1.js'
2016-10-10T03:34:11.188369+00:00 app[web.1]: at Function.Module._resolveFilename (module.js:339:15)
2016-10-10T03:34:11.188370+00:00 app[web.1]: at Function.Module._load (module.js:290:25)
2016-10-10T03:34:11.188371+00:00 app[web.1]: at Module.require (module.js:367:17)
2016-10-10T03:34:11.188371+00:00 app[web.1]: at require (internal/module.js:20:19)
2016-10-10T03:34:11.188372+00:00 app[web.1]: at Object.<anonymous> (/app/server.js:42:15)
2016-10-10T03:34:11.188373+00:00 app[web.1]: at Module._compile (module.js:413:34)
2016-10-10T03:34:11.188374+00:00 app[web.1]: at Object.Module._extensions..js (module.js:422:10)
2016-10-10T03:34:11.188374+00:00 app[web.1]: at Module.load (module.js:357:32)
2016-10-10T03:34:11.188375+00:00 app[web.1]: at Function.Module._load (module.js:314:12)
2016-10-10T03:34:11.188375+00:00 app[web.1]: at Function.Module.runMain (module.js:447:10)
app.js
/**CUSTOM_MODULES**/
var module1 = require('module1.js');
/**MODULES_END**/
module1.js
function module1(){
//My code
}
module.exports = module1;
module1.js is in the same directory as my app.js.
I have tried countless things but nothing seems to work.
I have tried:
var module1 = require('module1.js');
var module1 = require('./module1.js');
var module1 = require('../module1.js');
var module1 = require('module1');
package.json : app.js
{
"name": "nano-server",
"version": "1.0.0",
"description": "",
"main": "server.js",
"dependencies": {
"express": "^4.14.0",
"mysql": "^2.11.1",
"socket.io": "^1.4.8",
"module1": "0.0.0"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js"
},
"author": "",
"license": "ISC"
}
package.json : module1
{
"name": "module1",
"version": "0.0.0",
"description": "get something",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": {
"name": "sergey"
},
"license": "BSD-2-Clause",
"readme": "ERROR: No README data found!",
"_id": "module1#0.0.0",
"dist": {
"shasum": "dc9b96f8a43b596bf735da4d2004ba410429bde0",
"tarball": "https://registry.npmjs.org/module1/-/module1-0.0.0.tgz"
},
"_from": "module1#latest",
"_npmVersion": "1.3.11",
"_npmUser": {
"name": "sergey-user",
"email": "aaa#mail.ru"
},
"maintainers": [
{
"name": "sergey-user",
"email": "aaa#mail.ru"
}
],
"directories": {},
"_shasum": "dc9b96f8a43b596bf735da4d2004ba410429bde0",
"_resolved": "https://registry.npmjs.org/module1/-/module1-0.0.0.tgz"
}
directory
_server
.git
node_modules
.bin
express
module1
index.js
package.json
mysql
node-mysql
node-uuid
socket.io
package.json
server.js
Try var module1 = require('./module1');
Adding your module as a dependency to your application would resolve this issue. To achieve that try the below command.
npm install module1 --save
Hope this helps!
install npm -g and try again afterwords.
In my case, after trying everything without any success, updating node helped (v10.3.1 -> v12.13.1).
Why am I getting this error?
Uncaught Error: Cannot find module './charset.js'
package.json
{
"name": "app",
"version": "1.0.0",
"description": "Application built with Flux and React.js",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Joe Collins",
"license": "",
"devDependencies": {
"browserify": "*",
"flux": "*",
"gulp": "*",
"reactify": "*",
"vinyl-source-stream": "*",
"object-assign": "*"
},
"dependencies": {
"bufferutil": "^1.2.1",
"charset": "^1.0.0",
"http": "0.0.0",
"react": "^0.14.7",
"react-dom": "^0.14.7",
"socket.io": "^1.4.5",
"utf-8-validate": "^1.2.1"
}
}
gulpfile.js
var gulp = require('gulp');
var browserify = require('browserify');
var reactify = require('reactify');
var source = require('vinyl-source-stream');
gulp.task('browserify', function() {
browserify('./src/js/main.js')
.transform('reactify')
.bundle()
.pipe(source('main.js'))
.pipe(gulp.dest('dist/js'))
});
gulp.task('copy', function() {
gulp.src('src/index.html')
.pipe(gulp.dest('dist'));
gulp.src('src/css/*.*')
.pipe(gulp.dest('dist/css'));
gulp.src('src/js/vendors/*.*')
.pipe(gulp.dest('dist/js'));
});
gulp.task('default', ['browserify', 'copy'], function() {
return gulp.watch('src/**/*.*', ['browserify', 'copy']);
});
App.js
var io = require('socket.io')();
You have to use socket.io-client for client side stuff. I have a hunch that charset.js uses a binary, or something that can't be converted to the browser.
https://github.com/socketio/socket.io-client
I was facing same problem but with some other module I did reinstall with latest and it is working for me
Problem
Error: Cannot find module './charset.js'
1) If you want to compile the package/file into executable, please pay attention to compilation warnings and specify a literal in 'require' call. 2) If you don't want to compile the package/file into executable and want to 'require' it from filesystem (likely plugin), specify an absolute path in 'require' call using process.cwd() or process.execPath.
at Function.Module._resolveFilename (module.js:534:15)
at Function.Module._resolveFilename (pkg/prelude/bootstrap.js:1269:46)
at Function.Module._load (module.js:464:25)
at Module.require (module.js:577:17)
at Module.require (pkg/prelude/bootstrap.js:1153:31)
at require (internal/module.js:11:18)
at /snapshot/html/config/configurator-file-api/node_modules/accepts/node_modules/negotiator/lib/negotiator.js:21:16
at Array.forEach (<anonymous>)
at Object.<anonymous> (/snapshot/html/config/configurator-file-api/node_modules/accepts/node_modules/negotiator/lib/negotiator.js:19:18)
at Module._compile (pkg/prelude/bootstrap.js:1243:22)
at Object.Module._extensions..js (module.js:644:10)
at Module.load (module.js:552:32)
at tryModuleLoad (module.js:495:12)
at Function.Module._load (module.js:487:3)
at Module.require (module.js:577:17)
at Module.require (pkg/prelude/bootstrap.js:1153:31)
Solution
cd /var/html/config/configurator-file-api/node_modules/accepts/
npm uninstall negotiator --save
npm install negotiator --save
Yes it is working