I'm importing a JavaScript file that defines a nested object but has no export. When doing it from the REPL and defining the nested object first it works. When running from a script, it errors out saying the object is not defined. I understand why I get the error, but not why it works in the REPL, and more importantly why the behaviour is different. My two questions are:
Where does the behaviour difference comes from?
Is it possible to emulate the REPL behaviour in a script?
Using node REPL
Running node from the command line as a REPL:
$ cat loadme.js
myvar.a.b="defined";
$
$ node
Welcome to Node.js v18.13.0.
Type ".help" for more information.
> process.version;
'v18.13.0'
> const myvar = {a: {b: null}};
undefined
> require("./loadme.js");
{}
> myvar
{ a: { b: 'defined' } }
From a script
$ cat loadme.js
myvar.a.b="defined";
$
$ cat main.j
#!/bin/env node
console.log(process.version);
const myvar = {a: {b: null}};
require("./loadme.js");
console.log("==>", myvar);
./main.js
v18.13.0
/home/yves/p/wip/js/dyn-requre/loadme.js:1
myvar.a.b="defined";
^
ReferenceError: myvar is not defined
I'm building a Vue project including some static files. For serving it locally, I've set this on package.json:
{
...
"eslintConfig": {
...
"ignorePatterns": [
"**/vendor/*.js"
]
}
...
}
and it works fine. But when I try to build it with npm run build, I get the following:
- Building for production...
[BABEL] Note: The code generator has deoptimised the styling of /app/src/assets/vendor/fontawesome-free-5.15.3-web/js/all.min.js as it exceeds the max of 500KB.
ERROR Failed to compile with 2 errors
error in ./src/assets/vendor/bootstrap-4.6.0/bootstrap.bundle.min.js
Module Error (from ./node_modules/thread-loader/dist/cjs.js):
/app/src/assets/vendor/bootstrap-4.6.0/bootstrap.bundle.min.js
6:144 error 'define' is not defined no-undef
6:157 error 'define' is not defined no-undef
6:234 error 'globalThis' is not defined no-undef
6:723 error 'a' is a function no-func-assign
6:27968 error Empty block statement no-empty
6:36778 error Unnecessary escape character: \- no-useless-escape
6:37519 error Unnecessary escape character: \- no-useless-escape
6:68065 error 'n' is defined but never used no-unused-vars
✖ 8 problems (8 errors, 0 warnings)
The 2 errors are for these assets, both are similar. Is there a way of skipping these errors on build? It seems like a linting issue only.
if your config file includes this statement, you can try to commend out this line:
"extends": "eslint:recommended"
Running mocha tests in node I'm getting the following syntax errors.
String Template: this one worked on 4.4, but is failing on 6.2.
/home/ubuntu/workspace/lib/admin.js:18
ROOT: `${homeDir}/.config`,
^
SyntaxError: Unexpected token ILLEGAL
full code:
var homeDir = os.homedir(),
configLocations = {
ROOT: `${homeDir}/.config`,
BASE: `${homeDir}/.config/nobjs`,
FILE: `${homeDir}/.config/nobjs/nobjs_config.json`
};
Default Parameter:
/home/ubuntu/workspace/lib/nobutil.js:4
function splitStringToArray(str, seperator = ','){
^
SyntaxError: Unexpected token =
These fail when I try to run mocha tests.
These seem to be supported.
All simple contrived examples seem to be working in the console. Is mocha the problem?
Thanks to #robertklep 's tip, it is a path problem, global mocha running the tests using system installed node on cloud9.
by installing mocha locally and prefixing my path so that mocha is resolved first, mocha calls my default nvm installed node.
export PATH=/home/ubuntu/workspace/node_modules/mocha/bin:$PATH
Follow the steps described in this document, I use the following command to install rxjs:
npm install #reactivex/rxjs --save
then I write the testing code below in my index.js file:
'use strict';
var Rx = require('#reactivex/rxjs');
Rx.Observable.from('test').subscribe(function (c) { return console.log(c); });
When I ran:
jpm run
I got these errors from the terminal:
JPM [error] Message: TypeError: root_1.root.Symbol is undefined
Stack:
#resource://xxxx/node_modules/#reactivex/rxjs/dist/cjs/util/Symbol_observable.js:5:5
#resource://xxxx/node_modules/#reactivex/rxjs/dist/cjs/Observable.js:3:27
#resource://xxxx/node_modules/#reactivex/rxjs/dist/cjs/Rx.KitchenSink.js:1:20
#resource://xxxx/node_modules/#reactivex/rxjs/index.js:1:18
#resource://xxxx/index.js:2:10
run#resource://gre/modules/commonjs/sdk/addon/runner.js:147:19
startup/</<#resource://gre/modules/commonjs/sdk/addon/runner.js:87:9
Handler.prototype.process#resource://gre/modules/Promise-backend.js:934:23
this.PromiseWalker.walkerLoop#resource://gre/modules/Promise-backend.js:813:7
this.PromiseWalker.scheduleWalkerLoop/<#resource://gre/modules/Promise-backend.js:747:1
I haven't used RxJS myself but I have been wanting to learn it.
Here are some addons out there that do use it:
https://addons.mozilla.org/en-US/firefox/addon/wappalyzer/
https://addons.mozilla.org/en-US/firefox/addon/keypocket/
https://addons.mozilla.org/en-US/firefox/addon/qtest-web-explorer/ - uses rx.lite
I would love to see how you use it
I'm trying to get unit tests coverage with Istanbul and Isparta, and I'm having some trouble.
Actually, here's my gulp file tasks:
gulp.task('pre-test', ['default'], function() {
return gulp.src('src/app/**/*.js')
.pipe(plumber())
.pipe(istanbul({
instrumenter: isparta.Instrumenter,
includeUntested: true
}))
.pipe(istanbul.hookRequire());
});
gulp.task('test', ['pre-test'], function() {
return gulp.src('src/test/**/*.js')
.pipe(mocha({reporter: 'spec'}))
.pipe(istanbul.writeReports({}));
});
When I start the gulp "test" task, I have the following errors:
[08:34:17] Plumber found unhandled error:
Error in plugin 'gulp-istanbul'
Message:
Unable to parse C:\projects\nodejs\mon-notaire\src\app\core\logger\concrete\ConsoleLogger.js
Line 1: Unexpected token
[08:34:17] Finished 'pre-test' after 2.11 s
[08:34:17] Starting 'test'...
stream.js:94
throw er; // Unhandled stream error in pipe.
^
C:\projects\nodejs\mon-notaire\src\test\core\TestConfReader.js:1
(function (exports, require, module, __filename, __dirname) { import ConfReade
^^^^^^
SyntaxError: Unexpected reserved word
at exports.runInThisContext (vm.js:73:16)
at Module._compile (module.js:443:25)
at Object.Module._extensions..js (module.js:478:10)
at Object.Module._extensions.(anonymous function) [as .js] (C:\projects\nodejs\mon-notaire\node_modules\gulp-istanbul\node_modules\istanbul\lib\hook.js:109:37)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Module.require (module.js:365:17)
at require (module.js:384:17)
at C:\projects\nodejs\mon-notaire\node_modules\mocha\lib\mocha.js:192:27
at Array.forEach (native)
npm ERR! Test failed. See above for more details.
How can I prevent these errors from occurring?
Have you set up .babelrc?
If you're using the latest version of isparta, which depends on babel v6, then you need to set up .babelrc like the following. ( You also need to do npm install --save-dev babel-preset-es2015 )
{
"presets": [
"es2015"
]
}
From gulp-istanbul github page.
var isparta = require('isparta');
var istanbul = require('gulp-istanbul');
gulp.src('lib/**.js')
.pipe(istanbul({
// supports es6
instrumenter: isparta.Instrumenter
}));
This line :
Message:
Unable to parse C:\projects\nodejs\mon-notaire\src\app\core\logger\concrete\ConsoleLogger.js
Means that there is a problem in your code in ConsoleLogger.js , so you might want to check that file out.
This Line :
C:\projects\nodejs\mon-notaire\src\test\core\TestConfReader.js:1
(function (exports, require, module, __filename, __dirname) { import ConfReade
^^^^^^
SyntaxError: Unexpected reserved word
Suggests that you are using ES6, but your gulp task is not transpiling it to ES5 before running it, which is why you are getting the error.
I have made a yeoman generator which creates a project for exactly this purpose (writing nodeJs projects in ES6) and includes code coverage using istanbul with source code mapping. You might want to take a look at that.
Otherwise, here is my working gulpfile from that generator.
I use istanbul, along with a module called remap-istanbul.
The error message indicates that you are using ES6, but the gulp-istanbul doesn't support it by default.
Of course, you can write your functions to compile the ES6 codes, but considering you are using gulp in this case, IMHO the simplest way you can do is to use gulp-babel-istanbul instead of gulp-istanbul, no need to change your code attached above at all.
import istanbul from 'gulp-babel-istanbul'
And the rest of the code remains the same.