unexpected token import in ES2017 with babel and Jest - node.js

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.

Related

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?

custom npm package not found in my service

There is a similar post here:
My custom NPM Package is not found,
but that did not resolve my issue.
Could not find a declaration file for module '#dc_microurb/common'.
'/Users//Projects/ticketing/auth/node_modules/#dc_microurb/common/build/index.js'
implicitly has an 'any' type. Try npm install #types/dc_microurb__common if it exists or add a new declaration
(.d.ts) file containing `declare module '#dc_microurb/common';
There is no #types/dc_microurb__common and I am unclear as to why it is suggesting to create a new .d.ts file, when that happens automatically during the build process.
This is the package.json file inside my published package:
{
"name": "#dc_microurb/common",
"version": "1.0.5",
"description": "",
"main": "./build/index.js",
"types": "./build/index.d.ts",
"files": [
"./build/**/*"
],
"scripts": {
"clean": "del ./build",
"build": "npm run clean && tsc",
"pub": "git add . && git commit -m \"Updates\" && npm version patch && npm run build && npm publish"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"del-cli": "^3.0.1",
"typescript": "^4.0.5"
},
"dependencies": {
"#types/cookie-session": "^2.0.41",
"#types/express": "^4.17.9",
"#types/jsonwebtoken": "^8.5.0",
"cookie-session": "^1.4.0",
"express": "^4.17.1",
"express-validator": "^6.6.1",
"jsonwebtoken": "^8.5.1"
}
}
Anybody with experience in publishing packages on npm where TypeScript is involved? I am unclear as to why this package is not being found by my auth service when it is successfully installed inside that service.
Did I mess up in the way I am exporting inside my common/src/index.ts file?
export * from './errors/bad-request-error';
export * from './errors/custom-errors';
export * from './errors/database-connection-error';
export * from './errors/not-authorized-error';
export * from './errors/not-found-error';
export * from './errors/request-validation-error';
export * from './middlewares/current-user';
export * from './middlewares/error-handler';
export * from './middlewares/require-auth';
export * from './middlewares/validate-request';
Does it all have to be inside a module.exports instead?
So after reviewing a few blogs on Medium on how this is done, I noticed a couple of things.
Inside my tsconfig.js, this was missing:
/* Advanced Options /
"forceConsistentCasingInFileNames": true / Disallow inconsistencies */
So I manually added it in, secondly, I noticed something I meant to remove earlier but forgot. So instead of this:
{
"name": "#dc_microurb/common",
"version": "1.0.5",
"description": "",
"main": "./build/index.js",
"types": "./build/index.d.ts",
"files": [
"./build/**/*"
],
I needed to have it like this:
{
"name": "#dc_microurb/common",
"version": "1.0.6",
"description": "",
"main": "./build/index.js",
"types": "./build/index.d.ts",
"files": [
"build/**/*"
],
After making those couple of corrections, now my package is available to my auth service.

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.

Creating a Node NPM module in 9.2.0 to support older versions of Node

Now that Node 9.2.0 has all the new features of the language, how do I go about creating a node module that is backwards compatible with older versions?
If I have a small module that Node 9 supports out of the box, like this.
const {map} = require('lodash')
async function test (...args) {
return map(args, (item) => {
return `${item} yeah`
})
}
module.exports = test
Are there any was to use babel to transpile this for the specific backward version that I would need to support using babel env? Is there any way I can conditionally load those babel development dependencies, say installing this via Node 4 using post-install scripts?
It seems like this is one solution one downside of which is it requires babel-runtime as a dep just in case, even if the current version of node doesn't need it. But in 9.2.0 the code above is the built code, it's simply moved by babel.
Here's an example package.json and on install it will build the src.
{
"name": "example",
"version": "1.0.0",
"main": "lib/index.js",
"scripts": {
"build": "babel src -d lib",
"postinstall": "npm run build"
},
"dependencies": {
"babel-runtime": "^6.26.0",
"lodash": "^4.17.4"
},
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-env": "^1.6.1"
},
"babel": {
"plugins": [
"transform-runtime"
],
"presets": [
[
"env",
{
"targets": {
"node": "current"
}
}
]
]
}
}

Force Browserify to transform dependencies?

I'm working on two Node packages at once, let's call them Library and Consumer. Library is responsible for rendering a bunch of stuff in the browser. All Consumer does is import Library from 'library' and call Library(someConfigHere) -- it's basically just a test to make sure Library is doing what I expect in the browser.
I've npm linked Library into Consumer and am trying to run Browserify on Consumer, but I get this error: ParseError: 'import' and 'export' may appear only with 'sourceType: module'. Library does indeed contain an ES6 export statement, so I'm guessing that Browserify is only running against Consumer and not Library.
So my question is: is there any way to force Browserify to transform dependencies as well?
This is my package.json:
{
"name": "consumer",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "budo index.js --port $PORT",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"devDependencies": {
"babel-preset-es2015": "^6.13.2",
"babel-preset-react": "^6.11.1",
"babelify": "^7.3.0",
"browserify-shim": "^3.8.12"
},
"browserify": {
"transform": [
"babelify"
]
},
"babel": {
"presets": [
"es2015",
"react"
]
}
}
This is Consumer's index.js:
import Library from 'library' // <= this is what isn't getting babelified
console.log(Library);
This is Library's index.js:
export default (config) => {
console.log('Testing testing')
}
Browserify transforms can be configured to be global, which means they will be applied to files within node_modules, too.
The configuration is per-transform. With babelify, you'd configure it like this:
browserify().transform("babelify", {
global: true
})
Or, if you are using the command line, like this:
browserify ... -t [ babelify --global ] ...
Or, to configure it in the package.json, it should be something like this (note the added square brackets):
"browserify": {
"transform": [
["babelify", { "global": true }]
]
}
Babelify also implements an ignore option, so it would be possible to configure it to transform only the files within node_modules that you want it to. There is more information here.
Another solution would be to include a similar browserify/babelify configuration in your library module's package.json. When processing dependencies, Browserify will check said dependency's pacakge.json files for transforms and will apply any that are configured.

Resources