简体   繁体   English

MeteorJs es6 模块导入

[英]MeteorJs es6 modules imports

I recently discovered that, whenever I try to to execute a node script like that:我最近发现,每当我尝试执行这样的节点脚本时:

"run-my-script": "node ./folder/function.js executeFunction $ARG"

It seem that using a script with node causes troubles with ES6 imports.似乎使用带有节点的脚本会导致 ES6 导入出现问题。 If I run a basic script like that I get this error:如果我运行这样的基本脚本,我会收到此错误:

SyntaxError: Cannot use import statement outside a module

My running command line look like that:我正在运行的命令行如下所示:

ARG=myArg npm run run-my-script

Like if it was impossible for meteor to run a node script outside without the need to transpile code to ES6 first.就像 meteor 不可能在不需要先将代码转换为 ES6 的情况下在外部运行节点脚本一样。

All my modules are designed by ES6 modules approach:我所有的模块都是通过 ES6 模块方法设计的:

import { SchemaBuyers } from './schema';

I don't want to specify type=module inside my package.json because first of all, it's not necessary, I don't have compilation errors when building the apps, ony when running pure node script, and secondly it would require to rename all my files extentions to.mjs for example.我不想在我的 package.json 中指定 type=module 因为首先,这不是必需的,我在构建应用程序时没有编译错误,只有在运行纯节点脚本时,其次它需要重命名例如,我所有的文件都扩展为.mjs。

Thank you very much !非常感谢 !

You can use node.js's --input-type option for this if you pipe the file into it:如果您将 pipe 文件放入其中,则可以为此使用 node.js 的--input-type选项:

cat test.js | node --input-type=module

Or in your case:或者在你的情况下:

"run-my-script": "cat ./folder/function.js | node --input-type=module executeFunction $ARG"

This assumes that the code in function.js will parse the provided arguments.这假设function.js中的代码将解析提供的 arguments。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM