[英]How do I fix “unexpected token 'export'” error when I try to use uuid package?
I'm working in a vue.js application with node.js in the backend.我在 vue.js 应用程序中工作,node.js 在后端。 We've installed the npm package uuid in the backend but it's not working.
我们已经在后端安装了 npm package uuid,但它不起作用。
Our package.json file in the backend contains this:我们在后端的 package.json 文件包含以下内容:
"uuid": "^8.2.0",
In our controller, in the constructor, we have this:在我们的 controller 中,在构造函数中,我们有这个:
this.uuid = require("uuid");
And we use it like this:我们像这样使用它:
const id = this.uuid.v1();
The problem is, when we call the endpoint on the backend, we get this error:问题是,当我们在后端调用端点时,我们得到这个错误:
C:\...\backend\node_modules\uuid\dist\esm-browser\index.js:1
export { default as v1 } from './v1.js';
^^^^^^
SyntaxError: Unexpected token 'export'
Googling this issue, the only solutions I've found involve importing v1 specifically, such as this:谷歌搜索这个问题,我发现的唯一解决方案涉及专门导入 v1,例如:
import {v1 as uuid} from "uuid";
But since we're using node.js on the backend, we get this error:但由于我们在后端使用 node.js,我们得到这个错误:
SyntaxError: Cannot use import statement outside a module
How can I solve this problem?我怎么解决这个问题?
I have been there.我去过那里。 Used this approach:
使用了这种方法:
const uuid = require('uuid');
const newUuid = uuid.v4();
You may use .v1()
instead of .v4()
.您可以使用
.v1()
而不是.v4()
。
You should first export your functions (that you need in other modules) in v1.js like below-您应该首先在 v1.js 中导出您的函数(您需要在其他模块中),如下所示 -
function xyz() {
}
module.exports = xyz;
You can then use this in the respective files as-然后,您可以在相应的文件中使用它作为 -
const xyz = require(‘../pathname’);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.