Reading from an external file in node.js - javascript

I've just started using node.js, and I have been looking for ways to count the number of lines in a cpp file stored on my machine (in a different directory from that of the node.js app).
I am trying just read text from a cpp file stored inside the node.js project for now, with this function:
console.log(fs.readFileSync('code.cpp', 'utf8'));
but I get this error:
fs.js:646 return binding.open(pathModule._makeLong(path),
stringToFlags(flags), mode);
^
Error: ENOENT: no such file or directory, open
'C:\Users\Heba\WebstormProjects\wrfile\sever.cpp'
at Object.fs.openSync (fs.js:646:18)
at Object.fs.readFileSync (fs.js:551:33)
at Object. (C:\Users\Heba\WebstormProjects\wrfile\app.js:5:16)
at Module._compile (module.js:643:30)
at Object.Module._extensions..js (module.js:654:10)
at Module.load (module.js:556:32)
at tryModuleLoad (module.js:499:12)
at Function.Module._load (module.js:491:3)
at Module.require (module.js:587:17)
at require (internal/module.js:11:18)
Process finished with exit code 1
Is this the right approach to the problem? if so how can I fix this error?
Thanks in advance.

according to the error that you get , your script cant locate the file.
make sur that 'code.cpp' is in the same location as your script
try using path.join(__dirname, 'code.cpp');
var fs = require('fs'),
path = require('path'),
file = path.join(__dirname, 'code.cpp');
console.log(fs.readFileSync(file, 'utf8'));

Related

Error while running an exe compiled with PKG

im compiling a file with PKG. But whenever I try to run the file it gives this error.
Error
All versions are up to date.
I'm trying to run my simple javascript code on another computer and this error pops up.
Error: Cannot find module 'C:\snapshot\highu\OneDrive\Masaüstü\XXX\node_modules\sqlite3\lib\binding\napi-v6-win32-unknown-x64\node_sqlite3.node'
Require stack:
- C:\snapshot\highu\OneDrive\Masaüstü\XXX\node_modules\sqlite3\lib\sqlite3-binding.js
- C:\snapshot\highu\OneDrive\Masaüstü\XXX\node_modules\sqlite3\lib\sqlite3.js
- C:\snapshot\highu\OneDrive\Masaüstü\XXX\.\.\.\.\.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 Module._resolveFilename (node:internal/modules/cjs/loader:946:15)
at Function._resolveFilename (pkg/prelude/bootstrap.js:1955:46)
at Module._load (node:internal/modules/cjs/loader:787:27)
at Module.require (node:internal/modules/cjs/loader:1012:19)
at Module.require (pkg/prelude/bootstrap.js:1855:31)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.<anonymous> (C:\snapshot\highu\OneDrive\Masaüstü\XXX\node_modules\sqlite3\lib\sqlite3-binding.js:4:17)
at Module._compile (pkg/prelude/bootstrap.js:1930:22)
at Module._extensions..js (node:internal/modules/cjs/loader:1166:10)
at Module.load (node:internal/modules/cjs/loader:988:32) {
code: 'MODULE_NOT_FOUND',
requireStack: [
'C:\\snapshot\\highu\\OneDrive\\Masaüstü\\XXX\\node_modules\\sqlite3\\lib\\sqlite3-binding.js',
'C:\\snapshot\\highu\\OneDrive\\Masaüstü\\XXX\\node_modules\\sqlite3\\lib\\sqlite3.js',
'C:\\snapshot\\highu\\OneDrive\\Masaüstü\\XXX\\.\\.\\.\\.\\.js'
],
pkg: true
}
Node.js v18.5.0

How can I load NAN libraries with Meteor?

How can I load NAN libraries with Meteor? Currently, I'm using this code:
greenworks = require('./imports/lib/lib/greenworks-osx64');
When I try to load a ".node" file, Meteor errors with this:
Error: Cannot find module './imports/lib/lib/greenworks-osx64'
at Function.Module._resolveFilename (module.js:325:15)
at Function.Module._load (module.js:276:25)
at meteorInstall.imports.lib.greenworks.js (imports/lib/greenworks.js:13:25)
at fileEvaluate (packages/modules-runtime/.npm/package/node_modules/install/install.js:153:1)
at require (packages/modules-runtime/.npm/package/node_modules/install/install.js:82:1)
at meteorInstall.imports.lib.theculling.js (imports/lib/theculling.js:1:18)
at fileEvaluate (packages/modules-runtime/.npm/package/node_modules/install/install.js:153:1)
at Module.require (packages/modules-runtime/.npm/package/node_modules/install/install.js:82:1)
at Module.Mp.import (/Users/jmeyer2k/.meteor/packages/modules/.0.7.5.8vojen++os+web.browser+web.cordova/npm/node_modules/reify/lib/runtime.js:70:16)
at meteorInstall.server.main.js (server/main.js:1:1)
What I've tried already: changing the path to different relative and absolute paths; checking code for references to ".js"

libsass bindings not found. Try reinstalling node-sass?

Im trying to create an offline app following this tutorial here:
http://rafaelquintanilha.com/offline-app-with-meteor-and-cordova/
But whenever I get to the:
meteor run android-device
I get an error:
=> Started proxy.
=> Started MongoDB.
=> Errors prevented startup:
While loading plugin compileScss from package fourseven:scss:
/home/bryam/.meteor/packages/fourseven_scss/.2.1.1.1l0pytj++os.linux.x86_64+web.browser+web.cordova/plugin.compileScss.os.linux.x86_64/npm/node_modules/meteor/compileScss/compileScss/node_modules/node-sass/lib/index.js:22:11:
libsass bindings not found. Try reinstalling node-sass? at
getBinding
(/home/bryam/.meteor/packages/fourseven_scss/.2.1.1.1l0pytj++os.linux.x86_64+web.browser+web.cordova/plugin.compileScss.os.linux.x86_64/npm/node_modules/meteor/compileScss/compileScss/node_modules/node-sass/lib/index.js:22:11)
at Object.
(/home/bryam/.meteor/packages/fourseven_scss/.2.1.1.1l0pytj++os.linux.x86_64+web.browser+web.cordova/plugin.compileScss.os.linux.x86_64/npm/node_modules/meteor/compileScss/compileScss/node_modules/node-sass/lib/index.js:188:23)
at Module._compile (module.js:409:26) at
Object.Module._extensions..js (module.js:416:10) at Module.load
(module.js:343:32) at Module.Mp.load
(/home/bryam/.meteor/packages/meteor-tool/.1.4.0-1.13j49n2++os.linux.x86_64+web.browser+web.cordova/mt-os.linux.x86_64/dev_bundle/lib/node_modules/reify/node/runtime.js:16:23)
at Function.Module._load (module.js:300:12) at Module.require
(module.js:353:17) at require (internal/module.js:12:17) at
Object. (/tools/isobuild/bundler.js:1565:22) at
Object.require (/tools/isobuild/bundler.js:1542:11) at
packages/compileScss/plugin/compile-scss.js:2:1 at
:151:4 at :158:3
=> Your application has errors. Waiting for file change.
I have tried everything that I found on google and I still keep getting the same error, somebody know how to fix this?

nodejs error undefined is not a function

I am trying to create a simple webserver using node js. This is the script that I have written.
var connect = require('connect');
connect.createServer(
connect.static("../angularjs")
).listen(5000);
I place the script in the nodejs directory and then I try to run it using the command "node server.js" and I get the following error
C:\Program Files\nodejs\server.js:4
connect.static("../angularjs")
^
TypeError: undefined is not a function
at Object.<anonymous> (C:\Program Files\nodejs\server.js:4:22)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:501:10)
at startup (node.js:129:16)
at node.js:814:3
I am new to javascript and request you to kindly guide.
Thanks & Regards
Sunil
your version of connect does not contain static in its propertys,
you either have to downgrade your version of connect (2.xx) or try this code snippet
var connect = require('connect'),
serveStatic = require('serve-static');
var app = connect();
app.use(serveStatic("../angularjs"));
app.listen(5000);
but before run
npm install serve-static
Because static is not the property of connect.
According to the phrase
serve-static - previously static
I think you are using a newer version of connect than the one you code is amed to.
So you have two solutions:
Use an older version of connect
Use serve-static middleware instead of the static method

Possible issue with multer? Error: Router.use() requires callback

Building a site with yeoman angular-fullstack works great locally, but when I deploy and use the dist/release version it gives me this fun error on my server.
Error: Router.use() requires callback functions but got a [object Object]
at Function.proto.use (/home/bitnami/htdocs/dist/node_modules/express/lib/router/index.js:327:11)
at Object.<anonymous> (/home/bitnami/htdocs/dist/server/api/save/index.js:10:8)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.<anonymous> (/opt/bitnami/nodejs/lib/node_modules/pm2/node_modules/pmx/lib/transaction.js:62:21)
at Module.require (module.js:365:17)
at require (module.js:384:17)
at module.exports (/home/bitnami/htdocs/dist/server/routes.js:13:25)
It seams that it's failing on this line
router.use(multer({ dest: './public/uploads/'}));
Though it works locally completely fine? Has me a bit stumped.
Wonder if its related to multer? I have the node module installed.
Thoughts?
Recent versions of multer changed the API. If you look at the examples, you will see you now have to do something like:
var upload = multer({ dest: './public/uploads/'});
// ...
router.use(upload.single('foofield'));
Or upload.array() or upload.fields().

Categories