Export Import Command Hazards in Nodejs - javascript

I am trying to use a machine learning library from Github. Using decision trees example code, I got the error message "Unexpected token declaration" on the following row
import { DecisionTreeRegression as DTRegression } from 'ml-cart';
Given that import command is not fully supported by nodejs, I changed the above line to
var DTRegression = require ('ml-cart');
Now nodejs is flagging error on the subsequent statement var reg = new DTRegression(); with message "DTRegression is not a constructor".
Looking through index.js file at ./node_modules/src/index.js, I can see the following code:
export { DecisionTreeClassifier } from './DecisionTreeClassifier';
export { DecisionTreeRegression } from './DecisionTreeRegression';
Then I tried directly referencing the ./node_modules/src/DecisionTreeRegression.js file using require command. That gave errors on 'export class' command inside DecisionTreeRegression.js.
Then I tried using the following command to make the import work.
node --experimental-modules myapp.mjs
I got the error "SyntaxError: The requested module 'ml-cart' does not provide an export named 'DecisionTreeRegression'".
Nodejs version is 10.3 on Ubuntu 16.04 LTS.
Any help would be highly appreciated.
Thanks
Best Regards,
Adeel

Try the mjs extension here
DecisionTreeRegression.mjs when importing with
node --experimental-modules myapp.mjs

Related

Unable to import typescript file dynamically in deno exe

I am trying to import a file from aws in exe using its public link based on some user input but unable to do so
e.g. I created my exe using the following command
deno compile --allow-all main.ts
User run this exe using command like ./exe --qId 345. I parse this qId in exe and load some file from public link like this
let { runTestCases } = await import ("https://test-cases.nyc3.digitaloceanspaces.com/<qId>.ts")
But it gives me following error
TypeError: Module not found
at async getQuestionTestCaseFile (file:///F:/CodeQuotient/Deno_exe/deno/Controllers/question_controller.ts:47:37)
at async file:///F:/CodeQuotient/Deno_exe/deno/main.ts:14:1
It works fine when i run it using following command
deno run --allow-all main.ts
I want to know whether its possible or not to import module dynamically like this in exe.
If there is some other way of achieving this ?
As of Deno version 1.29.1:
"Dynamic" import() is not currently supported in deno compile.
This is a known issue and is being tracked at the following GitHub issue in the project's repository:
denoland/deno#8655 - deno compile does not have dynamic import support

Jest import statement: 'TypeError: Cannot set property 'fillStyle' of null'

I type:
npm test
and I get:
It's interesting to note that the import statement works inside the BootScene.test.js file but it doesn't work in the imported file.
I focused on the import statement not working under JEst. So I thought maybe it has something to do with Ecma Script version jest uses. So I tried this solution but the error persists.
This is the repo/branch of this question.
When I type npm start. Everything flows swiftly and with no errors.
The error occurs because jestdom is not able add canvas, which we need to do. For me this happened because I was using lottiefiles for the application.
Here is how I fixed it for my react application.
Install jest-canvas-mock as below .
npm i jest-canvas-mock
Then go to setup file like setup-tests.js and import jest-canvas as below .
import "jest-canvas-mock";
Now run test, you should not get errors any more.
I solved it by following this instructions.
Thanks CherryDt.
Installing npm i jest-canvas-mock
and Importing import "jest-canvas-mock" in set-up file; works for me

Get "SyntaxError: Cannot use import statement outside a module" when attempting to import a class in Jasmine spec file

As an exercise I decided to create a little vanilla JavaScript game using ES6 syntax that runs in the browser. The program works fine.
I'd like to test it using Jasmine. However, whenever I try to perform an import e.g.
import Deck from "../Deck.js";
Deck.js starts:
export default class Deck {
I get error SyntaxError: Cannot use import statement outside a module.
Things I've done:
Installed node v13.0.1 - I thought this version of node allowed es6 modules.
Installed jasmine and initialized node ./node_module/jasmine/bin/jasmine init
Run node ./node_module/jasmine/bin/jasmine - works fine without imports
Run node --experimental-modules ./node_module/jasmine/bin/jasmine - doesn't work with imports
Tried require instead of import: const Deck = require('../Deck.js'); - SyntaxError: Unexpected token 'export'
How do I get jasmine to work with imports? At the moment I cannot include any files to test !
I'm sure I've gone about this the wrong way, but i just want some cmd line tests.
Follow the offical guide from Babel at https://babeljs.io/setup#installation
Then choose one of the options from this solution: https://stackoverflow.com/a/59399717/673351
Personally, I have renamed my spec files to have the mjs extension becaue I would like ot use the LTS (currently 12) version of node.

Running JavaScript module returns SyntaxError: Unexpected token export

I'm trying to run a javascript package simple-statistics from
command-line via node
jupyter notebook via javascript kernel
In either case, I'm trying to import the module via:
var ss = require('simple-statistics')
but receiving the error:
C:\Users\...\simple-statistics\index.js:8
export { default as linearRegression } from './src/linear_regression';
^^^^^^
SyntaxError: Unexpected token export
I have tried multiple solutions such as
updating .babelrc to include multiple configurations of
{
"presets": ["latest"]
}
updating package.json to include code very similar to the above
pretty much everything I've found on SO
I'm new to Javascript, and to be honest it's a bit overwhelming navigating the sheer quantity of modules that may be related to the problem (babel, webpack, etc.).
I'm hoping someone can point me in the right direction, or help me what I assume is a simple issue. I'm drawing a blank. Thanks!
Solved (for this case).
As pointed out by loganfsmyth and Ruslan, I failed to create the distribution build.
Everything worked after running npm run build.

Stryker mutation tesing with ES6 files

I have encountered an issue where I am trying to perform mutation testing on my util classes in my react project using this http://stryker-mutator.github.io/ library. However I get the following errors,
[2017-05-17 16:29:04.321] [ERROR] CoverageInstrumenterStream - Error while instrumenting file "path/to/something.js", error was: Error: Line 29: Unexpected token
[2017-05-17 16:29:05.586] [ERROR] Stryker - One or more tests errored in the initial test run:
SyntaxError: Unexpected token import
seems the library cant identify the import and the export statements in the file.
I tried to search a fix for this but came up short. It will be grately appreciated if you experts can help me on a workaround, or a solution via grunt where I could change the import and exports to require and module.exports without harming the code format of the logic.
Thanks alot
I have worked with stryker framework. In our project, we were using browserify for importing files. So if you are using the same, you have to provide 'browserify' in framework array in stryker.conf.js.
The problem is that this version of Stryker work with ES5 by default.
There are a new version of Stryker which support ES6 (https://www.npmjs.com/package/stryker-javascript-mutator).

Categories