0

I need to develop a npm package like wpcom-xhr-request, this package can be used in node and web, I cloned this package and give it a new name , create a personal git package. it can only work in web, when it is used in node, it give this error:

import requestHandler from 'acme-xhr-request';
^^^^^^

SyntaxError: Unexpected token import
    at createScript (vm.js:80:10)

why does wpcom-xhr-request can be used at both environment? how can do this without n8-make? currently , makefile make our work a litter complex , I don't like to introduce new complexity。

Vidy Videni
  • 1,897
  • 3
  • 15
  • 23

1 Answers1

1

I suspect the version of node you're using doesn't understand import yet. It can be enabled via experimental features of node, but you'll need to use the require syntax (not import), or transpile it down to ES5 with a tool like babel for now.

Node.js - SyntaxError: Unexpected token import

Setup webpack for your node app, then install the babel-loader to transpile the ES6 javascript down to ES5.

https://github.com/babel/babel-loader

a7drew
  • 7,801
  • 6
  • 38
  • 39
  • hi, thanks for your help, the problem is I copied all files from ` wpcom-xhr-request`, build this new package , it doesn't work in browser and node, why does `wpcom-xhr-request` work , but mine? can you use that package as an example show me what I did wrong, please? – Vidy Videni Apr 09 '18 at 16:13
  • you are right , I did't use bable to transcode es6, however I also didn't find babel-loader configuration in the `wpcom-xhr-request` , I am wondering why it works. – Vidy Videni Apr 09 '18 at 16:15