I'm on Windows 10 TP build 9926, using nodejs. I want to be able to import a Javascript into a current session of nodejs that is running on Windows command prompt so that functions from the imported script can be called from within the REPL. How can I achieve that? My attempt of "import" and "require" did not succeed.
I tried the following in a nodejs session running from the directory that has the "learn.js" javascript;
var n = require("learn.js")
Then got the following error message:
Error: Cannot find module 'learn.js'
at Function.Module._resolveFilename (module.js:336:15)
at Function.Module._load (module.js:278:25)
at Module.require (module.js:365:17)
at require (module.js:384:17)
at repl:1:9
at REPLServer.defaultEval (repl.js:132:27)
at bound (domain.js:254:14)
at REPLServer.runBound [as eval] (domain.js:267:12)
at REPLServer.<anonymous> (repl.js:279:12)
at REPLServer.emit (events.js:107:17)
The learn.js file needs to look like the following:
"use strict";
module.exports.myfunc1 = function() {
console.log("Hi there.");
}
module.exports.myfunc2 = function() {
console.log("Goodbye.");
}
Also, you must require it like this:
var n = require('./learn.js');
or
var n = require('./learn');
You must make the path relative to where you are running the REPL. Node won't automatically check the local directory.
Related
I have a file for my Blockchain called gale.js, and I need the Blockchain variable to get into my miner.js file:
let galeCoin = new Blockchain(); // My Blockchain
Can I access this variable from my miner.js file so I can run the minePendingTransactions() function from the Blockchain class
var galeCoin = /* the coin imported from the gale.js file */
var minerAdress = "0xOTUYboIUYEO5C274OUIYGo8isYOCYW6o87TO"; // SHA256 Hash for the miner
while (true) {
minePendingTransactions(minerAdress); // mine (forever)
}
I would also like to access this variable in my user.js file:
var galeCoin = /* the coin imported from the gale.js file */
var userAdress = galeCoin.newAdress(); // A new adress for the user
// I will add the code here later...
And many other files!
But I can't seen to access the variable from the other files!
I tried this:
let galeCoin = new Blockchain();
export { galeCoin };
import { galeCoin } from './gale.js'
but that gives me this error when I run miner.js using node miner.js:
node:internal/modules/cjs/loader:936
throw err;
^
Error: Cannot find module 'gale.js'
Require stack:
- /Users/Movies/Desktop/devProjects/Blockchain/SecondTry/miner.js
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
at Function.Module._load (node:internal/modules/cjs/loader:778:27)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.<anonymous> (/Users/Movies/Desktop/devProjects/Blockchain/SecondTry/miner.js:1:1)
at Module._compile (node:internal/modules/cjs/loader:1101:14)
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:81:12) {
code: 'MODULE_NOT_FOUND',
requireStack: [ '/Users/Movies/Desktop/devProjects/Blockchain/SecondTry/miner.js' ]
}
I dont understand the error message
I need help as soon as posible!
NOTE: My files are in /Users/Movies/Desktop/devProjects/Blockchain/SecondTry/
Try using the following to import galeCoin from gale.js:
var {galeCoin} = require('./gale.js');
You'll also need to export the galeCoin object from gale.js. You can use the following (append somewhere at the end of gale.js):
module.exports = {galeCoin};
Then you're going to want to run npm install gale.js to ensure that module is added to the node_modules directory. For this you're going to want to make sure the node_modules is in the same director as miner.js. If it is not, npm -g install gale.js.
I should note, I'm assuming you're using node.js, which from the error you threw, seems to be the case.
I hope this was helpful:)
Try :
export let galeCoin = new Blockchain();
//add export statement at the beginning of the line where you declare your variable
You can read about it here: https://javascript.info/import-export
I am just learning Unit.js with mocha. I am following the steps at: http://unitjs.com/guide/quickstart.html . I have installed mocha and am running the "example.js" script. The script fails with
$ mocha example.js
0 passing (1ms)
Running the example.js in with -full-trace I get:
$ mocha example.js -full-trace
C:\Users\rball\AppData\Roaming\npm\node_modules\mocha\lib\utils.js:652
throw new Error("cannot resolve path (or pattern) '" + path + "'");
^
Error: cannot resolve path (or pattern) '-t'
at Object.lookupFiles (C:\Users\rball\AppData\Roaming\npm\node_modules\mocha\lib\utils.js:652:15)
at C:\Users\rball\AppData\Roaming\npm\node_modules\mocha\bin\_mocha:326:30
at Array.forEach (native)
at Object.<anonymous> (C:\Users\rball\AppData\Roaming\npm\node_modules\mocha\bin\_mocha:325:6)
at Module._compile (module.js:435:26)
at Object.Module._extensions..js (module.js:442:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:311:12)
at Function.Module.runMain (module.js:467:10)
at startup (node.js:134:18)
at node.js:961:3
I am running this with cygwin64. and running in the directory: /home/rball/test
the example.js is:
// load Unit.js module
var test = require('../node_modules/unit.js');
// just for example of tested value
var example = 'hello';
// assert that example variable is a string
test.string(example);
// or with Must.js
test.must(example).be.a.string();
// or with assert
The unit.js is in the node_modules directory.
The error in the full trace output is baffling since I am not aware of any path with a "-t" in it.
What am I missing?
Thanks for your help
I think you need an extra '-' in front of '-full-trace'.
Example: '--full-trace'
You set your asserter with 'test.string(example)'. You've already passed it the specified variable type, which would be fine if you were validating the string value, length, etc.
Try this instead:
'test.value(example).isString();'
----or----
'test.value(example).must.be.a.string();'
http://unitjs.com/api/value.html#isString
I have downloaded jsfeat from here, and unzipped the downloaded file.
Inside the directory, I wrote the following:
require("./build/jsfeat.js");
var matrix = new jsfeat.matrix_t(2,2, jsfeat.U8_t | jsfeat.C1_t);
matrix.data[1] = 4;
console.log(matrix.data[1]);
I ran the program as follows:
$ node matrix.js
But, got the following error:
/Users/abc/Desktop/JavaScript/inspirit-jsfeat-59cc928/matrix.js:2
var matrix = new jsfeat.matrix_t(2,2, jsfeat.U8_t | jsfeat.C1_t);
^
ReferenceError: jsfeat is not defined
at Object.<anonymous> (/Users/abc/Desktop/JavaScript/inspirit-jsfeat-59cc928/matrix.js:2:19)
at Module._compile (module.js:425:26)
at Object.Module._extensions..js (module.js:432:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:311:12)
at Function.Module.runMain (module.js:457:10)
at startup (node.js:136:18)
at node.js:972:3
Why is that? How can I solve the issue?
Thanks.
You need to assign the imported module to a variable:
var jsfeat = require("./build/jsfeat.js");
If you prefer you could install the module with NPM to save downloading it manually:
$ npm install jsfeat
The module will be saved to ./node_modules/jsfeat. You can then require it with var jsfeat = require('jsfeat').
I have the following tiny program.js which tries to execute a binary file:
var childProcess = require('child_process');
var path2Binary = '/home/myuser/myproj/bins/mybin';
var par = '--file=' + '/home/myuser/myproj/files/myfile.txt';
var ret = childProcess.execFileSync(path2Binary, [par]);
if (!ret) throw 'Error invoking process!';
var cnt = ret.stdout;
if (!cnt) throw 'Error retrieving output!';
console.log(cnt);
The program tries to execute a binary file and passes it a parameter (a file). The output of this process will be then displayed.
I try to run this: node program.js, but get the following
var ret = childProcess.execFileSync(path2Binary, [par]);
^
TypeError: Object #<Object> has no method 'execFileSync'
at Object.<anonymous> (/home/myuser/myproj/program.js:6:24)
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:929:3
More information
I am running on CentOS, Node version is v0.10.36.
I tried running sudo yum install nodejs, but it tells me it is already installed so Node installation looks kinda good.
What's the problem?
On a side note...
If I replace childProcess.execFileSync with childProcess.spawn I get the same.
If I change the first line into the following:
var exec = require('child_process').execFileSync;
Then I get an undefined exception on exec.
Synchronous child processes aren't supported in node v0.10.36 - https://nodejs.org/docs/v0.10.36/api/child_process.html
Looks like it may have been introduced in 0.12.
node is crashing at the following line:
var tcp = require('tcp'),
error text:
node.js:201
throw e; // process.nextTick error, or 'error' event on first tick
^
Error: Cannot find module 'tcp'
at Function._resolveFilename (module.js:334:11)
at Function._load (module.js:279:25)
at Module.require (module.js:357:17)
at require (module.js:368:17)
at Object.<anonymous> (C:\Program Files\nodejs\websocket\websocket.js:11:11)
at Module._compile (module.js:432:26)
at Object..js (module.js:450:10)
at Module.load (module.js:351:31)
at Function._load (module.js:310:12)
at Module.require (module.js:357:17)
What is the problem? I found the source on the Internet, and the author, and the visitors also can run it...
Try require('net') instead:
$ node
> var tcp = require('tcp');
The 'tcp' module is now called 'net'. Otherwise it should have a similar interface.
> var tcp = require('net');
> $
Others are able to run, may be because they are using Node module when 'tcp' was there...
Now its called 'net', but its all the same thing no need of checking..
If you want to cross check for your more information, here are the links:
1.http://nodejs.org/api/net.html
2.https://github.com/joyent/node/blob/master/lib/net.js