Tomi Melone
Tomi Melone

Reputation: 165

How to use nanoid module on NodeJS?

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();

And I get the error message:

TypeError: nanoid is not a function
  at Object.<anonymous> (myapp.js directory)

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

Upvotes: 15

Views: 24496

Answers (3)

Lex
Lex

Reputation: 800

If you do not want to switch to {"type":"module"} in your package.json then you can change nanoid version to 3.0.0

"dependencies": {
    "nanoid": "^3.0.0",
}
npm install
const { nanoid } = require('nanoid')

Upvotes: 0

Hossein Azad
Hossein Azad

Reputation: 108

in new method ES6 must use IMPORT like

import { nanoid } from 'nanoid';
var ID = nanoid();

Upvotes: 0

Neriya Bar-lev
Neriya Bar-lev

Reputation: 602

nanoid is a function inside the module try this

var { nanoid } = require("nanoid");
var ID = nanoid();

Upvotes: 39

Related Questions