Sublime text 3 doesn't run nodeJS code with its build system - javascript

I was working with sublime text 3 and running my code with node build system.When I have updated nodeJS to v10, the problems began.
Node Build system :
{
"cmd": ["C:/Program Files/nodejs/node.exe","$file"],
"selector": "source.js"
}
The console shows this error:
console.log(window);
^
ReferenceError: window is not defined
at Object.<anonymous> (C:\Users\mhsn67\Documents\Front End NanoDegree\Example\Background-generator\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
[Finished in 0.2s]

Related

eslint error while writting and running test units in node js

enter image description here
The following error is occurring when running tests on my code.
rror [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './lib/rules/no-warning-comments' is not defined by "exports" in /data/credenceInternship/iwf-cli/node_modules/eslint/package.json
at throwExportsNotFound (internal/modules/esm/resolve.js:290:9)
at packageExportsResolve (internal/modules/esm/resolve.js:513:3)
at resolveExports (internal/modules/cjs/loader.js:432:36)
at Function.Module._findPath (internal/modules/cjs/loader.js:472:31)
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:867:27)
at Function.Module._load (internal/modules/cjs/loader.js:725:27)
at Module.require (internal/modules/cjs/loader.js:952:19)
at require (/data/credenceInternship/iwf-cli/node_modules/v8-compile-cache/v8-compile-cache.js:159:20)
at Object.<anonymous> (/data/credenceInternship/iwf-cli/node_modules/eslint-plugin-unicorn/rules/expiring-todo-comments.js:5:18)
at Module._compile (/data/credenceInternship/iwf-cli/node_modules/v8-compile-cache/v8-compile-cache.js:192:30)

Can't run require('opencv4nodejs')

I'm using macOS Monterey 12.2.1, Apple M1. And in the project directory, I installed opencv-build and opencv4nodejs by this script code:
"scripts": { "postinstall": "npm i opencv-build && npm i opencv4nodejs" }
When I run cv = require('opencv4nodejs'), I got this error message and I can't understand it. How can I run this code correctly?
/usr/local/bin/node /Users/david/Desktop/project2/main.js
/Users/david/Desktop/project2/node_modules/opencv4nodejs/lib/cv.js:47
throw err
^
Error: Cannot find module '/Users/david/Desktop/project2/node_modules/opencv4nodejs/build/Release/opencv4nodejs'
Require stack:
- /Users/david/Desktop/project2/node_modules/opencv4nodejs/lib/cv.js
- /Users/david/Desktop/project2/node_modules/opencv4nodejs/lib/opencv4nodejs.js
- /Users/david/Desktop/project2/main.js
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
at Function.Module._load (node:internal/modules/cjs/loader:778:27)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.<anonymous> (/Users/david/Desktop/project2/node_modules/opencv4nodejs/lib/cv.js:40:8)
at Module._compile (node:internal/modules/cjs/loader:1103:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1155: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) {
code: 'MODULE_NOT_FOUND',
requireStack: [
'/Users/david/Desktop/project2/node_modules/opencv4nodejs/lib/cv.js',
'/Users/david/Desktop/project2/node_modules/opencv4nodejs/lib/opencv4nodejs.js',
'/Users/david/Desktop/project2/main.js'
]
}
Process finished with exit code 1
Use Node 14.18.1, then uninstall opencv4nodejs.
Run export CXXFLAGS="-std=c++14 -Wno-c++11-narrowing"
Run npm install opencv4nodejs

npx react-native init MainApp Giving error (code: 'MODULE_NOT_FOUND',) . What's the isue here

I got this error while running the above command (npx react-native init MainApp)
I have installed the latest version of node and npm. I am not sure why this error is reflected.
Tried clearing the cache changing the node version to 17 but no luck. I am new to this react native.
node:internal/modules/cjs/loader:936
throw err;
^
Error: Cannot find module './error'
Require stack:
- C:\Users\priye\AppData\Local\npm-cache\_npx\7930a8670f922cdb\node_modules\#hapi\hoek\lib\assert.js
- C:\Users\priye\AppData\Local\npm-cache\_npx\7930a8670f922cdb\node_modules\#hapi\topo\lib\index.js
- C:\Users\priye\AppData\Local\npm-cache\_npx\7930a8670f922cdb\node_modules\#react-native-community\cli\node_modules\joi\lib\types\keys.js
- C:\Users\priye\AppData\Local\npm-cache\_npx\7930a8670f922cdb\node_modules\#react-native-community\cli\node_modules\joi\lib\types\function.js
- C:\Users\priye\AppData\Local\npm-cache\_npx\7930a8670f922cdb\node_modules\#react-native-community\cli\node_modules\joi\lib\index.js
- C:\Users\priye\AppData\Local\npm-cache\_npx\7930a8670f922cdb\node_modules\#react-native-community\cli\build\tools\config\schema.js
- C:\Users\priye\AppData\Local\npm-cache\_npx\7930a8670f922cdb\node_modules\#react-native-community\cli\build\tools\config\readConfigFromDisk.js
- C:\Users\priye\AppData\Local\npm-cache\_npx\7930a8670f922cdb\node_modules\#react-native-community\cli\build\tools\config\index.js
- C:\Users\priye\AppData\Local\npm-cache\_npx\7930a8670f922cdb\node_modules\#react-native-community\cli\build\commands\install\install.js
- C:\Users\priye\AppData\Local\npm-cache\_npx\7930a8670f922cdb\node_modules\#react-native-community\cli\build\commands\index.js
- C:\Users\priye\AppData\Local\npm-cache\_npx\7930a8670f922cdb\node_modules\#react-native-community\cli\build\index.js
- C:\Users\priye\AppData\Local\npm-cache\_npx\7930a8670f922cdb\node_modules\#react-native-community\cli\build\bin.js
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
at Function.Module._load (node:internal/modules/cjs/loader:778:27)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.<anonymous> (C:\Users\priye\AppData\Local\npm-cache\_npx\7930a8670f922cdb\node_modules\#hapi\hoek\lib\assert.js:3:21)
at Module._compile (node:internal/modules/cjs/loader:1101:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153: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) {
code: 'MODULE_NOT_FOUND',
requireStack: [
'C:\\Users\\priye\\AppData\\Local\\npm-cache\\_npx\\7930a8670f922cdb\\node_modules\\#hapi\\hoek\\lib\\assert.js',
'C:\\Users\\priye\\AppData\\Local\\npm-cache\\_npx\\7930a8670f922cdb\\node_modules\\#hapi\\topo\\lib\\index.js',
'C:\\Users\\priye\\AppData\\Local\\npm-cache\\_npx\\7930a8670f922cdb\\node_modules\\#react-native-community\\cli\\node_modules\\joi\\lib\\types\\keys.js',
'C:\\Users\\priye\\AppData\\Local\\npm-cache\\_npx\\7930a8670f922cdb\\node_modules\\#react-native-community\\cli\\node_modules\\joi\\lib\\types\\function.js',
'C:\\Users\\priye\\AppData\\Local\\npm-cache\\_npx\\7930a8670f922cdb\\node_modules\\#react-native-community\\cli\\node_modules\\joi\\lib\\index.js',
'C:\\Users\\priye\\AppData\\Local\\npm-cache\\_npx\\7930a8670f922cdb\\node_modules\\#react-native-community\\cli\\build\\tools\\config\\schema.js',
'C:\\Users\\priye\\AppData\\Local\\npm-cache\\_npx\\7930a8670f922cdb\\node_modules\\#react-native-community\\cli\\build\\tools\\config\\readConfigFromDisk.js',
'C:\\Users\\priye\\AppData\\Local\\npm-cache\\_npx\\7930a8670f922cdb\\node_modules\\#react-native-community\\cli\\build\\tools\\config\\index.js',
'C:\\Users\\priye\\AppData\\Local\\npm-cache\\_npx\\7930a8670f922cdb\\node_modules\\#react-native-community\\cli\\build\\commands\\install\\install.js',
'C:\\Users\\priye\\AppData\\Local\\npm-cache\\_npx\\7930a8670f922cdb\\node_modules\\#react-native-community\\cli\\build\\commands\\index.js',
'C:\\Users\\priye\\AppData\\Local\\npm-cache\\_npx\\7930a8670f922cdb\\node_modules\\#react-native-community\\cli\\build\\index.js',
'C:\\Users\\priye\\AppData\\Local\\npm-cache\\_npx\\7930a8670f922cdb\\node_modules\\#react-native-community\\cli\\build\\bin.js'
]
}
Hi I myself resolved the issue. I deleted the npm-cache folder from the AppData folder and it worked :). Thanks

Babel JS babel-preset-php error

I was just trying the new Babel's babel-preset-php (https://gitlab.com/kornelski/babel-preset-php#php7-to-es7-syntax-translator). I did everything in the README file, I installed the preset with npm i -S babel-preset-php. Then I created a .babelrc file with the following contents;
{
"presets": ["php"]
}
Installed the cli with npm i -g babel-cli. Then I created a simple PHP file that only contains a simple function:
<?php
function addCalculator($x, $y)
{
return $x + $y;
}
And tried to run the transpiler with babel number.php -o file.js. But I get an error in the execution of the script:
/home/claudio/Documents/Development/babel/node_modules/babel-preset-php/lib/plugins.js:6
Identifier(p) {
^
SyntaxError: Unexpected token ( (While processing preset: "/home/claudio/Documents/Development/babel/node_modules/babel-preset-php/index.js")
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 Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (/home/claudio/Documents/Development/babel/node_modules/babel-preset-php/index.js:1:79)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
I'm not that experient with nodejs and npm, so any idea on what might be happening?
Edit: Ok, I just realized that you are calling a public function outside of a class. That's not correct PHP. You can't define a function as public outside of a class. Your PHP code is just wrong.
The error is being emitted before it even touches your PHP code. It is, in fact, erroring on this particular line of babel-preset-php itself:
return {
visitor: {
Identifier(p) { // This is the invalid line
if (p.node.name != 'Exception' || p.scope.hasBinding("Exception")) {
return;
}
This preset is using the shorthand object initializers added in ECMAScript 2015. What is probably happening here is that your local Javascript environment does not support ES2015 syntax.
I would recommend updating node and babel to the latest versions and trying again.

Node and Require import error in Mocha?

I have a working Node application and I'm trying to add Mocha tests, but getting some odd import errors.
This is my file structure:
package.json
index.js
src/
chart.js
highcharts-options.js
test/
test_chart.js
This is what my chart.js file looks like:
var chartOptions = require('./highcharts-options');
var analyseChart = {
doSomething: function() { ... }
};
module.exports = analyseChart;
And this is what highcharts-options.js looks like:
var HighCharts = require('highcharts-browserify');
Highcharts.theme = { ... };
Currently I import everything from /src into a single index.js file, then bundle it with browserify, which works just fine, no errors in the application.
Now I want to start writing Mocha tests for the functions in /src.
This is my first stub in test_chart.js:
var chart = require('../src/chart');
chart.doSomething();
But when I run mocha, I get the following error:
Users/.../js/src/highcharts-options.js:11
Highcharts.theme = {
^
ReferenceError: Highcharts is not defined
at Object.<anonymous> (/Users/.../js/src/highcharts-options.js:11:1)
at Module._compile (module.js:456:26)
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 Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (/Users/.../js/src/chart.js:7:20)
at Module._compile (module.js:456:26)
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 Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (/Users/.../js/test/test_chart.js:1:75)
How can I fix this import error for Mocha?
var HighCharts = require('highcharts-browserify');
Highcharts.theme = { ... };
You have a spelling error.
High[cC]harts
How did you not notice this yourself even when you were told exactly what was wrong?
Some errors are so common that we ultimately stop trusting them, and start looking for other faults in our code without even investigating it.
This is especially true for the "someVar is undefined" error in javascript.
Takeaway:
Sometimes you just got to trust your error messages.

Categories