Error trying to run a javascript program from the terminal - javascript

I'm trying to run a javascript file prompt.js from the terminal but I'm getting this error.
$ node prompt.js
internal/modules/cjs/loader.js:583
throw err;
^
Error: Cannot find module 'prompt'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:581:15)
at Function.Module._load (internal/modules/cjs/loader.js:507:25)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:22:18)
at Object.<anonymous> (/Users/pathwang/Documents/School/Spring2019/CSC404/CSC404_MyHomework/Patrick_Hwang_CSC404_Hw1/prompt.js:1:77)
at Module._compile (internal/modules/cjs/loader.js:689:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
This is what is in my prompt.js file:
var prompt = require('prompt');
var schema = {
properties: {
name: {
pattern: /^[a-zA-Z\s\-]+$/,
message: 'Name must be only letters, spaces, or dashes',
required: true
},
email: {
},
exam1: { type: 'number'},
exam2: { type: 'number'},
exam3: { type: 'number'}
}
};
var student = {
name: '',
id: '',
exam1: 60,
exam2: 60,
exam3: 60,
at1: true,
at2: true,
at3: true,
at4: true,
at5: true,
adjust: true,
final: 100,
letter: ' '
};
//
// Get two properties from the user: username and email
//
prompt.get(schema, function (err, result) {
//
// Log the results.
//
console.log('Command-line input received:');
console.log(' username: ' + result.username);
console.log(' email: ' + result.email);
console.log(' exam1:', result.exam1);
console.log(' exam2:', result.exam2);
console.log(' exam3:', result.exam3);
var totalExam;
totalExam = (1/3) * (result.exam1 +
result.exam2 +
result.exam3);
console.log(' Average exam =', totalExam )
});
//
// Start the prompt
//
prompt.start();
I'm in the right directory and I can run other files in the project folder by using $ node test.js. I also can open prompt.js file from the terminal in a different project folder where I copied the source code from. I don't know why this is happening? I would appreciate the help thanks.

The line var prompt = require('prompt'); causing the error. The error is saying it can't find said module. Or the module is not part of the project it doesn't exist. The solution is to find the module called prompt and add the directory into the project folder. This will solve the problem.

Related

SyntaxError: Unexpected token

No changes made to lang.js file since months ago but suddenly syntax error when trying to preview locally and can't deploy changes.
Not sure why this is happening - could somebody please kindly help? Many thanks!
Disclosure/warning: I have 0 programming knowledge.
MacBook:pty-bus Ying$ node index.js
/Users/Ying/Desktop/pty-bus/lang.js:19
...req.lang.marketing.global,
^^^
SyntaxError: Unexpected token ...
at Object.exports.runInThisContext (vm.js:78:16)
at Module._compile (module.js:543:28)
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.require (module.js:498:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/Users/Ying/Desktop/pty-bus/index.js:20:36)
at Module._compile (module.js:571:32)
lang.js
var path = require('path');
function langMiddleware(req, res, next){
let lang = req.query.lang || 'en';
if(['es', 'en'].indexOf(lang) == -1) lang = 'en'
fs.readFile(path.join(__dirname, 'lang', lang + '.json'), 'utf8', function(err, data){
req.lang = JSON.parse(data);
req.langCode = lang;
next();
})
};
function render(req, res, template, vars={}){
let renderObj = {
...req.lang.marketing.global,
title: req.lang.titles[template],
site_title: req.lang.site_title,
site_description: req.lang.site_description,
...(req.lang.marketing[template] || {}),
lang: {...(req.lang.marketing.global.language || {})},
langCode: req.langCode,
...vars,
path: req.path
};
res.render(template, renderObj);
}
module.exports = { langMiddleware, render }
It looks like your ability to use spread operator has been removed. I would guess you have recently changed your node version, or edited either your babelrc or webpack config. Either way, I don't think it's anything wrong with your current code in that file.
Node.js v7.4.0 has V8 5.4 which supports ...operator for arrays, but not for objects. You need at least V8 6.0, i.e. the last Node.js 8 branch version will do (v8.15.0).
Tt looks like node v7.4.0 not support spread operator - change let renderObj = {...} to
let renderObj = {
title: req.lang.titles[template],
site_title: req.lang.site_title,
site_description: req.lang.site_description,
langCode: req.langCode,
path: req.path
};
Object.assign(renderObj,
req.lang.marketing.global,
(req.lang.marketing[template] || {}),
vars
);
renderObj.lang = Object.assign({}, (req.lang.marketing.global.language || {}));

NFC Error in nodejs

I was about to make a program that reads NFC cards via NFC reader.
when I execute my program i get the following errros...
/home/dotmark/Desktop/nfc2/node_modules/bindings/bindings.js:83
throw e
^
Error: libnfc.so.5: cannot open shared object file: No such file or directory
at Object.Module._extensions..node (module.js:598:18)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.require (module.js:498:17)
at require (internal/module.js:20:19)
at bindings (/home/dotmark/Desktop/nfc2/node_modules/bindings/bindings.js:76:44)
at Object.<anonymous> (/home/dotmark/Desktop/nfc2/node_modules/nfc/index.js:1:95)
at Module._compile (module.js:571:32)
at Object.Module._extensions..js (module.js:580:10)
My script for loading nfc libraries is as follows...
var nfc = require('nfc').nfc
, util = require('util')
;
console.log('nfc.version(): ' + util.inspect(nfc.version(), { depth: null }));
// { name: 'libfnc', version: '1.7.0' }
console.log('nfc.scan(): ' + util.inspect(nfc.scan(), { depth: null }));
Any help would be really great..

Plugins for acorn loose_parser?

This code snippet seems not work, why?
var acorn = require("./node_modules/acorn/dist/acorn_loose");
/* Extend default Acorn's methods.*/
acorn.pluginsLoose.testPlug = function(looseParser) {
looseParser.extend('finishNode', function(nextMethod) {
return function(node, type) {
console.log(node, type);
return nextMethod.call(this, node, type);
};
});
};
var res = acorn.parse_dammit("alert(1", {ecmaVersion: 6, plugins: {testPlug: true}});
console.log(res)
Give us next error:
...\node_modules\acorn\dist\acorn.js:508
if (!plugin) throw new Error("Plugin '" + name + "' not found")
^
Error: Plugin 'testPlug' not found
at Parser.loadPlugins (...\node_modules\acorn\dist\acorn.js:508:26)
at new Parser (...\node_modules\acorn\dist\acorn.js:444:10)
at Object.tokenizer (...\node_modules\acorn\dist\acorn.js:3115:12)
at new LooseParser (...\node_modules\acorn\dist\acorn_loose.js:15:23)
at Object.parse_dammit (...\node_modules\acorn\dist\acorn_loose.js:1258:13)
at Object.<anonymous> (...\plugins_poc.js:16:17)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
Is it bug or I do something wrong?
Enviroment: idea intellij 16.
Update:
Chrome give the similar error.
var res = acorn.parse_dammit("alert(1", {ecmaVersion: 6, plugins: {testPlug: true}});
You need to use pluginsLoose instead of plugins here as well!

How to perform jsonschemavalidation through http?

I have defined a restservice and trying to do a jsonschema validation with paperwork:
var _ = require('underscore');
var validate = require('isvalid-express');
var paperwork=require('paperwork');
module.exports = function (app) {
app.post('/myroute', paperwork({
username: /[a-z0-9]+/,
password: String,
age: Number,
interests: [String],
jobs: [{
company: String,
role: String
}]
}, function (req, res) {
// ...
}));
};
This is the request I post this in postman to myroute:
{
username: 'brucewayne',
password: 'iambatman',
age: 36,
interests: ['Climbing', 'CQC', 'Cosplay'],
jobs: [{
company: 'Wayne Inc.',
role: 'CEO'
}]
}
However it throws an error:
TypeError: undefined is not a function
at module.exports (c:\heroku\newher\node_codechallenge\node_modules\paperwork\paperwork.js:129:5)
at module.exports (c:\heroku\newher\node_codechallenge\app\testroutes.js:12:26)
at Object.<anonymous> (c:\heroku\newher\node_codechallenge\test.js:16:31)
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 Module.runMain [as _onTimeout] (module.js:497:10)
at Timer.listOnTimeout [as ontimeout] (timers.js:112:15)
What is the best way of doing a json schemavalidation for http in node.js?
You may consider using JSON-schema standard rather than non-standard schema definition that paperwork uses.
There are many JavaScript JSON Schema validators, here is the benchmark: https://github.com/ebdrup/json-schema-benchmark

Node mustache rendering server side

I'm trying to create a simple application to compile mustache templates into static pages server side, here's what I've got so far:
var view = {
title: "Joe",
calc: function () {
return 2+4;
}
};
var mustache = require("mustache");
var template = require("./home.template");
var output = mustache.to_html(template, view);
console.log(output);
And my template looks like:
{{title}} spend {{calc}}
Any suggestions as to what is causing this to fail?
Here is the complete error message:
home.template:1
} spend {{calc}}
^
module.js:437
var compiledWrapper = runInThisContext(wrapper, filename, true);
^
SyntaxError: Unexpected token {
at Module._compile (module.js:437:25)
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> (/Users/MorehouseJ09/Documents/production_development/mustache/current/compiler.js:12:16)
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)
Any help would be great!
Use fs.readFile() to read your template in as a string. Require won't work unless it's requiring javascript code, not mustache code.
http://nodejs.org/api/fs.html#fs_fs_readfile_filename_encoding_callback
Edit
See if this works...
var mustache = require("mustache");
var fs = require("fs");
var view = {
title: "Joe",
calc: function () {
return 2+4;
}
};
fs.readFile('./home.template', 'utf-8', function (err, data) {
if (err) throw err;
var output = mustache.to_html(data, view);
console.log(output);
});

Categories