Node.js - Import Package Issue - Not a Function - node.js

Using node v12.9.0 on macOS Mojave, have the following project structure (with app being the root folder):
app
\
utils.js
app.js
utils.js
console.log('utils.js')
const add = function(a ,b) {
return a + b
}
module.exports = add
app.js
const add = require('./utils.js')
const sum = add(4, -2)
console.log(sum)
Tried running this:
node app.js
Error:
app/app.js:3
const sum = add(4, -2)
^
TypeError: add is not a function
at Object.<anonymous> (/Users/devuser/app/app.js:3:13)
at Module._compile (internal/modules/cjs/loader.js:936:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:947:10)
at Module.load (internal/modules/cjs/loader.js:790:32)
at Function.Module._load (internal/modules/cjs/loader.js:703:12)
at Function.Module.runMain (internal/modules/cjs/loader.js:999:10)
at internal/main/run_main_module.js:17:11

Can you try this:
utils.js
const add = function(a ,b) {
return a + b
}
module.exports = {
add : add
}
app.js
const utils = require('./utils.js')
const sum = utils.add(4, -2)
console.log(sum)

It actually works now using my original code inside both the Integrated Terminal that Visual Studio Code provides and the native macOS Terminal.
Now it just prints:
2
Don't know why it was had a TypeError earlier?
Am new to node.js so I can't comment on what changed?

Related

Node.JS, Sequelize: Package exports for 'C:\(my project folder)\node_modules\sequelize\node_modules\uuid' do not define a valid '.' target

I'm getting this error after installing Sequelize library:
Error: Package exports for 'C:\(my project folder)\node_modules\sequelize\node_modules\uuid' do not define a valid '.' target
at resolveExportsTarget (internal/modules/cjs/loader.js:545:13)
at applyExports (internal/modules/cjs/loader.js:459:14)
at resolveExports (internal/modules/cjs/loader.js:508:12)
at Function.Module._findPath (internal/modules/cjs/loader.js:577:20)
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:879:27)
at Function.Module._load (internal/modules/cjs/loader.js:785:27)
at Module.require (internal/modules/cjs/loader.js:956:19)
at require (internal/modules/cjs/helpers.js:74:18)
at Object.<anonymous> (C:\(my project folder)\node_modules\sequelize\lib\utils.js:7:16)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
I went to node_modules\uuid\index.js file and there's only one variable in module.exports:
var v1 = require('./v1');
var v4 = require('./v4');
var uuid = v4;
uuid.v1 = v1;
uuid.v4 = v4;
module.exports = uuid;
And also in node_modules\sequelize\lib\utils.js, where this error seems to be from, I can see such code (lines 7,8). But I don't know why it fails.
const uuidv1 = require('uuid').v1;
const uuidv4 = require('uuid').v4;
So, I'm pretty confused what can I do to fix this problem.
Node version is v14.6.0 (just upgraded from v13 in hope that would fix the problem)

Angular universal build breaks with the following error: throw Error("not supported");

Running the following command : npm run build:ssr && npm run serve:ssr runs 3 different compilation stages but on the last one it returns the following error resulting to the server not starting.
Error: not supported
at Root.loadSync (/var/www/html/stage/node_modules/#grpc/proto-loader/node_modules/protobufjs/src/root.js:234:15)
at Object.loadSync (/var/www/html/stage/node_modules/#grpc/proto-loader/node_modules/protobufjs/src/index-light.js:69:17)
at Object.<anonymous> (/var/www/html/stage/node_modules/#grpc/proto-loader/build/src/index.js:244:37)
at Module._compile (internal/modules/cjs/loader.js:721:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:732:10)
at Module.load (internal/modules/cjs/loader.js:620:32)
at tryModuleLoad (internal/modules/cjs/loader.js:560:12)
at Function.Module._load (internal/modules/cjs/loader.js:552:3)
at Module.require (internal/modules/cjs/loader.js:657:17)
at require (internal/modules/cjs/helpers.js:22:18)
Environment and packages"
Angular CLI: 8.3.3
Node: 11.6.0
OS: linux x64
Angular: 8.2.5
... animations, common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... platform-server, platform-webworker
... platform-webworker-dynamic, router, service-worker
My server.ts looks as follows
const PORT = process.env.PORT || 4000;
const DIST_FOLDER = join(process.cwd(), 'dist');
const domino = require('domino');
const fs = require('fs');
const path = require('path');
const template = fs.readFileSync(path.join(__dirname, join(DIST_FOLDER, 'browser/index.html'))).toString();
const win = domino.createWindow(template);
global['window'] = win;
global['navigator'] = win.navigator;
global['Node'] = win.Node;
global['Event'] = win.Event;
global['Event']['prototype'] = win.Event.prototype;
global['document'] = win.document;
global["branch"] = null;
global['localStorage'] = localStorage;
Finally solved the problem.
1. Deleted problematic folder ./node_modules/#grpc/...
2. Manually uploaded a new #grpc folder with
#grpc
-- proto-loader
---- build/src/index.js
---- LICENCE
---- package.json
here's the repo to those who may find it useful
--> https://github.com/Gerald34/Angular-Universal-grpc
you can fix by adding process to your fake window in the server.ts file
win.process = process;
You can find more info in this article, with also other errors I encountered with Angular SSR + Firebase

Error while importing one file into another in Node.JS

I am using import while importing some functions from my practice.js file into different.js file.
practice.js file:-
function sum(x,y){
return x+y;
}
const pi = 3.14;
module.exports = {
sum : sum,
pi:pi
};
different.js file:-
import {sum,pi} from "./practice.js";
console.log("2 pie: "+sum(pi,pi));
Now when I am using require, the output is proper and no error is given.
When I am using import, there is this following error:-
SyntaxError: Unexpected token {
at Module._compile (internal/modules/cjs/loader.js:749:23)
at Object.Module._extensions..js
(internal/modules/cjs/loader.js:816:10)
at Module.load (internal/modules/cjs/loader.js:672:32)
at tryModuleLoad (internal/modules/cjs/loader.js:612:12)
at Function.Module._load (internal/modules/cjs/loader.js:604:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:868:12)
at internal/main/run_main_module.js:21:11
I have asked my colleagues and they told me that this is about ES6 and Babel is not configured in your system.
But I am not sure how to proceed with this. Can anybody please help me how to do it?
Rename your main file (different.js) to different.mjs.
Rename your practice.js file to practice.mjs and make it look like this:
function sum(x, y) {
return x + y;
}
const pi = 3.14;
export {sum, pi};
Then run node --experimental-modules different.mjs to run Node with it's experimental module loader.
You can read more here

Koa-pg Can't find module pg

Hey so I'm trying to get Nodejs Koa to talk to postgres using the Koa-pg module, but I keep getting a 'Can't find module pg' error.
I've tried to follow the koa-pg examples, but have come up short...so any advice would on how to progress would be appreciated.
If created my app.js file as follows:
var koa = require('koa');
var route = require('koa-route');
var koaPg = require('koa-pg');
var roads = require('./controllers/roads');
var app = module.exports = koa();
app.use(route.get('/roads/bbox/', roads.bbox));
app.listen(3000);
console.log('listening on port 3000');
And then created my controller file as follows:
var credentials = require('../credentials.js');
var environment = credentials.dev;
app.use(koaPg('postgres://' + environment.user + '#' + environment.host + ':' + environment.port + '/' + environment.database))
module.exports.bbox = function * bbox(next) {
var result = yield this.koaPg.db.client.queryPromise('SELECT now()')
console.log('result: ', result)
this.body = result.rows[0].now.toISOString()
};
But I'm getting the following error:
module.js:338
throw err;
^
Error: Cannot find module 'pg'
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 Object.<anonymous> (c:\Users\User\Documents\restful_koa\node_modul
es\koa-pg\index.js:12:27)
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 Module.require (module.js:365:17)
This is only a personal project but I'd love to understand where i'm going wrong.
Cheers
As mentioned in the comments:
You need to install the pg module via npm install pg or npm install pg --save if you want to save it to package.json.
The reason you need to do this is koa-pg has co-pg as a dependency so when you install the former the latter is also installed. But pg is not a dependency of co-pg and that is why you have to install it separately.

Error: Module version mismatch. when trying to launch in electron shell

When I try to launch my app in electron shell I get the below error:
I'm using node v0.12.3
I have installed electron-prebuilt
Uncaught Exception:
Error: Module version mismatch. Expected 43, got 14.
at Error (native)
at Object.module.(anonymous function) (ATOM_SHELL_ASAR.js:118:20)
at Object.module.(anonymous function) [as .node] (ATOM_SHELL_ASAR.js:118:20)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:298:12)
at Module.require (module.js:353:17)
at require (module.js:372:17)
at bindings (/src/git/superqa/node_modules/bcrypt/node_modules/bindings/bindings.js:76:44)
at Object.<anonymous> (/src/git/superqa/node_modules/bcrypt/bcrypt.js:3:35)
at Module._compile (module.js:418:26)
// my main.js file looks like this superqa/main.js
var app = require("./app/app.js");
var App = require("./src/app.js");
new App(app);
//my src/app.js looks like this superqa/src/app.js
var path = require("path");
var BrowserWindow = require('browser-window');
module.exports = App;
function App(app) {
var self = this;
this._app = app;
this._assetDir = path.resolve(__dirname, "..", "dist");
this.mainWindow = null;
app.on('ready', function() {
self.mainWindow = new BrowserWindow({
width: 1024,
height: 768
});
self.mainWindow.loadUrl("http://localhost:3000/login");
});
}
The problem is that the pre-built binaries were built with io.js 1.x, which uses a different (higher) ABI version than node.js. So if you want to use the pre-built version, you have to use io.js 1.x. Otherwise you will have to build electron yourself if you want to keep using node 0.12.x.

Resources