How to use nanoid module on NodeJS?

Tom*_*one 8 javascript module mongoose mongodb node.js

I know that maybe this question is very silly, but I can't make nanoid to work on my NodeJS app.

I already read the docs.

This is my app.js configuration:

var nanoid = require("nanoid");
var ID = nanoid();
Run Code Online (Sandbox Code Playgroud)

And I get the error message:

TypeError: nanoid is not a function
  at Object.<anonymous> (myapp.js directory)
Run Code Online (Sandbox Code Playgroud)

Maybe I need to import the function inside nanoid but I dont know how to do it. Help would be appreciate. Thanks.

小智 21

nanoid is a function inside the module try this

var { nanoid } = require("nanoid");
var ID = nanoid();
Run Code Online (Sandbox Code Playgroud)

  • 自 2023 年起,这不再有效 (8认同)