Testing with enzyme and mocha -- "Unexpected token import" - node.js

I'm trying to run the Enzyme/Mocha example project at https://github.com/lelandrichardson/enzyme-example-mocha. I cloned the project off of Github and ran npm install. When I ran mocha without making any other changes to the project, this is the output I received:
/Projects/enzyme-example-mocha/test/Foo-test.js:1
(function (exports, require, module, __filename, __dirname) { import React from 'react';
^^^^^^
SyntaxError: Unexpected token import
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:511:25)
at Object.Module._extensions..js (module.js:550:10)
at Module.load (module.js:456:32)
at tryModuleLoad (module.js:415:12)
at Function.Module._load (module.js:407:3)
at Module.require (module.js:466:17)
at require (internal/module.js:20:19)
My node --version is v6.1.0 and my mocha --version is 2.4.5.
How do I get this project to transpile into something Node/Mocha can run? Thanks.

Did you try npm run test? I saw the test script in package.json (in the "scripts" section), ran it, and it worked the first time.
The difference with npm run test vs just mocha is the former includes the file test/.setup.js, which requires babel-register and executes it, which is essentially what you were saying about missing a transpile step.

You can add the preset 2 setting in your .babelrc.
$ npm install --save-dev babel-preset-stage-2
Add the following line to your .babelrc file:
{
"presets": ["stage-2","react","es2015"]
}

Related

How to uninstall a node module completely?

UPDATE
I don't have a problem with my ionic module, but with my node install(s).
It seems I have 4 node installs:
running npm start on a createreactapp installed with v15.6.0, I get:
so somehow it is using node 4.5.0.
What’s going on?
Original post:
I have a module, that is malfunctioning. Uninstalling and reinstalling it ultimately results in the same error. And I can see, that uninstalling it doesn't get rid of all the files.
Specifics:
The module in question is Ionic
The error I'm getting ever since I tried to update it is:
/usr/local/lib/node_modules/#ionic/cli/node_modules/semver/internal/re.js:1
(function (exports, require, module, __filename, __dirname) { const { MAX_SAFE_COMPONENT_LENGTH } = require('./constants')
^
SyntaxError: Unexpected token {
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:373:25)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (/usr/local/lib/node_modules/#ionic/cli/node_modules/semver/index.js:2:20)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
it is shown whenever I run an ionic command, even ionic --help
(btw. this line, that is shown from rs.js is the first line of the file)
I've tried uninstalling it with npm uninstall -g #ionic/cli
and npm uninstall -g ionic
(I also once accidentally typed npm uninstall - ionic. and it did something...)
And reinstalling it with npm install -g #ionic/cli
Tried this several times.
I've also updated node to 15.6.0.
--
I have to admit I'm generally a bit confused about node modules and where they live.
Because for Ionic there are files (I'm using MacOs 11.2.3) in /usr/local/lib/node_modules/#ionic/
But also in [user folder]/node_modules/#ionic
And there's a .ionic in the user folder.
Perhaps there is something I don't understand
How do I go about issues such as this?

gulp syntaxe error after installation

Ok so I have a problem installing gulp in my project. I followed the steps on the installation guide but I get a SyntaxError when trying to run their default example.
I just installed gulp with : npm install --global gulp-cli and npm install --save-dev gulp
I created a file named gulpfile.js at the parent of my project and then added the following code to the file :
var gulp = require('gulp');
gulp.task('default', function() {
// place code for your default task here
});
Then I runed gulp in powershell located at the parent of my project expecting to execute nothing because there is no code in my task but I received the following error :
PS D:\Users\UserName\project> gulp
D:\Users\UserName\project\gulpfile.js:1
(function (exports, require, module, __filename, __dirname) { ��v
^
SyntaxError: Invalid or unexpected token
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 execute (C:\Users\UserName\AppData\Roaming\npm\node_modules\gulp-cli\lib\versioned\^3.7.0\index.js:28:18)
PS D:\Users\UserName\project>
My gulp version after installation are :
[18:56:36] CLI version 2.0.1
[18:56:36] Local version 3.9.1
and my npm version is :
5.6.0
Note that I also tried with actuall task and got the same result
I would like to know what I did wrong or what I need to do to resolve the problem.
As Felix Kling said in the comments, there was strange characters in the beginning of my gulpfile.
Indeed, when I copied and pasted the code, it added the hindden caracter for Control-V...

How do I know if my nodeJS library has been compiled using Babel?

I have a nodeJS script that contains the following lines
import StratumClient from "node-stratum-client"
const client = new StratumClient()
I would like to get the script to work as is, without changing the first line, which is causing this error
(function (exports, require, module, __filename, __dirname) { import StratumClient from "node-stratum-client"
^^^^^^
SyntaxError: Unexpected token import
So I was following the instructions here -- Node error: SyntaxError: Unexpected token import . Below is the output
localhost:node-stratum-client satishp$ npm install --save-dev babel-cli babel-preset-es2015
npm WARN deprecated babel-preset-es2015#6.24.1: 🙌 Thanks for using Babel: we recommend using babel-preset-env now: please read babeljs.io/env to update!
npm WARN node-stratum-client#0.0.1 No repository field.
+ babel-cli#6.26.0
+ babel-preset-es2015#6.24.1
added 46 packages in 4.642s
localhost:node-stratum-client satishp$ echo '{ "presets": ["es2015"] }' > .babelrc
localhost:node-stratum-client satishp$ ./node_modules/.bin/babel src -d lib
src/StratumClient.js -> lib/StratumClient.js
src/StratumClient.test.js -> lib/StratumClient.test.js
src/example.js -> lib/example.js
src/index.js -> lib/index.js
However, when I go to run my script again, I get the same error. I'm curious if I did something wrong above. How do I know if my script was compiled using babel?
localhost:node-stratum-client satishp$ npm install
npm WARN node-stratum-client#0.0.1 No repository field.
up to date in 2.505s
localhost:node-stratum-client satishp$ node run.js
/Users/satishp/Documents/workspace/node-stratum-client/run.js:1
(function (exports, require, module, __filename, __dirname) { import StratumClient from "node-stratum-client"
^^^^^^
SyntaxError: Unexpected token import
at new Script (vm.js:51:7)
at createScript (vm.js:138:10)
at Object.runInThisContext (vm.js:199:10)
at Module._compile (module.js:624:28)
at Object.Module._extensions..js (module.js:671:10)
at Module.load (module.js:573:32)
at tryModuleLoad (module.js:513:12)
at Function.Module._load (module.js:505:3)
at Function.Module.runMain (module.js:701:10)
at startup (bootstrap_node.js:193:16)

SyntaxError: Unexpected token import - import * as gulp from 'gulp'

If you run gulp, you will get the following error message.
Is module required for gulpfile not installed? ..
$ gulp
[22:17:15] Failed to load external module ts-node/register
[22:17:15] Failed to load external module typescript-node/register
[22:17:15] Failed to load external module typescript-register
[22:17:15] Failed to load external module typescript-require
C:\project\interview\gulpfile.ts:1
(function (exports, require, module, __filename, __dirname) { import * as gulp f rom 'gulp';
^^^^^^
SyntaxError: Unexpected token import
at createScript (vm.js:56:10)
at Object.runInThisContext (vm.js:97:10)
at Module._compile (module.js:542:28)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Liftoff.handleArguments (C:\Users\admin\AppData\Roaming\npm\node_modules\gu lp\bin\gulp.js:116:3)
There are 3 things you need to do to make gulp work with ES6 imports:
Rename gulpfile.js to gulpfile.babel.js
Run this command:
npm i #babel/preset-env #babel/register -D
Add a .babelrc file to the root folder of your project with the following code in it:
{
"presets": [
"#babel/preset-env"
]
}
You should now be able to use ES6 imports in your Gulp files.
Note: if you want to use TypeScript in your Gulp files instead, you should follow the steps outlined here:
https://stackoverflow.com/a/49849088/1611058
1) Create a ".babelrc" file in your Projekt folder (where also all your other config files are)
Code (.babelrc)
{
"presets": ["es2015"]
}
2) Setup/Install the Babel in your NPM (see https://babeljs.io/setup). E. g. with Node:
npm install babel-preset-env --save-dev

Unexpected String Error Require Express Not Getting Found in JS App file

I have a js web app that is written in express, node, socket io and coffeescript. I am trying to get the local server started on my local machine.
I went to the directory that contained the package.json file and ran npm install to install all the dependencies which included express.
when I run npm list I do see express there.
When I try to start the app with
node app.coffee inside the directory of where the app.coffee is located
its given me this error
exports, require, module, __filename, __dirname) { express = require 'express'
^^^^^^^^^
SyntaxError: Unexpected string
at Module._compile (module.js:439:25)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:901:3
I made sure I have node by doing
which node and I installed coffee script on my machine with
sudo npm install -g coffee-script
I even tried
coffee app.coffee
I am not sure what else I need to check. The app.coffee is in my nodejs folder.
Simply run
coffee app.coffee -n
You can find out more info here

Resources