简体   繁体   English

当我尝试使用 uuid package 时,如何修复“意外令牌'导出'”错误?

[英]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.

相关问题 尝试发布有效的npm程序包,但是当我尝试使用它时崩溃[模块解析失败:意外令牌] - trying to publish a working npm package but crashes when i try to use it[Module parse failed: Unexpected token] NodeJS SyntaxError:意外的令牌。 我该如何尝试修复? - NodeJS SyntaxError: Unexpected token . How can I try to fix? NPM模块触发“ SyntaxError:意外令牌…”! 我如何解决它? - NPM Module triggering a “SyntaxError: Unexpected token …”! How do I fix it? Sequelize UUID 中的意外令牌“导出” - Unexpected token 'export' in Sequelize UUID 当我尝试在 meteor 上运行 vulcan 时,如何解决此错误? 它在体内给出 - How do I fix this error when I try to run vulcan on meteor? It is given in the body 为什么当我尝试在 expressjs 中下载文档时出现此错误“JSON 中的意外令牌 % 位于位置 0” - Why i have this error "Unexpected token % in JSON at position 0" when i try to download a document in expressjs 尝试在 nodejs 中使用 readfilesync 时,我收到“位置 0 处的 JSON 中的意外令牌” - I get "Unexpected token in JSON at position 0" when try to use readfilesync in nodejs 节点,当我运行package.json`bin``mand`时,给我一个'意外令牌附近的语法错误`('` - Node, when I run package.json `bin` `command` , give me `syntax error near unexpected token `(' ` 当我尝试在我的 React 项目中获取数据时,我收到此错误: Uncaught (in promise) SyntaxError: Unexpected token &lt; in JSON at position 0 - When i try to fetch data in my React project, then I get this error: Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0 当我尝试解析 JSON 中的字符串时,出现错误解析 JSON:语法错误:JSON 中的意外令牌 u 在 position 0 - I get Error parsing JSON: SyntaxError: Unexpected token u in JSON at position 0 when i try to parse a string from a JSON
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM