2

jade permits you to simply write

include folder/file

to include code from another file.

Is it possible to add simply cut - copy style code from another file in node for javascript files?

Its for development purpose, to isolate some code and work on it seperately.

PS:- I'm aware of require('jsfile.js') and export.x = function(){..

3
  • Why don't you want to use require ? Commented Nov 6, 2013 at 10:26
  • I'm in development, the functions have dependencies. so, it makes for statements like y= require('x')(a, b, c), then using y.q(p). Where as simple including would break it down to: q(p) Commented Nov 6, 2013 at 10:33
  • Think what will happen if two different modules use same object name. Besides this should work: q = y.q? Commented Nov 6, 2013 at 13:29

2 Answers 2

2

The accepted answer is wrong.

Depending on whether node fs and eval were available at the time this question was written, the accepted answer was probably always wrong.

While not recommended, what you want to do is essentially possible:

  1. Use node's built-in filesystem functions to read the file you want to "copy-paste" into the current file.
  2. Use eval() to "paste" that file into your current file and run it as if it was part of the current file.

https://github.com/dennishall/node-require-without-require

Sign up to request clarification or add additional context in comments.

Comments

0

Update 6 Oct 2020: Embarrassingly, the answer I've provided below is false.

I am not certain what were the circumstances for my writings below, as I was familiar with eval at the time (and a very long time before then), however, it is what it is :)

Read the answer that @Dennis wrote for the correct one.


You cannot merge (or include) a script file into another script file during runtime. Utilizing require is your best option to separate your application logic into multiple files.

JavaScript is an object oriented language, and what you are asking for is a solution to a problem that exists in procedural programming languages.

I suggest that you design your application in such a way that would allow you to separate its files into object types that take on different responsibilities instead of treating each file as a script within some global state.


To answer your other question, Jade is actually parsing its source files and therefore can provide its own file merging. If we apply this to our scenario, Jade is to jade source files as V8 is to JavaScript source files. Since the jade language is procedural, it makes sense to allow this kind of feature where in JavaScript (which is object oriented) it doesn't.

1 Comment

The scenario for not using require would be one with dynamic js files, since require does a cache of required files

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.