Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add instructions for dynamic import in README.md #516

Merged
merged 4 commits into from
Jan 8, 2025
Merged

Conversation

YitzchakMeltz
Copy link
Contributor

Pull Request Overview

I edited the README to include the option to dynamically import the latest nanoid version even when an earlier version of Node is being used.

Pull Request Details

When working on a specific project, I was in a situation where I was using Node JS 20.11.0 and commonJS. Without going into details, we could neither switch over to ESM nor could we upgrade to Node JS 20.12.0. The only solution that worked for me was importing the module dynamically as follows:

module.exports.create = async () => {
  const { customAlphabet } = await import('nanoid');
  const nanoid = customAlphabet(alphanumeric, 10);

  // Remaining code
};

This helped solve my issue and I would like to help other developers who might be facing a similar issue.

Thank you for creating this great package!

@ai
Copy link
Owner

ai commented Jan 6, 2025

  1. Three or is hard to understand. Lets’ split to blocks or ul-items.
  2. Let’s add code example or a link

@YitzchakMeltz
Copy link
Contributor Author

  1. Three or is hard to understand. Lets’ split to blocks or ul-items.
  2. Let’s add code example or a link

Let me know if this is what you had in mind.

README.md Outdated

- For Node.js 16 or newer you can dynamically import Nano ID as follows:
```js
const { nanoid } = await import('nanoid');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Top-level await works in Node.js 16?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Note bellow already addresses that, but I can make that more clear in the example if you'd like.

README.md Outdated
- For Node.js 16 or newer you can dynamically import Nano ID as follows:
```js
module.exports.createID = async () => {
const { nanoid } = await import('nanoid');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can’t recommend importing nanoid again and again on every createID() call

Copy link
Contributor Author

@YitzchakMeltz YitzchakMeltz Jan 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ai For developers working in a serverless environment, this is both a viable and the best solution.
Dynamically importing the function with not happen instantly. If you dynamically import the package outside of the function, you might try to access the package before it finished importing which would result in an error.
In a standard server environment you can simply run a function that imports the package when you startup the server. While that might be a better solution for that instance, the above solution (while not optimal) will work as well.
The serverless community is large enough that I feel this is the best generalized solution.
If you prefer, we can add a note specifying that the package will be imported every time the function runs.
Let me know what you think.

Copy link
Owner

@ai ai Jan 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a cache and lazy import?

let nanoid
async function createID() {
  if (!nanoid) await import()...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. That is a good solution. I will update the pull request.

@ai ai merged commit 5681219 into ai:main Jan 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants