babel / example-node-server Goto Github PK
View Code? Open in Web Editor NEWExample Node Server w/ Babel
License: ISC License
Example Node Server w/ Babel
License: ISC License
Hello,
It says the following in README.md
:
So we've cheated a little bit by using babel-node. While this is great for getting something going. It's not a good idea to use it in production.
We should be precompiling your files, so let's do that now.
I'm curious, why exactly is it not a good idea? Isn't the code getting executed the same? And as servers are generally longer-running then clients, the code doesn't get recompiled so often but only when the app crashes or gets redeployed. I see the advantages of pre-compiling on the client side, but I'm not sure why it's necessary on the server side. Could someone elaborate?
Correct me if I am wrong but looking at babelrc file, this example will transpile ES6 to ES5. Transforming ES6 features into ES5 for NodeJS is basically exchanging superior codes for inferior ones.
The example should not transpile ES6 codes down to ES5.
Would be nice to see sourcemap configuration for use in something like node-inspector
Since i updated to latest beta,
"babel-cli": "^7.0.0-beta.3",
"babel-core": "^7.0.0-beta.3",
"babel-node": "^7.0.0-beta.3",
"babel-preset-env": "^7.0.0-beta.3",
In my .babelrc I have "presets": ["env", "next/babel"]
I note in the current release v6 everything works fine but not in v7.
When I follow these instructions to the letter, everything works until the step where we move things over to a .babelrc file and remove the options from the scripts. Then I get a
[nodemon] starting `babel-node lib/index.js`
/usr/local/lib/node_modules/babel/node_modules/babel-core/lib/transformation/file/options/option-manager.js:126
if (!option) this.log.error("Unknown option: " + alias + "." + key, ReferenceError);
^
TypeError: Cannot read property 'error' of undefined
at OptionManager.mergeOptions (/usr/local/lib/node_modules/babel/node_modules/babel-core/lib/transformation/file/options/option-manager.js:126:28)
I did it step by step according to the tutorial
But there is an error here
How should I do, Thanks
For anyone who has problems getting source maps to work for devel, here is a solution that I just came up with. I am using Node 5.3.
Install the following packages:
$ npm install --save babel-cli babel-preset-es2015 source-map-support
$ npm install --save-dev nodemon
In this example I am using Heroku for production. The reason I put most of the packages in "dependencies" instead of in "devDependencies" to enable Heroku to transpile the js files when it's deployed (so we don't have to check in any dist files). If you don't use Heroku you can use --save-dev
for all the packages.
In your index file (index.js, app.js worker.js etc), add:
import 'source-map-support/register';
Important If you have previously used babel-register hook (eg require('babel-register')
) make sure to remove it or it will skew the line numbers in stack traces.
package.json - The postinstall script transpiles all js files and put the result in dist/. postinstall will run after any npm install
, which Heroku also run when deploying a new version of your app. The start-web and start-worker scripts are for development use and simply tell nodemon to run postinstall (eg. transpiling) on every reload, the transpiled output is then executed.
{
"scripts": {
"start-web": "nodemon dist/app.js --ignore dist/ --exec 'npm run postinstall && node'",
"start-worker": "nodemon dist/worker.js --ignore dist/ --exec 'npm run postinstall && node'",
"postinstall": "babel *.js -d dist"
}
}
Now you can npm run start-web
locally and it will update as files changes, with the correct line numbers in stack traces:
$ npm run start-web
$ npm run start-worker
$ heroku local # Simulate Heroku env locally (no file watch)
Procfile (Heroku only) - since Heroku automatically run the postinstall npm script at deploy time we can simply use vanilla node to run the transpiled files (eg dist/app.js and dist/worker.js):
web: node --optimize_for_size --max_old_space_size=460 --gc_interval=100 dist/app.js
worker: node --optimize_for_size --max_old_space_size=460 --gc_interval=100 dist/worker.js
.gitignore - make sure to exclude the dist folder before committing and deploying to Heroku:
node_modules
dist
I'm not sure if I am missing something, but this is the only way I could source maps and file watcher to work for production and dev, without using additional tools like gulp or such. Curious to if there is a simpler way (is it possible to use source maps with babel-node for example?).
What's going on?
"scripts": {
"start": "nodemon server/index.js --exec babel-node --presets es2015,stage-0"
},
I think the readme.md
file is wrong.
The correct script is:
"scripts": {
"start": "nodemon server/index.js --exec babel-node --presets=es2015,stage-0"
},
=
symbol
When getting the error:
Running "babel:dist" (babel) task
Warning: Path must be a string. Received undefined Use --force to continue.
The solution is to change the dist object from src to path
dist: { path: { './server.js': 'src/app.js' } }
At least in versions
"babel-preset-es2015": "^6.24.1", "babel-preset-stage-2": "^6.24.1", "grunt-babel": "^6.0.0"
Also need to change the example.
Can we do that way in production without falling in performance pit trap?
// enable runtime transpilation to use ES6/7 in node
/* eslint-disable */
var fs = require('fs');
var path = require('path');
var babelrc = fs.readFileSync(path.resolve(__dirname, '../.babelrc'));
var config;
try {
config = JSON.parse(babelrc);
} catch (err) {
console.error('==> ERROR: Error parsing your babelrc');
console.error(err);
}
/* eslint-enable */
require('babel-core/register')(config);
got example from https://github.com/bdefore/universal-redux/blob/master/bin/transpile.js
It would be good to make a code coverage tool to work with babel. I have tried to configure both istanbul, babel-istanbul/isparta to get a correct code coverage report, but i havenยดt be able to get the correct code references on the report. I didnยดt even know if the coverage metrics are right.
Could you provide an example for this setup?
There is another coverage tool besides istanbul which plays better with babel?
Thank you!
Like all the rest of babels documentation: babel6 and unusuable in babel7 times.
Is there a list with documentation that needs rework? I'd be happy to contribute here and there
Newer version does not need explicit setting of babel-preset-es2015 babel-preset-stage-2
. Simply using babel-preset-env
in place of babel-preset-es2015 babel-preset-stage-2
will suffice.
Everything else seems fine.
Thanks for writing.
For now I just restart manually after making changes because babel doesn't automatically transpile original code when changes are made.
So basically I feel getting no benefits from using nodemon from this setup.
Should I use another library for watching purpose?
Hi!
I'm deploying my app in a elastic bean instance so basically I have the option to run just one script.
If I try to run something like:
"start": "npm run build & node script.js"
it's not guaranteed that the first one has finished before the second one starts. Is there an way to pass through that?
Ps: I'm using eb deploy from ElasticBean
Thanks
Not an issue; just a recommendation (maybe).
If the sourcemaps are modified slightly, you can get Webstorm and Babel to work very well in the same folder. This way, you can potentially avoid the /dist
folder. In fact, the Webstorm Debugger works on breakpoints on either the compiled or the es6 files. NOTE: Using find/replace (naive I know), we can make sure that all import '*.es6'
file dependencies are changed to the import '*.compiled.js'
files.
refId=4GFnsQoznrdysdG5rxPtYA%3D%3D&trackingId=%2F7GLnoCt9PfJM4J3oPSOaw%3D%3D&position=17&pageNum=0&trk=public_jobs_jserp-result_search-card
... after npm install --save-dev babel-cli babel-preset-env
babel
is still not available in CL. Instead i see below output.
root@socket-server-ZZ:~/projects/my_app# babel src -d dist/
The program 'babel' can be found in the following packages:
* babel-1.4.0
* openbabel
Try: apt install <selected package>
Say index.js
imports a date utility function from ../utils/date
. If we run babel-node lib/index/js
, that will work. But the serve script, node dist/index.js
will no longer work, because utils/date.js
has an export
statement, which Babel didn't transpile.
We could have utils/src/date.js
transpiled to utils/lib/date.js
, but then index.js
needs to be changed to explicitly import utils/lib/date
, and babel-node lib/index.js
alone will no longer work correctly for development because utils/lib/date.js
is outdated vs. utils/src/date.js
.
@thejameskyle, what would be the best practice here?
Jdjdjdudi@dandv i@loganfsmyth
Would be useful to me. Where a project would normally use something like:
"bin": {
"my-app": "bin/my-app.js"
},
You're not using nodemon
or an npm run
script. Would you just not compile the entry point script?
We could add to this README or add another example-node-cli
or something of the sort.
Can someone please update the documentation on how to use this with Babel 7. thank you
First of all... thanks. ๐๐ฟ
I had modified the port, now it is running on port 8000. Also, I had to change the test final route, now is pointing to the 8000 and not into the
http.get('http://127.0.0.1:1337', res => {
.
And everything looks fine, except that when I run the test and log the output to check if it is okay, it returns that is okay... but assert doesn't notice it.
`
server on port 8000 ready
Example Node Server
๐
๐ปโโ๏ธ 200
1) should return 200
0 passing (30ms)
1 failing
`
Please update this sample to get it working using the new babel6 stuff.
Thanks
Hi, thanks for you example.
I have a question. When I added my additional module via
import test from './test.jsx';
with content
export default 1;
babel compile it to
var _test = require('./test');
var _test2 = _interopRequireDefault(_test);
and it does not work because node does not understand export default 1;
.
Babel somehow resolve dependencies for production? Or I should use webpack for example?
I love the set up but I was wondering where can I deploy a node server like this one ?
Please correct me if I interpret the section "Watching file changes with nodemon" wrong.
What I found in this section is that after issue npm start
, the project won't rebuild when index.js is modified, i.e. when I modified the greetings, it was not updated when I refresh the page. The script seems only to build the project once when the command is issued.
Following script works for me:
"start": "nodemon --exec \"npm run build && node dist/index.js\" -e js --ignore dist/"
What if for example, it will require ./util/something
which contains ES6 code, it doesn't seem to compile it together?
In the section of 'Watching file changes with nodemon', I find something wrong. If change file 'index.js', nodemon will indeed restart the server. But the 'build' script will not rerun automatically. So if we refresh the browser, we will find the page will not change as we expected.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.