我正在创建一个脚本,用于运行我系统中的某些js文件(实际上它们是React应用程序中的纯js文件),我想评估它们的内容并获取变量的内容以在我的脚本中使用。请查看此处的代码:
import glob from 'glob';
const cwd = '/path/to/code';
const findAll = () => glob('**/*config.js', { cwd }, (err, matches) => {
// matches is an array of paths like ['dir/myfile-config.js']
if (err) {
console.error(err);
return;
}
const file = matches[0];
// file here is the path to a JS file that contains something like export const CONFIG = {prop1: 'value1'};
import(`${cwd}/${file}`)
.then(console.log)
.catch(console.error);
});
当我调用import()函数时,它会抱怨:
export const CONFIG = {
^^^^^^
SyntaxError: Unexpected token 'export'
然而'glob'库已经正确导入。我不明白为什么无法导入该文件,也没有在互联网上找到任何相关信息。我可以找到关于如何使节点了解模块系统的信息,但这不是问题所在,因为我的程序理解import/export语法。
我正在使用node 12.13.0运行我的程序,例如:
node --experimental-modules ./index.js
我还尝试过使用node 13.14.0(删除--experimental-modules标志),但结果相同:(真的很感激任何帮助。
我也添加了我正在使用的package.json:
{
"name": "config-retriever",
"version": "1.0.0",
"description": "",
"main": "index.js",
"license": "MIT",
"type": "module",
"private": true,
"dependencies": {
"glob": "^7.1.6"
}
}