将“ Vanilla” JavaScript库加载到Node.js ='意外的令牌导出' [英] Load “Vanilla” Javascript Libraries into Node.js = 'Unexpected token export'

查看:75
本文介绍了将“ Vanilla” JavaScript库加载到Node.js ='意外的令牌导出'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以相关问题为基础加载"香草 Javascript库到Node.js中



我正尝试使用以下方法将香草 Javascript库 rsa.js加载到NodeJS中克里斯·W。本质上,我已经创建了一个自定义的node_modules目录,如下所示:

  ./ node_modules / rsa / rsa-lib / rsa.js 
./node_modules/rsa/rsa-lib/README
./node_modules/rsa/index.js

唯一的问题是,在rsa.js的最后一行编译模块失败:

 未定义: 836 
出口默认rsa;
语法错误:意外的令牌导出

rsa.js

  var rsa = {}; 
(function(rsa){

...

})(rsa);
console.log( rsa,rsa);
出口默认rsa;

index.js

  var fs = require('fs'); 

//读取和评估库
filedata = fs.readFileSync(’./ node_modules / rsa / rsa-lib / rsa.js’,’utf8’);
eval(文件数据);

/ * rsa.js文件定义了一个类 rsa,这是我们要导出的所有内容* /
exports.rsa = rsa

任何建议如何解决此问题?

解决方案

由于库正在使用



export default ThingToExport;

而不是



module.exports = somethingToExport



这是Node尚不支持的ES6功能,因此,当Node尝试运行代码时,会引发错误。



如何解决::尝试修改库的最后一行,使其显示为 module.exports = rsa;



我应该补充一点, eval 并不是将库加载到自己的代码中的好方法。这就是要求的目的。如果您已经使用 npm i 安装了该库,并且该库位于node_modules中,则可以使用 var rsa = require将其加载到代码中。 ('rsa');



同样,如果您不转译代码,则导出默认值可能会出现问题,所以您可能会希望将其更改为 module.exports


Building on related issue: Load "Vanilla" Javascript Libraries into Node.js

I'm trying to load a 'Vanilla' Javascript library 'rsa.js' into NodeJS, using the method described by Chris W. in the question above. In essence I've created a custom node_modules directory like this:

./node_modules/rsa/rsa-lib/rsa.js
./node_modules/rsa/rsa-lib/README
./node_modules/rsa/index.js

Only problem is, compiling the module fails on the last line of rsa.js:

undefined:836
export default rsa;
SyntaxError: Unexpected token export

rsa.js

var rsa = {};
 (function(rsa) {

   ...

})(rsa);
console.log("rsa",rsa);
export default rsa;

index.js

var fs = require('fs');

// Read and eval library
filedata = fs.readFileSync('./node_modules/rsa/rsa-lib/rsa.js','utf8');
eval(filedata);

/* The rsa.js file defines a class 'rsa' which is all we want to export */
exports.rsa = rsa

Any suggestions how to fix this problem?

解决方案

The error 'Unexpected token export' is caused because the library is using

export default thingToExport;

instead of

module.exports = thingToExport

This is an ES6 feature not supported by Node (yet), so when Node tries to run the code, it throws an error.

How to solve: try modifying the last line of the library so it says module.exports = rsa;.

I should add, though, that eval is not a good way to load a library into your own code. That is what require is for. If you have installed this library with npm i and it is in your node_modules, you should be able to load it into your code with var rsa = require('rsa');.

Again though, if you're not transpiling the code, it may have problems with export default, so you will probably want to change that to module.exports either way.

这篇关于将“ Vanilla” JavaScript库加载到Node.js ='意外的令牌导出'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆