Using node express with node versions 10 and above fails - node.js

I can see many posts on this problem, but no one answers the simple question.
I want to type
express myNewProject
on a system running node V12.16.2
All the replies get into WHY it fails, such as "fs.writeFile(...) requires a third (or fourth) parameter"
Am I really forced to not use express any more? Is there some fix I can apply to my system and make this work?
here is the session:
bcuser#autumn14:~$ express myNewProject
create : myNewProject
install dependencies:
$ cd myNewProject && npm install
run the app:
$ DEBUG=myNewProject ./bin/www
fs.js:144
throw new ERR_INVALID_CALLBACK(cb);
^
TypeError [ERR_INVALID_CALLBACK]: Callback must be a function. Received { mode: 438 }
at maybeCallback (fs.js:144:9)
at Object.writeFile (fs.js:1252:14)
at write (/usr/lib/node_modules/express-generator/bin/express:246:6)
at /usr/lib/node_modules/express-generator/bin/express:210:5
at /usr/lib/node_modules/express-generator/bin/express:261:11
at /usr/lib/node_modules/express-generator/node_modules/mkdirp/index.js:29:20
at FSReqCallback.oncomplete (fs.js:155:23) {
code: 'ERR_INVALID_CALLBACK'
}

This error looks like you need to update either express or express-generator to the latest version. You're on Express 4.9.0 and the latest is 4.17.1. The error stack looks like it's referring to express-generator, but perhaps it's a version of Express embedded into the express-generator install. In either case, I think you need a newer version.
As for background, starting in node.js v10, node started requiring asynchronous callbacks on a number of asynchronous functions such as fs.write() and fs.unlink(). So, any code that was kind of doing fire and forget calling of those functions without paying any attention to the completion or error (by not providing a callback function) would generate this ERR_INVALID_CALLBACK error.

Related

NodeJS unexpected strict mode reserved word "let" when installing ejs (not my code)

I'm getting this output when trying to install a node application using npm install:
$ npm install
> ejs#2.7.4 postinstall /home/dh_8u42k7/quotegoodeair.com/node_modules/ejs
> node ./postinstall.js
/home/dh_8u42k7/quotegoodeair.com/node_modules/ejs/postinstall.js:9
let envDisable = isTrue(process.env.DISABLE_OPENCOLLECTIVE) || isTrue(process.
^^^
SyntaxError: Unexpected strict mode reserved word
I have already found this question which contains a workaround. However, this isn't my own code, so I can't just remove "let", it seems to be a problem with ejs itself.
For another thing, the file postinstall.js doesn't even exist, even the ejs folder doesn't exist, so I can't just go in and edit the file.
Is there something wrong with my node application, or with ejs, or maybe with npm or Node itself? Why does Node install components which aren't supported by Node? Why does Node tell me there's an error in a file which doesn't exist?
It may be that your version of node does not support the features in the application you are trying to install.
One solution may be to find out what the version of node the application that you are trying to install originally used.
I believe you can check the engine field in your package.json file.
If you local node version is different, and you need to change it, here is a link to a question providing that information: How can I specify the required Node.js version in package.json?
I also found this post interesting: https://github.com/tj/n/issues/472
It's for a different application but they received the same error message. The issue was conflicting npm folders. It's possible that the program that you are trying to install is conflicting with your existing npm setup if you have other nom folders in the directory you are installing the application in.
Ultimately, given the information you provided, I think your best bet it to look in the package.json file of the application you're trying to install and see if your local setup is conflicting with how the program is trying to be installed or run.

ReferenceError: WebAssembly is not defined

I'm using ttest nodejs module in application. Due to updates to it I tried to use updated version ttest#2.0.0. When module start compute data the error appears:
ReferenceError: WebAssembly is not defined
The NodeJS version - 6.9.1.
Error appears in next place:
class CephesWrapper {
constructor(sync) {
// Initialize the runtime's memory
this._wasmMemory = new WebAssembly.Memory({
'initial': TOTAL_MEMORY / WASM_PAGE_SIZE,
'maximum': TOTAL_MEMORY / WASM_PAGE_SIZE
});
.....
}
}
Is there any advices about solving this issue? Is it possible to define WebAssembly?
The WebAssembly support is added in NodeJs 8.0.0. Update your NodeJS to make it work.
In my case, the problem was that I was running my browser in safe mode (Mozilla Firefox).
Simply shutdown your browser completely and restart. This will quit safe mode (If stuck in safe mode, see here)
For reference, I had found this solution here : https://github.com/mozilla-iot/gateway/issues/2047
Even my node version was 12, and npm version was 6. I was getting the reference error for webassembly.
So I updated the node with latest version(16), it changed the npm version also. Now the reference issue is fixed.
D:\ReactJS\react_demo>node -v
v16.14.0
D:\ReactJS\react_demo>npm -v
8.3.1

Enabling implicit type checking for Javascript in VS Code results in "Cannot find name 'require'" error

I made a very simple Node project (npm init, accept all defaults) and opened it in VS Code. I followed the instructions in the docs to turn on global implicit type checking: set javascript.implicitProjectConfig.checkJs to true in user prefs.
Now, when I try to use require() in the code, I get a red squiggly underline with the error [js] Cannot find name 'require'. Do I need to do something to tell the Typescript server that this will run in a Node environment, and thus require will be provided as a global?
ETA: I'm not totally clear on the disconnect between whatever is generating the "Cannot find name" error, and whatever is creating Intellisense suggestions. In spite of the require() error, Intellisense works correctly. If I npm install --save moment, then write
const moment = require("moment");
moment.
then I get Intellisense completion of Moment functions, even though require has a red squiggly line under it.
You may have to install node typing via
npm install --save-dev #types/node
This will install node types locally.

Meteor application hangs up after creating bundle

I am trying to run meteor app after deploying but it hangs up and never responds I have already set all the env varibales. Please help
node main.js
Note: you are using a pure-JavaScript implementation of bcrypt.
While this implementation will work correctly, it is known to be
approximately three times slower than the native implementation.
In order to use the native implementation instead, run
meteor npm install --save bcrypt
in the root directory of your application.
... no response

How to include dependencies in J2V8

How to include dependencies in J2V8? I would like to use certain dependencies in the javascript file for instance the crypto package.
var crypto = require('crypto');
function foo(){ return crypto.createHash('md5').update('Apple').digest("hex");}
However, I got the following error saying require keyword is undefined.
undefined:1: ReferenceError: require is not defined
var crypto = require('crypto');
^
ReferenceError: require is not defined at <anonymous>:1:14
com.eclipsesource.v8.V8ScriptExecutionException
at com.eclipsesource.v8.V8._executeVoidScript(Native Method)
Can anyone tell me how to import an package into J2V8?
Unless you're working with Node, require is not a feature. Usually, you want to use a bundler like webpack to pack your structured source code into one large file so that it can be understood by browsers. This way you can use require and npm packages for your frontend code, which makes development easier, and a bundler turns it with every build (or live update) into a different format, that's hard to read for humans, but is valid Javascript.
I have had success using node modules in J2v8, please check out this blog :http://eclipsesource.com/blogs/2016/07/20/running-node-js-on-the-jvm/
NodeJs nodeJS = NodeJs.createNodeJs();
After registering callbacks
nodeJs.exec(File thescripttoexecute)
Make sure you have the proper path to the node modules in the require() command.
You may have to make a nodejs package that takes dependencies and exports what you need. Then, You have to execute npm install manually.
or You can just npm install what-you-need.
Create Node JS Runtime, and use require with your your-package-dir/index.js or exact location of module that you need. like this:
V8Object module = nvm.require(file);
Now you can call the function likes:
module.executeJSFunction("test");
To deliver entire dependencies you have to bundlize module directory your self.
What if you have to support cross-platform, refer https://www.npmjs.com/package/node-pre-gyp also.

Resources