JD.
JD.

Reputation: 15551

Do all npm packages need an export?

I am new to nodejs packages and what I understood was that to share code you had to do a module.export (in addition to adding a package.json)

For example bootstrap-select does not have an export function but is available on npm.

So my question is do all modules require an export and also can I do a require('bootstrap-select') in my code?

Upvotes: 1

Views: 2601

Answers (3)

nirmal
nirmal

Reputation: 51

As per i know ,
1.All npm modules are not required to build an app.
2.If we use var bootStrap = require('bootstrap-select'); using bootStrap variable you can access bootStrap module.
so we can pass that object in anywhere of your code
3.To install a dependency modules,
In package.json give dependency block as like this
"dependencies": {
"express": "2.3.12",
"jade":   "latest",
"redis":   "0.6.0"
}
you can change and edit your packages. then enter a command npm install in command prompt it will install only dependency modules.
If i made any mistakes please correct me Thanks.

Upvotes: 0

Aaron Dufour
Aaron Dufour

Reputation: 17535

No, npm modules do not require doing something with module.exports. If you do not touch that object, requireing your module will return an empty object (since that is the default for module.exports. However, this can be useful if your module is only intended to be required for side effects, rather than a return value.

For example, the module you linked to modifies global state by attaching a jQuery event handler.

Upvotes: 1

Peter Lyons
Peter Lyons

Reputation: 146104

no, all npm modules do not require an export. npm is now being used more generally for not only javascript packages intended for use under node.js but front end code for browsers, CSS libraries, etc. At the very least, an npm package could just deliver a payload of files not even including any javascript, such as some images, some CSS, some HTML, etc.

So you can only do require('some-module') if that package has either an index.js file or has set the main property in it's package.json file properly.

However if you are authoring a javascript module for node.js, then yes you'll need to export something in order for your module to load properly.

Upvotes: 3

Related Questions