Mocha testing Typescript source file: unable to import node modules - node.js

I have setup mocha to test typescript source files. I have successfully run a test on one file that only imported source files, but I'm seeing a failure running a test that imports a node module.
I am running this on the command line to run the tests:
mocha -r ts-node/register -r jsdom-global/register spec/**/**.spec.ts
In the test that is failing, the source file that it is testing has this import
import * as angular from "angular";
And when the test is run, I see this error:
ReferenceError: angular is not defined
at Object.<anonymous> (/Users/bsacamano/testRepos/myApp/node_modules/angular/index.js:2:18)
at Module._compile (node:internal/modules/cjs/loader:1103:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1157:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.<anonymous> (/Users/bsacamano/testRepos/myApp/web/common/default/unsecured/shared-modules/services/flexlabel/flex.service.ts:1:1)
at Module._compile (node:internal/modules/cjs/loader:1103:14)
at Module.m._compile (/Users/bsacamano/testRepos/myApp/node_modules/ts-node/src/index.ts:1455:23)
at Module._extensions..js (node:internal/modules/cjs/loader:1157:10)
at Object.require.extensions.<computed> [as .ts] (/Users/bsacamano/testRepos/myApp/node_modules/ts-node/src/index.ts:1458:12)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.<anonymous> (/Users/bsacamano/testRepos/myApp/spec/shared-modules/services/flexlabel/flex.service.spec.ts:1:1)
at Module._compile (node:internal/modules/cjs/loader:1103:14)
at Module.m._compile (/Users/bsacamano/testRepos/myApp/node_modules/ts-node/src/index.ts:1455:23)
at Module._extensions..js (node:internal/modules/cjs/loader:1157:10)
at Object.require.extensions.<computed> [as .ts] (/Users/bsacamano/testRepos/myApp/node_modules/ts-node/src/index.ts:1458:12)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.exports.requireOrImport (/Users/bsacamano/testRepos/myApp/node_modules/mocha/lib/nodejs/esm-utils.js:49:16)
at async Object.exports.loadFilesAsync (/Users/bsacamano/testRepos/myApp/node_modules/mocha/lib/nodejs/esm-utils.js:91:20)
at async singleRun (/Users/bsacamano/testRepos/myApp/node_modules/mocha/lib/cli/run-helpers.js:125:3)
at async Object.exports.handler (/Users/bsacamano/testRepos/myApp/node_modules/mocha/lib/cli/run.js:370:5)
I have a different test for a source file that has these imports
import {IHttpService, IPromise, IRequestConfig} from 'angular';
and that test completes without error.
Also, the code works in production; I'm seeing that angular is defined and working.
Any ideas on what I can do to get the test to pass?
Update:
I think it has to do with the clown show that is bare imports. My web app is built with webpack, and webpack takes care of converting those imports when bundling.
Out of desperation, I tried building a bundle for my tests (which I really didn't want to do since I'm not distributing the file, but you know). I then ran mocha on the bundle, but I'm still getting the same error, angular is not defined, just with a different stack trace.
Update 2:
Even if I were to remove the "bare" import and replace with an import of the script relative to the file, it doesn't work. I don't get the ReferenceError, but the import just doesn't happen; if I console.log(angular); after importing it, I see that it is an empty object.

Related

Electron: TypeError: Cannot create property 'default' on symbol 'Symbol(nodejs.util.inspect.custom)'

I've developed an electron app with following tech stack:
NodeJS (10.15) with Babel to allow ES6 import/export syntax.
Sequelize on SQLite and GraphQL to handle an internal database.
ReactJS (bundled) for views.
I've configured everything to work with Electron and running "electron" command in project root folder works without errors.
Now i'm trying to distribute it using this tutorial: https://electronjs.org/docs/tutorial/application-distribution
So i've simply downloaded the 4.1.4 binaries for windows and moved the project folder under resources/app/
But running the electron.exe i've the following error:
App threw an error during load
TypeError: Cannot create property 'default' on symbol 'Symbol(nodejs.util.inspect.custom)'
at Object.<anonymous> (C:\workspace\myapp\resources\app\node_modules\graphql\jsutils\nodejsCustomInspectSymbol.js:18:84)
at Object.<anonymous> (C:\workspace\myapp\resources\app\node_modules\graphql\jsutils\nodejsCustomInspectSymbol.js:20:3)
at Module._compile (internal/modules/cjs/loader.js:711:30)
at Module._compile (C:\workspace\myapp\resources\app\node_modules\pirates\lib\index.js:83:24)
at Module._extensions..js (internal/modules/cjs/loader.js:722:10)
at Object.newLoader [as .js] (C:\workspace\myapp\resources\app\node_modules\pirates\lib\index.js:88:7)
at Module.load (internal/modules/cjs/loader.js:620:32)
at tryModuleLoad (internal/modules/cjs/loader.js:559:12)
at Function.Module._load (internal/modules/cjs/loader.js:551:3)
at Module.require (internal/modules/cjs/loader.js:658:17)
at require (internal/modules/cjs/helpers.js:20:18)
at Object.<anonymous> (C:\workspace\myapp\resources\app\node_modules\graphql\jsutils\defineToJSON.js:8:57)
at Object.<anonymous> (C:\workspace\myapp\resources\app\node_modules\graphql\jsutils\defineToJSON.js:36:3)
at Module._compile (internal/modules/cjs/loader.js:711:30)
at Module._compile (C:\workspace\myapp\resources\app\node_modules\pirates\lib\index.js:83:24)
at Module._extensions..js (internal/modules/cjs/loader.js:722:10)
at Object.newLoader [as .js] (C:\workspace\myapp\resources\app\node_modules\pirates\lib\index.js:88:7)
at Module.load (internal/modules/cjs/loader.js:620:32)
at tryModuleLoad (internal/modules/cjs/loader.js:559:12)
at Function.Module._load (internal/modules/cjs/loader.js:551:3)
at Module.require (internal/modules/cjs/loader.js:658:17)
at require (internal/modules/cjs/helpers.js:20:18)
i've also tried to run npx electron resources/app/ from binaries folder and i've got the same error.
So i think the issue is something about the paths.
i've also tried electron-packager and the error is the same because this tool does the same process of guide above.
What can i do to fix it?
UPDATE1: after some investigations i've found that babel causes the issues. I guess when you run a project from a parent folder. I'm trying to find a solution
i've solved using this kind of configuration for babel/register:
const rootPath=path.resolve(path.join(__dirname, "../../"));
require("#babel/register")({
root: rootPath,
extends: path.join(rootPath, '.babelrc'),
ignore: [/node_modules/],
only: [rootPath],
})

Datastore is not a contructor error when trying ./bin/www

I received an error when I try to execute the command "./bin/www" in my installed ubuntu application on Windows.
Error is as below:
/mnt/c/Users/admin/Documents/html/developer_portal/app.js:91
dataset: new Datastore(datastoreOptions)
^
TypeError: Datastore is not a constructor
at Object.<anonymous> (/mnt/c/Users/admin/Documents/html/developer_portal/app.js:91:12)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/mnt/c/Users/admin/Documents/html/developer_portal/bin/www:7:11)
at Module._compile (module.js:652:30)
I ran into the same issue trying to re-use some older code with v3.0 of the node helper library for Datastore. It looks like the import syntax has changed a bit in v3. Line 91 in your app.js probably looks something like:
const datastore = new Datastore({projectID: 'myProject'});
Just before that you probably declare Datastore like:
const Datastore = require('#google-cloud/datastore');
... when what you now want is:
const {Datastore} = require('#google-cloud/datastore');

Including node modules in AWS Lambda

I am trying to create a nodejs lambda function with the web3 module.
However, when zipping my handler.js and the entire node_modules directory and testing the lambda, the function cannot find web3.
Structure of the zip file:
handler.js
node_modules/
-web3_module_1/
-web3_module_2/
-other_modules_used_by_web3/
According to this source, the zip file structure should be the following:
handler.js
web3_module_1/
web3_module_2/
other_module/
But even with this structure, I get an error:
Unable to import module 'handler': Error
at Function.Module._load (module.js:417:25)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/var/task/handler.js:3:14)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
What is the correct way to achieve this?

Custom dependency require Electron

With the package.json, it's possible to do something like this
"dependencies": {
"WindowManager": "./lib/WindowManager"
},
But when trying to do this in Electron, it gives me this error:
App threw an error during load
Error: Cannot find module 'WindowManager'
at Module._resolveFilename (module.js:470:15)
at Function.Module._resolveFilename (.../desktop/node_modules/electron/dist/Electron.app/Contents/Resources/electron.asar/common/reset-search-paths.js:35:12)
at Function.Module._load (module.js:418:25)
at Module.require (module.js:498:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (.../desktop/lib/init.js:15:35)
at Object.<anonymous> (.../desktop/lib/init.js:22:3)
at Module._compile (module.js:571:32)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:488:32)
Is it because of Electron, or am I doing something wrong?
./lib/WindowManager is a folder with a package.json in it with a main pointing to the WindowManager.js, so that should work.
The case you try to do normally works, make sure that the dependency "WindowManager" has been properly copied by the build tools to either:
.../desktop/node_modules/electron/dist/Electron.app/Contents/Resources/app.asar/node_modules
or
.../desktop/node_modules/electron/dist/Electron.app/Contents/Resources/node_modules
If WindowManager does not exist in the disted node_modules there is something wrong with your build process, hard to be more precise than that.

problems with node paths in prod env

I have my first node server app running fine on localhost, but in a digitalocean (ubuntu) environment I can't get the paths to work.
This line
require("./lib/dbHelper").initilize();
Generate this error
Error: Cannot find module './lib/dbHelper'
at Function.Module._resolveFilename (module.js:339:15)
at Function.Module._load (module.js:290:25)
at Module.require (module.js:367:17)
at require (internal/module.js:16:19)
at Object.<anonymous> (/var/www/wsApp/app.js:18:1)
at Module._compile (module.js:413:34)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Function.Module.runMain (module.js:447:10)
And this line
let User = require("../models/User");
generates this error
Error: Cannot find module '../models/User'
at Function.Module._resolveFilename (module.js:339:15)
at Function.Module._load (module.js:290:25)
at Module.require (module.js:367:17)
at require (internal/module.js:16:19)
at Object.<anonymous> (/var/www/wsApp/routes/auth.js:5:12)
at Module._compile (module.js:413:34)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Module.require (module.js:367:17)
I have looked here (https://gist.github.com/branneman/8048520) for solutions and I have tried suggestions 3, 4.2 and 6 but nothing changes the error messages.
Worth noting is that this line seems to work fine
app.use("/", require("./routes/auth.js"));
Being new to node I don't quite understand the path mappings, and it seems strange that this last one would work while the first one doesn't since they both use the dot syntax...
If you are developing on a case-insensitive filesystem (OS X or Windows), you may be getting tripped up by the fact that the linux filesystem is case sensitive. Check, for example, that the "H" in "dbHelper" is really uppercase in your git repository.

Resources