我被一个问题困住了,我必须生成一个6位数字的字母数字代码,它应该是唯一的,为此我使用的是纳米线,现在当我编写这个代码时:
const {nanoid} = require("nanoid");
const ID = nanoid();我有错误:
const {nanoid} = require("nanoid");
^
Error [ERR_REQUIRE_ESM]: require() of ES Module C:\Users\HP\Desktop\test\node_modules\nanoid\index.js from C:\Users\HP\Desktop\test\server.js not supported.
Instead change the require of index.js in C:\Users\HP\Desktop\test\server.js to a dynamic import() which is available in all CommonJS modules.
at Object.<anonymous> (C:\Users\HP\Desktop\test\server.js:1:18) {
code: ←[32m'ERR_REQUIRE_ESM'←[39m
}如果我编码这个:
import { nanoid } from 'nanoid'
const id = nanoid() //=> "V1StGXR8_Z5jdHi6B-myT"我有错误:
(node:4636) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
C:\Users\HP\Desktop\test\server.js:4
import { nanoid } from 'nanoid'
^^^^^^
SyntaxError: Cannot use import statement outside a module
at Object.compileFunction (node:vm:352:18)
at wrapSafe (node:internal/modules/cjs/loader:1033:15)
at Module._compile (node:internal/modules/cjs/loader:1069:27)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
at node:internal/main/run_main_module:17:47现在我尝试更改package.json文件
"type":"module"但是我的项目使用babel,它将自动地将导入转换为require,因此第一个错误再次出现。
你能告诉我如何制作纳米机器人吗?
发布于 2022-06-23 12:19:25
这是一个特性,而不是一个bug。请参阅changelog以获得4.0版本https://github.com/ai/nanoid/issues/365中最新更改的详细信息。
https://stackoverflow.com/questions/72711271
复制相似问题