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

Example using node modules with njs #19

Open
cliffxuan opened this issue Sep 1, 2022 · 2 comments
Open

Example using node modules with njs #19

cliffxuan opened this issue Sep 1, 2022 · 2 comments

Comments

@cliffxuan
Copy link

cliffxuan commented Sep 1, 2022

Thanks for all these nice examples.

Can you please also create an example for using 3rd party node modules? Or is it still supported after the removal of the js_include directive?

I've been following this guide, however, it doesn't seem to work.
https://nginx.org/en/docs/njs/node_modules.html

Is the guide for using the js_include directive only? With js_include an export default {...} statement isn't needed at the end of the bundled js file. However, js_import requires this export default {...} statement, which undoes the effort by the load.js in the global namespace.

@lcrilly
Copy link

lcrilly commented Sep 1, 2022

You can use js_import with node modules but you are correct about the requirement for an export statement. However, the export can be for an empty function. You can simply add 2 lines to your .js file

export default {foo}
function foo(){}

That should work. I have a more compete example at https://github.com/lcrilly/nginx-xml-json#using-npm

@cliffxuan
Copy link
Author

@lcrilly Thanks very much for the reply with the solution as well as the reference to the complete example. I can see it working and will give it a try.

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

No branches or pull requests

2 participants