-
-
Notifications
You must be signed in to change notification settings - Fork 808
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
Conversation
|
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'); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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'); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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()...
There was a problem hiding this comment.
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.
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:
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!