Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to require a file other than the npm main file in node?

package.json:

... "name": "mypackage", "main": "src/index.js" ... 

Directory structure:

|- src/ |--- index.js |--- other.js 

I can require src/index.js with require('mypackage');, but how can I require src/other.js?

If the answer is require('mypackage/src/other');, is there a way to make it so I can require it with require('mypackage/other'); (i.e. teaching node what the source file directory is of your module?

like image 657
Mike Sherov Avatar asked Oct 08 '15 13:10

Mike Sherov


People also ask

How do I require another file in NodeJS?

If, despite all the other answers, you still want to traditionally include a file in a node. js source file, you can use this: var fs = require('fs'); // file is included here: eval(fs.

For what require () is used in NodeJS?

1) require() In NodeJS, require() is a built-in function to include external modules that exist in separate files. require() statement basically reads a JavaScript file, executes it, and then proceeds to return the export object.

Why we always require modules at the top of a file?

Now, I personally find it better to declare all your modules on the top of the file since it: Makes it easier to know when you are requiring a module that has a bug (for example, crashes something) Makes it easier for the reader to know all the dependencies of your module.

Can I use require in js file?

The require() method is used to load and cache JavaScript modules. So, if you want to load a local, relative JavaScript module into a Node. js application, you can simply use the require() method.


2 Answers

AFAIK You'll have to explicitly expose it in the root:

Directory structure:

|- src/ |--- index.js |--- other.js |- other.js 

Then in /other.js

module.exports = require('src/other.js'); 

Now you can do require('mypackage/other')

like image 117
Creynders Avatar answered Sep 23 '22 20:09

Creynders


I'm currently looking into the exact same thing.

Package.json has a property called 'files':

http://blog.kewah.com/2014/npm-as-a-front-end-package-manager/

https://docs.npmjs.com/files/package.json

The "files" field is an array of files to include in your project. If you name a folder in the array, then it will also include the files inside that folder.

But I have yet to find how to do a import/require of such a file. I don't really see another point in listing these files other then to be able to import/require them?


I was able to import a file from a package if it was listed in this files array.

{     "name": "local-ui-utilities",     "version": "0.0.1",     "description": "LOCAL UI Utilities",     "main": "index.jsx",     "author": "Norbert de Langen",     "license": "none",     "dependencies": {     },     "files": [        "/colors/sets/variables.css"     ] } 

I'm able to import the css file from the package using postcss-import:

@import "local-ui-utilities/colors/sets/a.css"; 

This probably isn't your use-case, but postcss-import just uses npm under the hood. So This should work for your use-case as well, I would think.

This question and accepted answer seem related: Node/NPM: Can one npm package expose more than one file?

like image 39
Norbert Avatar answered Sep 23 '22 20:09

Norbert