在节点中包含外部.js文件。js应用

时间:2021-06-25 16:09:58

I have an app.js node application. As this file is starting to grow, I would like to move some part of the code in some other files that I would "require" or "include" in the app.js file.

我有一个app.js节点应用程序。当这个文件开始增长时,我想在其他文件中移动部分代码,我将“要求”或“包含”在app.js文件中。

I'm trying things like:

我试着像:

// Declare application
var app = require('express').createServer();

// Declare usefull stuff for DB purposes
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var ObjectId = Schema.ObjectId;

// THE FOLLOWING REQUIRE DOES NOT WORK
require('./models/car.js');

in car.js:

在car.js:

// Define Car model
CarSchema = new Schema({
  brand        : String,
  type : String
});
mongoose.model('Car', CarSchema);

I got the error:

我得到了错误:

ReferenceError: Schema is not defined

I'm just looking to have the content of car.js loaded (instead of having everything in the same app.js file) Is there a particuliar way to do this in node.js ?

我只是想知道汽车的内容。加载js(而不是将所有内容都放在同一个app.js文件中)是否有一种在节点中这样做的特定方式。js ?

6 个解决方案

#1


98  

To place an emphasis on what everyone else has been saying var foo in top level does not create a global variable. If you want a global variable then write global.foo. but we all know globals are evil.

要强调其他人在顶层所说的var foo并不会创建全局变量。如果你想要一个全局变量,那么写global.foo。但我们都知道全球变暖是邪恶的。

If you are someone who uses globals like that in a node.js project I was on I would refactor them away for as there are just so few use cases for this (There are a few exceptions but this isn't one).

如果你是一个在节点中使用全局变量的人。我正在进行的js项目会重构它们,因为只有很少的用例(有一些例外,但这不是一个)。

// Declare application
var app = require('express').createServer();

// Declare usefull stuff for DB purposes
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var ObjectId = Schema.ObjectId;

require('./models/car.js').make(Schema, mongoose);

in car.js

在car.js

function make(Schema, mongoose) {
    // Define Car model
    CarSchema = new Schema({
      brand        : String,
      type : String
    });
    mongoose.model('Car', CarSchema);
}

module.exports.make = make;

#2


54  

The correct answer is usually to use require, but in a few cases it's not possible.

正确的答案通常是使用require,但在某些情况下是不可能的。

The following code will do the trick, but use it with care:

下面的代码可以起到这个作用,但是要小心使用:

var fs = require('fs');
var vm = require('vm');
var includeInThisContext = function(path) {
    var code = fs.readFileSync(path);
    vm.runInThisContext(code, path);
}.bind(this);
includeInThisContext(__dirname+"/models/car.js");

#3


17  

Short answer:

简短的回答:

// lib.js
module.exports.your_function = function () {
  // Something...
};

// app.js
require('./lib.js').your_function();

#4


8  

you can put

你可以把

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

at the top of your car.js file for it to work, or you can do what Raynos said to do.

在你的车顶。js文件是用来工作的,或者你可以按照Raynos的说法去做。

#5


4  

If you just want to test a library from the command line, you could do:

如果您只想从命令行测试一个库,您可以这样做:

cat somelibrary.js mytestfile.js | node

#6


0  

This approach works for me in Node.js, Is there any problem with this one?

这种方法在Node中对我很适用。js,这个有什么问题吗?

File 'include.js':

文件“include.js”:

fs = require('fs');

File 'main.js':

文件“main.js”:

require('./include.js');

fs.readFile('./file.json', function (err, data) {
    if (err) {
        console.log('ERROR: file.json not found...')
    } else {
        contents = JSON.parse(data)
    };
})

#1


98  

To place an emphasis on what everyone else has been saying var foo in top level does not create a global variable. If you want a global variable then write global.foo. but we all know globals are evil.

要强调其他人在顶层所说的var foo并不会创建全局变量。如果你想要一个全局变量,那么写global.foo。但我们都知道全球变暖是邪恶的。

If you are someone who uses globals like that in a node.js project I was on I would refactor them away for as there are just so few use cases for this (There are a few exceptions but this isn't one).

如果你是一个在节点中使用全局变量的人。我正在进行的js项目会重构它们,因为只有很少的用例(有一些例外,但这不是一个)。

// Declare application
var app = require('express').createServer();

// Declare usefull stuff for DB purposes
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var ObjectId = Schema.ObjectId;

require('./models/car.js').make(Schema, mongoose);

in car.js

在car.js

function make(Schema, mongoose) {
    // Define Car model
    CarSchema = new Schema({
      brand        : String,
      type : String
    });
    mongoose.model('Car', CarSchema);
}

module.exports.make = make;

#2


54  

The correct answer is usually to use require, but in a few cases it's not possible.

正确的答案通常是使用require,但在某些情况下是不可能的。

The following code will do the trick, but use it with care:

下面的代码可以起到这个作用,但是要小心使用:

var fs = require('fs');
var vm = require('vm');
var includeInThisContext = function(path) {
    var code = fs.readFileSync(path);
    vm.runInThisContext(code, path);
}.bind(this);
includeInThisContext(__dirname+"/models/car.js");

#3


17  

Short answer:

简短的回答:

// lib.js
module.exports.your_function = function () {
  // Something...
};

// app.js
require('./lib.js').your_function();

#4


8  

you can put

你可以把

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

at the top of your car.js file for it to work, or you can do what Raynos said to do.

在你的车顶。js文件是用来工作的,或者你可以按照Raynos的说法去做。

#5


4  

If you just want to test a library from the command line, you could do:

如果您只想从命令行测试一个库,您可以这样做:

cat somelibrary.js mytestfile.js | node

#6


0  

This approach works for me in Node.js, Is there any problem with this one?

这种方法在Node中对我很适用。js,这个有什么问题吗?

File 'include.js':

文件“include.js”:

fs = require('fs');

File 'main.js':

文件“main.js”:

require('./include.js');

fs.readFile('./file.json', function (err, data) {
    if (err) {
        console.log('ERROR: file.json not found...')
    } else {
        contents = JSON.parse(data)
    };
})