Use import keyword without Babel in Node 6 - node.js

I am wondered why Node.js node index.js raises the following error :
(function (exports, require, module, __filename, __dirname) { import * as math from "lib/math";
^^^^^^
SyntaxError: Unexpected token import
at Object.exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:513:28)
at Object.Module._extensions..js (module.js:550:10)
at Module.load (module.js:458:32)
at tryModuleLoad (module.js:417:12)
at Function.Module._load (module.js:409:3)
at Function.Module.runMain (module.js:575:10)
at startup (node.js:160:18)
at node.js:456:3
Even , node -v shows 6.2.1 .
Does import keywords is restricted with Babel , even we use Node 6 (6.2.1) ?

ES 2015 Modules (and therefore the import keyword) are not supported by default in Node.js v6.
You can enable experimental (and almost certainly buggy) module support by launching node with the --harmony_modules flag.
A better option might be to use Babel or a similar tool to transpile ES 2015 modules.
Or you can rewrite your code to not use ES 2015 modules at all.

Related

Node 12 Native import

Im trying to use native import which were released with node 12
but i always get this error even with importing just cors
import * as cors from 'cors';
^^^^^^
SyntaxError: Cannot use import statement outside a module
at Module._compile (internal/modules/cjs/loader.js:892:18)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)
at Module.load (internal/modules/cjs/loacjsder.js:812:32)
at Function.Module._load (internal/modules/cjs/loader.js:724:14)
at Function.Module.runMain (internal/modules/cjs/loader.js:1025:10)
at internal/main/run_main_module.js:17:11
node version v12.13.0
docs https://nodejs.org/docs/latest-v12.x/api/esm.html#esm_code_import_code_specifiers which i tried to follow.
Thanks for help
You have to use this flag --experimental-modules while running node js, also you will have to us .mjs instead of .js extension

Error importing redux:

ERROR:
Error: Cannot find module 'redux'
at Function.Module._resolveFilename (module.js:555:15)
at Function.Module._load (module.js:482:25)
at Module.require (module.js:604:17)
at require (internal/module.js:11:18)
at Object. (C:\dev\Django
code\portfolio_web\stats_frontend\node_modules\react-
redux\lib\connect\mapDispatchToProps.js:8:14)
at Module._compile (module.js:660:30)
at Module._extensions..js (module.js:671:10)
at Object.require.extensions.(anonymous function) [as .js] (C:\dev\Django
code\portfolio_web\stats_frontend\node_modules\babel-
register\lib\node.js:152:7) at Module.load (module.js:573:32)
at tryModuleLoad (module.js:513:12)
The file where I import react-redux is reduxStore.js:
import {createStore} from 'react-redux;
//There is nothing else. The function being imported doesn't change anything`
I'm running the file with babel-node, or with webpack-cli (which is using babel to transpile ES2015. In both cases I get the same error.
Tracing the error I can open the source (
...\stats_frontend\node_modules\react-redux\lib\connect\mapDispatchToProps.js of the error (its from official react-redux dist).
Line 8 (which causes the error) I can see a commonJS import:
var _redux = require('redux');
Upon further inspection I can see that the node search algorithm will not find 'redux' because no such file exists in ./node_modules/react-redux/ or in ./node_modules/
I have installed and updated my react-redux installation with node install --save-dev react-redux with no errors.
I was hoping someone can provide insight on why the error occurs and how to fix it
You are importing createStore from wrong library. import it from redux
import { createStore } from 'redux';

kue - Unexpected token import

Hi I am trying to import my class in worker and getting following error
(function (exports, require, module, __filename, __dirname) { import { Myclass } from '../utility/myclass';
^^^^^^
SyntaxError: Unexpected token import
at createScript (vm.js:53:10)
at Object.runInThisContext (vm.js:95:10)
at Module._compile (module.js:543:28)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:427:7)
I also get error in following
async function getOrder() : Promise<any> {
^
SyntaxError: Unexpected token :
I am using typescript and have included type files for kue. I run the worker from command line and it looks like it is able to understand the env... not sure whats wrong ..
It seems you running the code without ES6 support.
You need to use babel-cli to run this code, it will compile the code to ES6 before running it.
babel-node yourscript.js

Import keyword is not recognized by gulp throwing SyntaxError: Unexpected reserved word

Failed to load external module babel-core/register
Failed to load external module babel/register
somelocation\gulpfile.babel.js:27
import fs from 'fs';
^^^^^^
SyntaxError: Unexpected reserved word
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:413:25)
at Object.Module._extensions..js (module.js:452:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Module.require (module.js:365:17)
at require (module.js:384:17)
at Liftoff.handleArguments (\AppData\Roaming\npm\node_modules\gulp\bin\gulp.js:116:3)
at Liftoff.<anonymous> (\AppData\Roaming\npm\node_modules\gulp\node_modules\liftoff\index.js:192:16)
at module.exports (\AppData\Roaming\npm\node_modules\gulp\node_modules\liftoff\node_modules\flagged-respawn\index.js:17:3)
I am getting this error while trying to run gulp after npm as described here: deploy-appengine.md. How to fix it?
EDIT: as Luke Bennet points out correctly in a comment to this answer, it was indeed JavaScript - the ES6 "import" syntax. The issue is that this won't work out of the box in Node as yet.
You seem to be using the python module import syntax in NodeJS. You should use the NodeJS module require syntax.

Handlebars with Node.js, error on precompilation

I have been using handlebars.js client-side for a while, and I wanted to get into using pre-compiled templates. I followed the general instructions I could find on the web, including installing it on Node.js:
npm install handlebars -g
Next, I go to my Node.js directory and try to compile a simple template:
handlebars test.handlebars -f test.js
And I get an error (command prompt, mind you and i'm sorry):
C:\Users\Administrator\AppData\Roaming\npm\node_modules\handlebars\lib\handlebars.js:1
(function (exports, require, module, __filename, __dirname) { import Handlebar
^^^^^^
SyntaxError: Unexpected reserved word
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 Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (C:\Users\Administrator\AppData\Roaming\npm\node_modules\handlebars\bin\handlebars:105:18)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
I go to this handlebars.js file that errored and sure enough the code definitely doesn't look like javascript:
import Handlebars from "./handlebars.runtime";
// Compiler imports
module AST from "./handlebars/compiler/ast";
import { parser as Parser, parse } from "./handlebars/compiler/base";
// ... etc.
I don't really know what this is, so I don't know what I did wrong or what my problem is.
Looks like lib folder contains not processed ES6-like modules. So probably it was be not processed with es6-module-transpiler. I'm open issue https://github.com/wycats/handlebars.js/issues/642 also exist similar issue https://github.com/wycats/handlebars.js/issues/643 .
I'm workaround this by installing es6-module-transpiler manually process files, and quick fix remain errors (something around with ["default"] exports).

Resources