2

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)
satish
  • 703
  • 5
  • 23
  • 52
  • What's in `run.js` ? Are you running code from `src` or `lib` ? – Gabriel Bleu Mar 05 '18 at 16:44
  • "run.js" is at the root of my project directory -- essentially the root of what I downloaded from here -- https://github.com/ryohey/node-stratum-client . I'm running the code from teh directory root (as well as where I ran all the other commands I listed in my question). – satish Mar 05 '18 at 17:07
  • no `run.js` in there ... – Gabriel Bleu Mar 05 '18 at 17:10
  • I know. I created it from the code listed in the "How to use" section. – satish Mar 05 '18 at 17:34

0 Answers0