How to compile and run an ES6 file with node when using babel6? - babeljs

I installed the latest version 6 of babel, babel-core and babel-loader.
How can I run an ES6 file in Node with Babel6?
Previously I would run the command
babel-node server.js
but now I get this message:
The CLI has been moved into the package `babel-cli`. See http://babeljs.io/docs/usage/cli/.
None of the instructions on that page say how to do this.

The message could be clearer. You've installed the babel package and you should have installed the babel-cli package.
npm uninstall babel
npm install babel-cli

Upon installing babel-cli I also had to specify the es2015 loader and to specifically use my local babel-node package since I don't have it installed globally.
./node_modules/.bin/babel-node --presets es2015 server.js

Related

Trying to run babel : "cannot find module #babel/core"

I'm trying to set up a build environment to explore react.
My build process uses gulp.
I installed packages like this:
npm install --save-dev gulp-babel#7 babel-core babel-preset-env
After discovering that I needed to install something related to babel and react I also ran:
npm install --save-dev #babel/preset-react
My .babelrc has this:
{ "presets": ["#babel/preset-react"] }
My gulpfile has this:
gulp.task('scripts', function() {
return gulp.src(['./src/js/main.js' ])
.pipe(babel({
presets : ['#babel/preset/react']
}))
.pipe(concat('test.js'))
.pipe(gulp.dest('./js'))
.pipe(uglify())
.pipe(rename('test.min.js'))
.pipe(gulp.dest('./js')) ;
});
When I run 'gulp scripts' I get this:
[22:51:14] Using gulpfile ~/play/learning-react-2/gulpfile.js
[22:51:14] Starting 'scripts'...
[22:51:14] 'scripts' errored after 59 ms
[22:51:14] Error in plugin "gulp-babel"
Message:
Cannot find module '#babel/core' (While processing preset: "/home/bob/play/learning-react-2/node_modules/#babel/preset-react/lib/index.js")
I deleted babel-core from node_modules, and reinstalled it using the command:
npm install --save-dev #babel/core
If I look at the contents of node_modules, I see these packages related to babel:
babel-code-frame/
babel-helper-builder-binary-assignment-operator-visitor/
babel-helper-call-delegate/
babel-helper-define-map/
babel-helper-explode-assignable-expression/
babel-helper-function-name/
babel-helper-get-function-arity/
babel-helper-hoist-variables/
babel-helper-optimise-call-expression/
babel-helper-regex/
babel-helper-remap-async-to-generator/
babel-helper-replace-supers/
babel-messages/
babel-plugin-check-es2015-constants/
babel-plugin-syntax-async-functions/
babel-plugin-syntax-exponentiation-operator/
babel-plugin-syntax-trailing-function-commas/
babel-plugin-transform-async-to-generator/
babel-plugin-transform-es2015-arrow-functions/
babel-plugin-transform-es2015-block-scoped-functions/
babel-plugin-transform-es2015-block-scoping/
babel-plugin-transform-es2015-classes/
babel-plugin-transform-es2015-computed-properties/
babel-plugin-transform-es2015-destructuring/
babel-plugin-transform-es2015-duplicate-keys/
babel-plugin-transform-es2015-for-of/
babel-plugin-transform-es2015-function-name/
babel-plugin-transform-es2015-literals/
babel-plugin-transform-es2015-modules-amd/
babel-plugin-transform-es2015-modules-commonjs/
babel-plugin-transform-es2015-modules-systemjs/
babel-plugin-transform-es2015-modules-umd/
babel-plugin-transform-es2015-object-super/
babel-plugin-transform-es2015-parameters/
babel-plugin-transform-es2015-shorthand-properties/
babel-plugin-transform-es2015-spread/
babel-plugin-transform-es2015-sticky-regex/
babel-plugin-transform-es2015-template-literals/
babel-plugin-transform-es2015-typeof-symbol/
babel-plugin-transform-es2015-unicode-regex/
babel-plugin-transform-exponentiation-operator/
babel-plugin-transform-regenerator/
babel-plugin-transform-strict-mode/
babel-preset-env/
babel-runtime/
babel-template/
babel-traverse/
babel-types/
I'm guessing that at least one of them is "babel core"
So... how do I actually run babel from gulp? How do I run it at all?
I had the same issue. I resolved it by removing node_modules and I reinstalled packages (with yarn OR npm install). The problem was fixed.
In case Victor's answer doesn't help, it may be worth a try to clean your machine's package cache by running npm cache clean/yarn cache clean or removing the cache folder manually.

How do I check which babel version is on my windows 10 OS?

I need help to find which version of Babel is on my Windows Machine. I have installed it using
npm install --save-dev babel-cli babel-preset-env.
How do I check which version is on my Windows OS?
After you finish installing Babel, your package.json file should look like this:
{
"name": "my-project",
"version": "1.0.0",
"devDependencies": {
"babel-cli": "^6.0.0"
}
}
So you can find your Babel version in this configuration file.
Try this at the command line:
npm list babel-cli
You can also check the version of babel-cli by finding the babel-cli folder in node_modules and looking at the version property of the package.json that is at the base of that folder.
If babel-cli was installed globally via -g flag of npm install, you could check the version by executing command babel --version.
Hopefully the helps!
As the babel is updated to babel 7,
check using
npm list #babel/cli
or
npm list #babel/core
possibly your local ./node_modules/.bin is not in $PATH check out
this previous question for further info.
babel --version
You can figure this out by typing in the command line:
babel --help, look over the output and you can see other options that you might need.
Good luck

How to verify facebook jest is installed successfully

I followed the instructions to install Facebook jest on https://facebook.github.io/jest/docs/getting-started.html#content :
npm install --save-dev jest-cli
After the install command I typed jest in the terminal, and press enter but It popped:
bash: jest: command not found.
But when I run the getting started sample by using npm test in the terminal, it worked well.
So, how can I verify that Facebook jest is installed successfully?
Ways to install a package in npm
In node.js you have two ways to install a package: globally or locally.
The sintax is the following:
// globally
npm install -g [package_name]
// locally
npm install --save-dev [package_name]
So, now what it happens is that you run the local one which downloads the package in node_modules under your project folder.
To check you installed jest properly so you can check on your node_modules if there is a jest folder.
How to check if jest is installed
In addition to that npm is creating a shortcut in you local node_modules under the directory .bin son in there you should find a link to jest.
You can test that like that:
cd your_project_folder
./node_modules/.bin/jest
Why npm test works?
The reason why npm test works is because when you run it npm is going to look for the commands globally and locally.

How to use sinon on karma?

I'm trying to use karma, chai and sinon to test spy.
I did:
npm install karma-sinon --save-dev
I added:
frameworks: ['mocha','chai','chai-sinon']
I run:
karma start
But I'm getting this error:
Error: No provider for "framework:chai-sinon"! (Resolving: framework:chai-sinon)
I had the same problem by simply adding the chai entry to the Frameworks section of my karma.conf.js script did not solve the problem. Following this Github Issue I found that you need to install the karma-cli globally, and everything else can be local and work.
npm install --save-dev karma
npm install -g karma-cli
npm install --save-dev karma-phantomjs-launcher karma-chai karma-sinon
During going through PluralSight course on Gulp I've come into the same problem.
This issue was due to lack of npm modules that are required. The string that helped me is this:
npm install --save-dev karma karma-chai karma-chai-sinon karma-chrome-launcher karma-coverage karma-growl-reporter karma-mocha karma-phantomjs-launcher karma-sinon mocha mocha-clean sinon-chai sinon phantomjs
Initially I've misspelled the karma-chai-sinon as karma-chai sinon so it successfully installed every dependency but resulted in the error.
So please try it. I've got the same error.
Did you add the 'karma-chai-sinon' to your plugins in your karma.conf.js
My plugins looks like this:
plugins: [ 'karma-chai-sinon', 'karma-mocha', 'karma-phantomjs-launcher', 'karma-babel-preprocessor' ]
I've had the same problem by simply adding the chai entry to the Frameworks section of my karma.conf.js script did not solve the problem, it's the same other frameworks that you point into frameworks array.
Have you installed the "karma-sinon-chai" npm package
Have you tried it?
e.g. npm install karma-simon-chai --dev-save

ember-cli ignores my coffeescript test files

I'm an ember-cli newbie and I'm trying to start testing my dumb app with some dumb tests. It seems that ember-cli ignores any *test.js.coffee files in tests directory.
I'm using version 0.0.44 and installed coffescript adapter with npm install --save-dev ember-cli-coffeescript
Any idea why?
Did you install the coffeescript module?
npm install --save-dev ember-cli-coffeescript