Why require koa will cause error - koa

I just begin to learn node.js, when I require koa and execute "node app.js"
Have a error. Who can help me explain it ?
and how to fix it ?
Log:
/home/leon/Learn/JS/crawler/node_modules/koa/lib/response.js:47
const { res } = this;
^
SyntaxError: Unexpected token {
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:387:25)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Module.require (module.js:367:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/home/leon/Learn/JS/crawler/node_modules/koa/lib/application.js:11:18)
at Module._compile (module.js:413:34)
at Object.Module._extensions..js (module.js:422:10)
Code :
var koa = require('koa');
var app = koa();
app.listen(3000);

Related

Accessing AWS native npms for fails

I'm working my way through an example for AWS Transcribe right out of Amazon's documentation, but it's failing at the require, and I'm wondering what I'm missing for the setup to get this to work. The log reports Error: Cannot find module '#aws-sdk/client-transcribe' and you can see the full example and log below.
This is my first project with lambdas and node.js, but I've gotten all the other AWS Lambda code running, so I suspect this is an error on my part. I've been searching and come up empty-handed.
const {
TranscribeClient,
StartTranscriptionJobCommand,
} = require("#aws-sdk/client-transcribe");
// Set the AWS Region
const REGION = "REGION"; // For example, "us-east-1"
// Set the parameters
const params = {
TranscriptionJobName: "JOB_NAME",
LanguageCode: "LANGUAGE_CODE", // For example, 'en-US'
MediaFormat: "SOURCE_FILE_FORMAT", // For example, 'wav'
Media: {
MediaFileUri: "SOURCE_LOCATION",
// For example, "https://transcribe-demo.s3-REGION.amazonaws.com/hello_world.wav"
},
};
// Create an Amazon Transcribe service client object
const client = new TranscribeClient({ region: REGION });
const run = async () => {
try {
const data = await client.send(new StartTranscriptionJobCommand(params));
console.log("Success - put", data);
} catch (err) {
console.log("Error", err);
}
};
run();
And this is the entry in the log:
{
"errorType": "Runtime.ImportModuleError",
"errorMessage": "Error: Cannot find module '#aws-sdk/client-transcribe'\nRequire stack:\n- /var/task/index.js\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js",
"stack": [
"Runtime.ImportModuleError: Error: Cannot find module '#aws-sdk/client-transcribe'",
"Require stack:",
"- /var/task/index.js",
"- /var/runtime/UserFunction.js",
"- /var/runtime/index.js",
" at _loadUserApp (/var/runtime/UserFunction.js:100:13)",
" at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)",
" at Object.<anonymous> (/var/runtime/index.js:43:30)",
" at Module._compile (internal/modules/cjs/loader.js:999:30)",
" at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)",
" at Module.load (internal/modules/cjs/loader.js:863:32)",
" at Function.Module._load (internal/modules/cjs/loader.js:708:14)",
" at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)",
" at internal/main/run_main_module.js:17:47"
]
}
2021-01-29T23:25:31.952Z undefined ERROR Uncaught Exception {"errorType":"Runtime.ImportModuleError","errorMessage":"Error: Cannot find module '#aws-sdk/client-transcribe'\nRequire stack:\n- /var/task/index.js\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js","stack":["Runtime.ImportModuleError: Error: Cannot find module '#aws-sdk/client-transcribe'","Require stack:","- /var/task/index.js","- /var/runtime/UserFunction.js","- /var/runtime/index.js"," at _loadUserApp (/var/runtime/UserFunction.js:100:13)"," at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)"," at Object.<anonymous> (/var/runtime/index.js:43:30)"," at Module._compile (internal/modules/cjs/loader.js:999:30)"," at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)"," at Module.load (internal/modules/cjs/loader.js:863:32)"," at Function.Module._load (internal/modules/cjs/loader.js:708:14)"," at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)"," at internal/main/run_main_module.js:17:47"]}
The example that you are using imports and use AWS Service V3 package clients.
At the moment the Node.js runtimes in lambda environment include AWS SDK 2.804.0.
You must include the new version in your deployment.
Install npm, then run 'npm install' on the github repo for these examples, as per the prerequisite instructions here for these examples.
It will install all the required AWS SDK for JavaScript V3 modules required for the example. If these are missing the error you quote is triggered.
It turns out there was a way to get this same functionality without installing the AWS npms beyond whatever Amazon delivers with the base lambda instance.
const AWS = require('aws-sdk');
// Set the AWS Region
const REGION = "REGION"; // For example, "us-east-1"
// Set the parameters
const params = {
TranscriptionJobName: "JOB_NAME",
LanguageCode: "LANGUAGE_CODE", // For example, 'en-US'
MediaFormat: "SOURCE_FILE_FORMAT", // For example, 'wav'
Media: {
MediaFileUri: "SOURCE_LOCATION",
// For example, "https://transcribe-demo.s3-REGION.amazonaws.com/hello_world.wav"
},
};
// Create an Amazon Transcribe service client object
const client = new TranscribeClient({ region: REGION });
const run = async () => {
try {
transcribe.startTranscriptionJob(params, (err, data) => {
if (err) {
console.log("Success - put", data);
throw(err);
}
else
{
const data = await client.send(new StartTranscriptionJobCommand(params));
console.log("Success - put", data);
}
});
} catch (err) {
console.log("Error", err);
}
};
run();

graphql - TypeError: graphql_1.getIntrospectionQuery is not a function

After installing merge-graphql-schemas I attached these code in my server.js
const typeDefs = mergeTypes(fileLoader(path.join(__dirname, './schema')), { all: true });
const resolvers = mergeResolvers(fileLoader(path.join(__dirname, ".resolvers")));
I get an error like this
var parsedIntrospectionQuery = graphql_1.parse(graphql_1.getIntrospectionQuery());
^
**TypeError: graphql_1.getIntrospectionQuery is not a function**
at Object.<anonymous> (E:\My_WorkSpace\node\eticket-backend\node_modules\graphql-tools-fork\src\stitch\introspectSchema.ts:15:54)
at Module._compile (internal/modules/cjs/loader.js:1147:30)
at Module._extensions..js (internal/modules/cjs/loader.js:1167:10)
at Object.require.extensions.<computed> [as .js] (E:\My_WorkSpace\node\eticket-backend\node_modules\babel-register\lib\node.js:152:7)
at Module.load (internal/modules/cjs/loader.js:996:32)
at Function.Module._load (internal/modules/cjs/loader.js:896:14)
at Module.require (internal/modules/cjs/loader.js:1036:19)
at require (internal/modules/cjs/helpers.js:72:18)
at Object.<anonymous> (E:\My_WorkSpace\node\eticket-backend\node_modules\graphql-tools-fork\src\stitch\index.ts:448:1)
at Module._compile (internal/modules/cjs/loader.js:1147:30)
[nodemon] app crashed - waiting for file changes before starting...

iisnode: Unknown stdin file type

I have iisnode working smoothly on win8/IIS8. I created a very simple hello world and it works great. However, when I try to use process.stdin I get the following error:
Application has thrown an uncaught exception and is terminated:
Error: Implement me. Unknown stdin file type!
at process.startup.processStdio.process.openStdin [as stdin] (node.js:405:17)
at Object.<anonymous> (C:\ApprendaPlatform\SiteData\developer\v1\root\shim\node_modules\actionhero\bin\zzz.js:7:20)
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:362:17)
at require (module.js:378:17)
at Object.<anonymous> (C:\Program Files\iisnode\interceptor.js:210:1)
at Module._compile (module.js:449:26)
Note that I do not get this with process.stdout.
My code:
// kaboom!
var breakthings = process.stdin;
// works
// var breakthings = process.stdout;
Is iisnode doing something funky to stdin, or have I misconfigured something?
In my case, and since the issue still occurs, I just override the getter from the process.stdin in the iisnode file
var events = require('events');
// Define a custom getter for process.stdin since iisnode still didn't fix the bug
process.__defineGetter__('stdin', function(){
return new events.EventEmitter();
})
// no kaboom anymore ;)
var breakthings = process.stdin;
hope this help ;)
UPDATE (02-06-2016):
In a more advisable and clean way:
var events = require('events');
delete process.stdin;
process.stdin = new events.EventEmitter();
// no kaboom anymore ;)
var breakthings = process.stdin;
Actionhero now has a check for this kind of thing https://github.com/evantahler/actionhero/commit/465a85fba9800466ab9ca3d5df65a18f7decd830

Error: Cannot find module 'connect/middleware/session/memory'

I have done npm install connect but Error: Cannot find module 'connect/middleware/session/memory' did not disappear in output. Which package didn't I install?
windows 7
C:\Users\guo\Desktop\js8_chat\chat>node -v
v0.10.26
C:\Users\guo\Desktop\js8_chat\chat>npm -v
1.4.3
express#3.5.1
var express = require('express'),
sio = require('socket.io'),
fs=require('fs'),
path = require('path')
url = require('url'),
parseCookie = require('connect').utils.parseCookie,
MemoryStore = require('connect/middleware/session/memory');
error:
module.js:340
throw err;
^
Error: Cannot find module 'connect/middleware/session/memory'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (C:\Users\guo\Desktop\js8_chat\chat\chat.js:11:16)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
Thanks.
If you're trying to create a MemoryStore and cookieParser then you can create it like this:
var EXPRESS_SID_KEY = 'express.sid';
var COOKIE_SECRET = 'very secret string';
var cookieParser = express.cookieParser(COOKIE_SECRET);
var sessionStore = new express.session.MemoryStore();
app.configure(function () {
app.use(cookieParser);
app.use(express.session({
store: sessionStore,
cookie: {
httpOnly: true
},
key: EXPRESS_SID_KEY
}));
});
And what you're trying to get above exists in express module and you have to look it as
var MemoryStore = require('express\node_modules\connect\lib\middleware\session\memory')

exception when trying to set broadcast flag

I'm working on a dhcp server/client and wanted to try stuff in my home network.
I want to send a DHCPDISCOVER message via udp into my network and catch the answer from my router. Here is my code:
var sender = dgram.createSocket('udp4');
sender.bind();
sender.on('error', function(err) {
console.log(err);
});
sender.setBroadcast(true);
var listener = dgram.createSocket('udp4');
listener.bind(68, function() {
console.log('BOUND');
});
listener.on('message', function(msg, rinfo) {
console.log(msg);
});
sender.send(message, 0, message.length, 67, "255.255.255.255");
But when I try to set the broadcast flag my application crashes.
sudo node main
dgram.js:345
throw errnoException(process._errno, 'setBroadcast');
^
Error: setBroadcast EBADF
at errnoException (dgram.js:454:11)
at Socket.setBroadcast (dgram.js:345:11)
at Object.<anonymous> (/Volumes/Projects/node-dhcp-server/node_modules/module-dhcp/main.js:31:8)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:902:3
felix#felixs-macbook-pro:/Volumes/Projects/node-dhcp-server/node_modules/module-dhcp%
I have no idea why it crashes...
Hmm ok this was an obvious fault ...
Of course the bind() method is asynchronous and I have to set the flag in the callback.
sender.bind( function() { sender.setBroadcast(true) } );

Resources