Run node.js with different file extension - javascript

Is is possible to run node.js with a different extension than .js like: node server.type instead of node server.js?
I have tried require.extensions
require.extensions['.type'] = require.extensions['.js'];
but I get this error:
Error: /root/project/server.type: invalid ELF header
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:901:3
However I think this has nothing to do with require unless I want to require these files from within node.js.

Works just fine:
$ echo 'console.log("hello world")' > server.type
$ node server.type
hello world
But I'm guessing that you want to run it as an executable. In that case, start your script with:
#!/usr/bin/env node
# followed by your actual script:
console.log("hello world");
And make your script executable:
chmod 755 server.type
After that:
$ /root/project/server.type
hello world

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 to build python with node-gyp?

I've seen the official tutorial as well as this one showing how to build python from source and edit it with visual studio etc., but how do I build it with node-gyp to use it as a native application in node?
For example, in the tutorial, they tell you to download the source code, and then (in windows) type in that directory:
"PCbuild/build.bat"
and then that builds the Visual Studio solution and python in general etc., but now how do I do that with node-gyp? In general, to include external dependencies to build with node-gyp, you just include them in the binding.gyp file. For python, I first installed 64 bit (and later 32 bit, same error as below) python to C:/Python382, then I copied that folder to my another folder in my C++ application, and set the binding.gyp file to this (to get the include and libs):
{
"targets": [
{
"target_name": "addon",
"sources": [
"<!#(node -p \"var fs=require('fs'),path=require('path'),walk=function(r){let t,e=[],n=null;try{t=fs.readdirSync(r)}catch(r){n=r.toString()}if(n)return n;var a=0;return function n(){var i=t[a++];if(!i)return e;let u=path.resolve(r,i);i=r+'/'+i;let c=fs.statSync(u);if(c&&c.isDirectory()){let r=walk(i);return e=e.concat(r),n()}return e.push(i),n()}()};walk('./sources').join(' ');\")"
],
"libraries":[
"C:/Users/Coby/Documents/aa/atzmus/CPPtesting/other/ok/Python382/libs/python38.lib",
"C:/Users/Coby/Documents/aa/atzmus/CPPtesting/other/ok/Python382/libs/python3.lib",
"C:/Users/Coby/Documents/aa/atzmus/CPPtesting/other/ok/Python382/libs/_tkinter.lib"
],
"include_dirs": [
"C:/Users/Coby/Documents/aa/atzmus/CPPtesting/other/ok/Python382/include"
],
"dll_files": [
"C:/Users/Coby/Documents/aa/atzmus/CPPtesting/other/ok/Python382/python38.dll"
]
}
]
}
and my hello.cc file, which is the other source, simply includes Python.h. When I run this build using node-gyp build or node-gyp rebuild, it actually builds fine with no errors at all, but then when I copy over the file addon.node to my actual nodeJS server, which simply has the one line var addon = require("./addon"), I get the following error output in CMD (however, it works fine with no errors without including Python.h in my .cc source file):
internal/modules/cjs/loader.js:1197
return process.dlopen(module, path.toNamespacedPath(filename));
^
Error: \\?\C:\Users\Coby\Documents\aa\atzmus\testServer\addon.node is not a valid Win32 application.
\\?\C:\Users\Coby\Documents\aa\atzmus\testServer\addon.node
←[90m at Object.Module._extensions..node (internal/modules/cjs/loader.js:1197
:18)←[39m
←[90m at Module.load (internal/modules/cjs/loader.js:983:32)←[39m
←[90m at Function.Module._load (internal/modules/cjs/loader.js:891:14)←[39m
←[90m at Module.require (internal/modules/cjs/loader.js:1023:19)←[39m
←[90m at require (internal/modules/cjs/helpers.js:72:18)←[39m
at Object.<anonymous> (C:\Users\Coby\Documents\aa\atzmus\testServer\oy.js:2:
9)
←[90m at Module._compile (internal/modules/cjs/loader.js:1128:30)←[39m
←[90m at Object.Module._extensions..js (internal/modules/cjs/loader.js:1167:1
0)←[39m
←[90m at Module.load (internal/modules/cjs/loader.js:983:32)←[39m
←[90m at Function.Module._load (internal/modules/cjs/loader.js:891:14)←[39m
The main part seems to be:
addon.node is not a valid Win32 application.
I tried doing this with python for 64bit windows, and for 32bit windows, my system in 64 bit, my nodeJS installation is 64 bit, but I also tried it with 32 bit note; I don't know how to fix this exactly, or if there is another way entirely to build python with node-gyp?
so:
What steps need to be taken in windows to build python through node-gyp, to be able to use python natively with nodejs without having to resort to child-process?

Grave (`) is marked as illegal token

I've been trying to transfer a node program I created locally to an EC2 server via Cloud9, and I have already manually replaced all of the (req,res) => {} statements because it didn't accept that. However, now it's giving me
console.log(`foo ${bar}`)
^
SyntaxError: Unexpected token ILLEGAL
at Module._compile (module.js:439:25)
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:945:3
It works fine on my local server and it runs fine here too, even without nodejs.
var bar = "bar"
console.log(`foo ${bar}`)
The version of node is v13.9.0 and it was installed via nvp.
Anyone know why this is happening? I really don't want to go through and convert all the strings with `
Thanks for the help, I figured out that cloud9 automatically installs nodejs, just not the right version. It installs 10.19.0, while the current latest version is 13.10.0. I installed the latest version, but the default was still 10.19.0. For anyone else having this problem, do
nvm ls
to list your installed node versions, then use
nvm alias default 13.10.0 (replace with whatever version you want)

I get a SyntaxError in Node.JS, but I can't figure out the origin of that error from the log messages

I am running a Node app on Azure. It's a Web API and when I try to call it, I see this message in the Node console:
Application has thrown an uncaught exception and is terminated:
SyntaxError: Unexpected token (
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:414:25)
at Object.Module._extensions..js (module.js:442:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:313:12)
at Module.require (module.js:366:17)
at require (module.js:385:17)
at Object.<anonymous> (D:\home\site\wwwroot\app.js:17:23)
at Module._compile (module.js:435:26)
at Object.Module._extensions..js (module.js:442:10)
I ran my whole code through eslint and didn't find any syntax errors. It also runs fine on my local machine, but doesn't run on Azure.
The message above points to line 17 and column 23 in app.js, which is a require:
let warehouseExport = require('./routes/warehouse-export-api');
I ran eslint and jshint specifically on the file referenced above, and there are no syntax errors.
What are some other ways I can get the exact location of the syntax error, and why could it be that the code fails on Azure but runs on my local machine?
What I tried:
Wrap in try-catch and grab the line number - it shows up undefined:
try {
var warehouseExport = require('./routes/warehouse-export-api');
} catch (error) {
console.log(error + ', line ' + error.lineNumber);
}
You should check version of nodejs you have installed on Azure. Maybe it's <= 4 and it doesn't support let keyword. Just a wild guess.
The cause of the error was that the Node version somehow got reset to 4.2.4.
When I go to Azure Portal and run node --version, I get version 8.1.4.
However, if I console.log the Node version inside my script, it is somehow still 4.2.4.
I will investigate why that happened and how to fix it, but for now it's clear that the bug is due to async function not being recognized by the old Node.
Update: the package.json file where the node version was specified got overwritten somehow, which is what caused the issue.

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

Categories