What command should we use to run an example in the develop branch?
Examples are throwing errors in the develop branch.
In the develop branch.
When I try to run any example in node, it's throwing an error in the develop branch:
$ node examples/javascript/which-letter-simple.js
.../brain.js/examples/javascript/which-letter-simple.js:1
(function (exports, require, module, __filename, __dirname) { import brain from '../../src';
^^^^^^
SyntaxError: Unexpected token import
at new Script (vm.js:51:7)
at createScript (vm.js:136:10)
at Object.runInThisContext (vm.js:197:10)
at Module._compile (internal/modules/cjs/loader.js:618:28)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:665:10)
at Module.load (internal/modules/cjs/loader.js:566:32)
at tryModuleLoad (internal/modules/cjs/loader.js:506:12)
at Function.Module._load (internal/modules/cjs/loader.js:498:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:695:10)
at startup (internal/bootstrap/node.js:201:19)
$ node --version
v9.11.2
It works fine in the master branch:
$ node examples/which-letter-simple.js
iterations: 10, training error: 0.23317416631428975
iterations: 20, training error: 0.210598456317468
iterations: 30, training error: 0.17436649541030716
iterations: 40, training error: 0.13409059488488398
iterations: 50, training error: 0.10131207107612829
iterations: 60, training error: 0.07420014998226022
iterations: 70, training error: 0.0518410705540017
iterations: 80, training error: 0.03558307536025094
iterations: 90, training error: 0.025044735270968704
iterations: 100, training error: 0.018431227715397347
iterations: 110, training error: 0.014178778824250277
iterations: 120, training error: 0.011323259386754258
iterations: 130, training error: 0.009319155256412576
iterations: 140, training error: 0.007856361954302204
iterations: 150, training error: 0.0067525114866180585
iterations: 160, training error: 0.005895903210432278
iterations: 170, training error: 0.005215334626081061
a
You have to change import (not supported in nodejs environment) statement to require, like:
const brain = require('../../src')
Thanks, I just changed that line in which-letter-simple.js, but now it breaks in index.js. Should I be using a different version of node, or is there some other runtime besides node that you're using to run these examples?
$ node examples/javascript/which-letter-simple.js
brain.js/src/index.js:1
(function (exports, require, module, __filename, __dirname) { import activation from './activation';
^^^^^^
SyntaxError: Unexpected token import
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Module._compile (module.js:616:28)
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> (brain.js/examples/javascript/which-letter-simple.js:1:77)
Yes, you right. This is the correct one: https://github.com/BrainJS/brain.js/blob/develop/dist/index.js
const brain = require('../../dist')
I was thinking of converting all those import statements to make it so we don't have to build it. Thoughts?
Thanks @mubaidr, after changing that line and running a fresh dist build, it is now running without any errors.
@robertleeplummerjr I would support that because it takes 30+ seconds to build (the develop branch) on my old macbook, so running example scripts without rebuilding could save a lot of time for anyone getting ready for a PR, like myself. I could take a crack at that if you want, so that you can focus on the more complex stuff like gpu support, etc.
@massaroni If you could do that, I would be very grateful. I've been very active getting GPU.js v2 working for NodeJS, React Native for both Android and iOS, and even RaspberryPi support! I'm solving an issue that crept up when converting https://github.com/hanbollar/Odin right now, after that, I believe we have all current projects and tests passing, and Brain.js will finally have full on stable GPU support _everywhere_.
Going to go ahead and close this as I believe the original question was covered.
@massaroni That would be great. looking forward!
Thanks guys, I'm happy to help out. This is an awesome project and you guys are super busy. I found a way to keep all the es6 import/export statements and still run examples in node without running a build, and I just submitted a PR for that.
Actually this is fine. But the conversion is still required, since most of the nodejs programs are written & run without the experimental esm flag enabled. And this conversion will enable users to be able to use this lib out of the box without any modification to their script or run command.
Sure thing, I can convert all those imports to require's.
Another solution is to use babel-core/register while running the examples. If you edit an example to start with const brain = require('../src').default; or import brain from '../src';, then you can run the example with node -r babel-core/register examples/learn-math.js. (This also means you can use imports and ES6+ stuff inside of the examples too.)
(There is a startup penalty, but it's much less after the first time because of Babel's caching, and the penalty is much less than how long it takes to run Brain.js.)
If there was some massive gain to be had, I'd be for it, but it is a level of indirection that isn't nessisary for sugar. Plus it makes the gpu layer more tedious. I'd rather have the control till there is some amazing gain.
Most helpful comment
I was thinking of converting all those import statements to make it so we don't have to build it. Thoughts?