How can I use Node.js native import/export in version 9 - node.js

I want to use Node native support for import/export with NPM packages. To start I'm running cuccumberjs version 3.1.0. However when I run this command:
/usr/local/bin/node --experimental-modules ./node_modules/.bin/cucumber-js
I get an error:
(node:42011) ExperimentalWarning: The ESM module loader is experimental.
SyntaxError: Unexpected token import
/full/path/to/test/directory/features/support/getWindowLocation.js:2
import {runJsInBrowser} from "./runJsInBrowser.mjs";
^^^^^^
All my automation test run fine until I try to convert one of my scripts to a ES module and import it. Here is an snippet from the file:
runJsInBrowsers.mjs
export default async function runJsInBrowser(
browserDriver,
windowFunc,
waitForReturn = true,
timeOut = 10000
) {
...
};
--
package.json
{
"name": "tests",
"version": "0.0.1",
"description": "Test for testing.",
"main": "index.js",
"license": "UNLICENSED",
"dependencies": {
"chromedriver": "^2.0",
"cucumber": "^3.1",
"selenium-webdriver": "^3.0"
},
"devDependencies": {
}
}

Related

Parcel+Sass+npm: "Error: Failed to install #parcel/transformer-sass: Callback must be a function. Received undefined

I'm trying to use Parcel to build a library, and am trying to import an index.scss file into my index.js file, it either fails to 'install #parcel/transformer-sass' if I don't include it as a dependency, or 'cannot find module "#parcel/transformer-sass"' when I do install it. Notably, Parcel will build successfully if I import an index.css' file into index.js` instead.
Does anyone know what I'm doing wrong?
Installing npm i #parcel/transformer-scss as a dependency, for which I get the error:
× Build failed.
Error: Failed to install #parcel/transformer-sass: Callback must be a function. Received undefined
Error: Failed to install #parcel/transformer-sass: Callback must be a function. Received undefined
at install
(C:\...\nvm\v14.18.0\node_modules\parcel\node_modules\#parcel\package-manager\lib\installPackage.js:131:11)
at processTicksAndRejections (internal/process/task_queues.js:95:5)
at async PromiseQueue._runFn
(C:\...\nvm\v14.18.0\node_modules\parcel\node_modules\#parcel\utils\lib\PromiseQueue.js:88:7)
at async PromiseQueue._next
(C:\...\nvm\v14.18.0\node_modules\parcel\node_modules\#parcel\utils\lib\PromiseQueue.js:75:5)
Installing #parcel/transformer-sass and #parcel/config-default, for which I receive the error:
× Build failed.
#parcel/core: Cannot find Parcel plugin "#parcel/transformer-sass"
C:\...\nvm\v14.18.0\node_modules\parcel\node_modules\#parcel\config-default\index.json:25:23
24 | "*.{styl,stylus}": ["#parcel/transformer-stylus"],
> 25 | "*.{sass,scss}": ["#parcel/transformer-sass"],
> | ^^^^^^^^^^^^^^^^^^^^^^^^^^ Cannot find module "#parcel/transformer-sass"
26 | "*.less": ["#parcel/transformer-less"],
27 | "*.{css,pcss}": ["#parcel/transformer-postcss", "#parcel/transformer-css"],
I also tried creating a .parcelrc of various configurations, including:
{
"transformers":{".scss":"#parcel/transformer-scss"}
}
and
{
"extends":"#parcel/config-default",
"transformers":{".scss":"#parcel/transformer-scss"}
}
which throw similar errors.
Here's my package.json:
"name": "frontend",
"version": "1.0.0",
"description": "",
"source": "src/index.js",
"targets": {
"default": {
"distDir": "../staticfiles"
}
},
"main": "index.js",
"module": "module.js",
"scripts": {
"watch": "parcel watch",
"build": "parcel build"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"parcel": "latest"
},
"dependencies": {
"#parcel/config-default": "^2.0.0",
"#parcel/transformer-sass": "^2.0.0",
"#popperjs/core": "^2.10.2",
"bootstrap": "^5.1.3",
"bootstrap-table": "^1.18.3",
"jquery": "^3.6.0"
}
}
here's my index.js:
import "./index.scss";
import 'bootstrap';
import 'bootstrap-table';
import $ from "jquery";
console.log($)
window.jQuery = $;
window.$ = $;
console.log("hello world");
$(function() {
$('#table').bootstrapTable()
})
Here's my index.scss:
#import "bootstrap/scss/bootstrap";
#import "bootstrap-table/dist/bootstrap-table";
// #import "bootstrap-table/dist/extenstions/filter-control/bootstrap-table-filter-control";
$body-color: slateblue;
body {
color: $body-color;
font-family: sans-serif;
}
The problem of Callback must be a function. Received undefined occurs when using parcel in a project directory that has a parent directory with an (unrelated) yarn.lock file.
To reproduce run touch ~/yarn.lock and then run parcel in a subdirectory of ~/.
To fix the issue delete any yarn.lock files in parent directories.
I had a problem with running the parcel, probably due to a node.js update, I updated the parcel and installed #parcel/transformer-sass there: https://www.npmjs.com/package/#parcel/transformer-sass.
After that, the parcel works fine.

Import & Export ES6 keywords in Jest & Mozilla extension development

I'm edveloping a Mozilla Firefox extension using Node.js and I'm trying to cover my implementation with unit tests using Jest.
So my package.json looks like so:
{
"name": "keep-your-session",
"version": "0.1.0",
"description": "Mozilla Firefox extension plugin to store and manage browsing sessions",
"main": "manifest.json",
"scripts": {
"test": "jest"
},
"type": "module",
"jest": {
"testEnvironment": "jest-environment-node",
"transform": {}
},
"repository": {
"type": "git",
"url": "git+https://github.com/BartoszKlonowski/keep-your-session.git"
},
"engines": {
"node": ">=13.0.0"
},
"keywords": [
"Firefox",
"Extension",
"Plugin",
"Session"
],
"author": "Bartosz Klonowski",
"license": "GPL-3.0-or-later",
"bugs": {
"url": "https://github.com/BartoszKlonowski/keep-your-session/issues"
},
"homepage": "https://github.com/BartoszKlonowski/keep-your-session#readme",
"devDependencies": {
"#babel/preset-env": "^7.14.7",
"eslint": "^7.28.0",
"jest": "^27.0.5",
"web-ext": "^6.1.0"
}
}
To test some of logic functions I used export syntax to export functions in it and import them in the test implementation file.
Two problems appeared:
1. Jest does not recognize import, export keywords throwing an error:
FAIL __tests__/content.tests.js
● Test suite failed to run
Jest encountered an unexpected token
Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.
...
Details:
P:\Projekty\KeepYourSession\__tests__\content.tests.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import { eventTargetIDToCommand } from '../src/popup/MainPopup';
^^^^^^
SyntaxError: Cannot use import statement outside a module
2. Extension stopped working - it works when there's no export in the file, so obviously it fails to launch the extension due to lack of transpiled/packed sources.
I've searched a lot for this problem and tried to add different plugins of Babel, or configure it in various ways, but obviously there's something missing and I just don't know what.
So my question is:
How to correctly configure my environment to transpile all the sources correctly for Mozilla Firefox extension and for Jest testing?

Mongo DB driver "Cannot find module 'bson'." and no intellisense

I'm setting up a server, and want to use Mongo DB as my Data storage but I am not getting any intellisence from Visual Studio Code whatsoever and adding and removing flow. When I "CTRL + Click" MongoClient I get 2 same errors i.e. Cannot find module 'bson'. when I hover over bson
it shows the location to AppData/Local/Microsoft/TypeScript/3.4/node_modules/#types/bson/index
could not find module bson on line :
`import { ObjectID, Timestamp } from 'bson';`
and line"
export { Binary, DBRef, Decimal128, Double, Long, MaxKey, MinKey, ObjectID, ObjectId, Timestamp } from 'bson';
What I've tried is installing mode and various npm packages you'll see from my package.json file and running npm install, npm update and installing node - gyp for global but still the error has persisted
package.json
{
"name": "MadLibs_SERVER",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"#types/bson": "^4.0.0",
"#types/mongodb": "^3.1.22",
"#types/node": "^11.13.0",
"bson": "^4.0.2",
"express": "^4.16.4",
"mongodb": "^3.2.3",
"node": "^11.13.0"
}
}
Update 1 :
After re installing all modules i.e express and mongodb and removing all others now I still don't get intellisense but opening the file I am getting more errors Cannot find type definition file for 'node'.
now my package has only mongodb and express but still no fix.

Unable to run a node.js file with #babel/preset-env

I'm trying to run a simple node.js file which needs the #babel/preset-env preset. The moment I run the js file, I get a message saying
Requires Babel “7.0.0-0” but was loaded with “6.26.3”
To replicate the issue, please try the following in a new folder:
1. Run the following commands
npm init
npm install #babel/register
npm install #babel/core#^7.2.2
npm install #babel/preset-env
Create a .babelrc file with the following
{
"presets": ["#babel/preset-env"],
"plugins": []
}
Create a sample emp.jsx with the following
import React from "react";
class CommentBox extends React.Component {}
Create a parse.js file with the following
require('babel-register')({presets: ['env', 'react']});
let Emp = require('./emp.jsx');
Now run the parse.js file by running
node parse.js
You should see the error mentioned above. I have been trying to fix with for a long time now. Please help.
Many Thanks
followed your instructions and using #babel/register instead with
this package.json run with no issues
tasted on
node : v8.11.2
yarn : 1.12.3
paese.json
require('#babel/register')({});
let Emp = require('./emp.jsx');
console.log(Emp)
packge.json
{
"name": "sof",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"devDependencies": {
"#babel/core": "^7.2.2",
"#babel/preset-env": "^7.2.3",
"#babel/register": "^7.0.0"
},
"dependencies": {
"react": "^16.7.0"
}
}
Found the problem. The .babelrc file that contained a reference to #babel/preset-env. Removed it and the js file compiled just fine.

unexpected token import in ES2017 with babel and Jest

I try to use Jest with bablejs and ES2017 in my project, according to the Jest Getting Started page and also Bablejs config for ES2017 this is my .babelrc file :
{
"presets": ["es2017"],
"env": {
"test": {
"presets": ["es2017"]
}
}
}
And my package.json is:
{
"name": "",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "jest"
},
"repository": {
"type": "git",
"url": ""
},
"author": "",
"license": "ISC",
"bugs": {
"url": ""
},
"homepage": "",
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-core": "^6.26.0",
"babel-jest": "^21.2.0",
"babel-polyfill": "^6.26.0",
"babel-preset-es2017": "^6.24.1",
"jest": "^21.2.1"
}
}
When I type npm test to run all my test with jest i get these error :
){import StateList from './StateList';
^^^^^^
SyntaxError: Unexpected token import
It means it doesn't know import.
babel-preset-es2017 does not transform import statements, because it only includes the plugins: syntax-trailing-function-commas and
transform-async-to-generator.
When installing babel-preset-es2017 you also get a warning that it has been deprecated in favour of babel-preset-env, which contains everything that the es201x presets contained and more.
warning babel-preset-es2017#6.24.1: 🙌 Thanks for using Babel: we recommend using babel-preset-env now: please read babeljs.io/env to update!
As shown in the Migration guide from es2015 to env, it is a drop-in replacement.
npm install --save-dev babel-preset-env
And change your .babelrc to:
{
"presets": ["env"]
}
Do not confuse babel-preset-env with Babel's env option, which I have removed from your current config, since you are using the exact same presets for the test environment as for any other, so it doesn't have any effect.
You can configure babel-preset-env to only transform features that are not supported by the platform you target, for example { "targets": { "node": "current" } } will only transform features that aren't supported by the Node version you are running. If no targets are specified, it will transform everything. For details see the Env preset documentation.
Note: With the upcoming version 7 of Babel, the official packages will be published under the namespace #babel, which means that babel-preset-env will be #babel/preset-env.

Resources