Node.js“不需要"其他文件而无需名称空间 [英] Node.js 'require'ing other files without needing namespaces

查看:73
本文介绍了Node.js“不需要"其他文件而无需名称空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将代码库从100%浏览器端切换到浏览器端和服务器端的混合.

I am switching a codebase from 100% browser side, to a mixture of browser side and server side.

我发现的问题是,要使我的代码使用node.js运行,我必须使用模块.为了使我的代码成为模块,几乎需要对我的所有代码进行大量重构.原因是在浏览器上,跨文件使用了许多功能,而无需担心任何命名空间.

The issue i have found is that to get my code to run using node.js i must use modules. In order to make my code into modules, it would require a huge refactor of almost all of my code. The reason is that on the browser many functions were used across files without worrying about any namespacing.

但是目前,在node.js中,我找不到实现相同效果的方法,而且我发现的所有解决方案都无效.

But currently, in node.js i have found no way to achieve this same affect, and all solutions i have found have not worked.

举一个我想要的例子,我希望能够做到这一点:

For an example of what i would like, i would like to be able to do this:

////////////
// file1.js
////////////

function someFunction(someArgs) {
    /* run some stuff, calculate some stuff */

    return something;
}

////////////
// file2.js
////////////

function someFunction2(someArgs) {
    /* run some code */

    let someValue = someFunction(someArgs);

    /* run some more code */
}

////////////
// file3.js
////////////

someFunction2(myArguments);


我已尝试在此处,但他们没有帮助我.


I have tried following solutions found here, but they have not helped me.


"masylum" 的答案不符合我的需要.

"masylum"'s answer does not do what i need.


"Udo G" 的答案,指的是使用eval在运行文件中包含另一个文件会引发以下错误:

"Udo G"'s answer, referring to using eval to include the other file in the running file throws this error:

console.log(isWhitelisted("test"));
            ^

ReferenceError: isWhitelisted is not defined
    at Object.<anonymous> (/Users/< snip >/server.js:45:13)
    at Module._compile (module.js:413:34)
    at Object.Module._extensions..js (module.js:422:10)
    at Module.load (module.js:357:32)
    at Function.Module._load (module.js:314:12)
    at Function.Module.runMain (module.js:447:10)
    at startup (node.js:139:18)
    at node.js:999:3

这是Udo G指出的,因为我使用的是严格模式:

This although was pointed out by Udo G as i am using strict mode:

还请注意,这不适用于严格使用";

Please also note this won't work with "use strict";


尼克·帕诺夫"(Nick Panov)的答案对我也不起作用,并抛出了与Udo G答案相同的错误.这也可能是因为使用严格.

"Nick Panov"'s answer also did not work for me, throwing the same error as Udo G's answer. This might also be because of the use strict.

在不重构整个代码库以坚持使用模块的情况下,有什么方法可以实现这一目标吗?

Is there any way i can achieve this without refactoring the whole codebase to adhere to using modules?

推荐答案

尝试使用构建步骤来实现这一目标.这些文件的简单组合将为您提供帮助.例如,查看 https://github.com/contra/gulp-concat

Try to use a building step to achieve this. Simple concat of the files will help you. For example, look at https://github.com/contra/gulp-concat

以下是与gulp相关的文档- https://github .com/gulpjs/gulp/blob/master/docs/getting-started.md

Here is docs related to gulp - https://github.com/gulpjs/gulp/blob/master/docs/getting-started.md

这篇关于Node.js“不需要"其他文件而无需名称空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆