SyntaxError: Unexpected token import - import * as gulp from 'gulp' - node.js

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

Related

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...

Why do I keep getting this Node SyntaxError after a while?

Can anyone help me with this SyntaxError?
I keep getting it on various paths not only mongoose, sometimes is bluebird sometimes another module.
Tried and deleted the node_modules folder and reinstall the npm modules which fixes the issue but after a couple of runs of the script, it appears again.
I honestly can't explain why it works after reinstalling the node_modules and after a few runs, it doesn't anymore.
Node: v9.8.0
NPM: 5.7.1
node_modules/mongoose/lib/options.js:1
(function (exports, require, module, __filename, __dirname) {
SyntaxError: Invalid or unexpected token
at new Script (vm.js:51:7)
at createScript (vm.js:136:10)
at Object.runInThisContext (vm.js:197:10)
at Module._compile (module.js:613:28)
at Object.Module._extensions..js (module.js:660:10)
at Module.load (module.js:561:32)
at tryModuleLoad (module.js:501:12)
at Function.Module._load (module.js:493:3)
at Module.require (module.js:593:17)
at require (internal/module.js:11:18)
You are having that SyntaxError because you have some broken files in your npm installations probably due to a fluctuation in your internet or any other reason. And you still got that same error after deleting the node_modules folder and re-install the npm modules again because of the same thing I said.
So your problem got fixed that last time because after you re-installed all the required dependencies ere fully installed in your node_modules.

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)

Testing with enzyme and mocha -- "Unexpected token import"

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"]
}

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