componentjs / component

frontend package manager and build tool for modular web applications
https://github.com/componentjs/guide
MIT License
4.55k stars 306 forks source link

Cant require node_modules when componentjs is installed? #644

Closed bijanmmarkes closed 6 years ago

bijanmmarkes commented 6 years ago

I am working on a project which is using componentjs, When trying to require a module in the node_modules folder such as require("nodemailer"); It cannot find it. There are no component dependencies which allow for mail. How do you require a node_module without having to create a component out of it? any ideas?

timaschew commented 6 years ago

I am working on a project which is using componentjs

Be aware, componentjs is deprecated since 2015

When trying to require a module in the node_modules folder such as require("nodemailer"); It cannot find it

nodemailer is not a compatible with component

There are no component dependencies which allow for mail.

componentjs is a build tool for frontend modules. Mail related modules require a server.

How do you require a node_module without having to create a component out of it? any ideas?

Not at all, if you're using componentjs. Just use Node.js

bijanmmarkes commented 6 years ago

The problem is the structure of component is it doesn't allow you to require node_modules from the server as it is trying to look for a component. I know nodemailer would not work with component but it seems to override the require command completely which is how you require server-side node_modules. How do you require non-components using node.js and use them on the front-end when component is present in the project? How would i require nodemailer using the "require" command when it tries to look for a component?

timaschew commented 6 years ago

The problem is the structure of component is it doesn't allow you to require node_modules from the server as it is trying to look for a component.

Of course, as I said, component is a package manager and build to tool (bundler) for the frontend. You won't be able to require a module which doesn't have nor a component.json or some special properties in the package.json and that is by design!

I know nodemailer would not work with component but it seems to override the require command completely which is how you require server-side node_modules.

Not exactly, it just provides an implementation of require, because browsers don't have requireimplemented.

How do you require non-components using node.js and use them on the front-end when component is present in the project?

  1. Not using component, switch to webpack. This is the de facto standard frontend bundler.
  2. Add a component.json or these properties to the module.
  3. If you don't have control of the module, wrap it.
  4. Try out https://github.com/eldargab/component-as-module

How would i require nodemailer using the "require" command when it tries to look for a component?

Ah, I think I got it. So you have already component in usage. But i still don't understand how you want to use nodemailer in the browser, this doesn't make sense and is just impossible, see https://stackoverflow.com/questions/37630543/can-i-use-nodemailer-in-browser

Anyway if you want to load a module which is not compatible with component you basically need another bundler. Some bundler polluting the global scope with require so you need to take care that they are bundling their modules isolated, for component see https://github.com/componentjs/guide/blob/master/creating-components/standalone-umd-builds.md

But this sounds like a really ugly hack. I recommend you to switch from component to webpack or some other maintained bundler.