0

What I'm actually doing is writing a VS Code Extension, but since I'm new to Node I'm struggling with referencing one JS file from another.

//main.js (compiled from TypeScript)

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
require("./Test.js");
console.log("hello");
t1();

and

//Test.js

function t1() {
    console.log("t1");
}

They're both in the same folder. If I run it from VS Code, or from node directly, it doesn't work

PS E:\VSCodeTest> node src/main.js
hello
E:\VSCodeTest\src\main.js:5
t1();
^

ReferenceError: t1 is not defined
    at Object.<anonymous> (E:\VSCodeTest\src\main.js:5:1)
    at Module._compile (module.js:635:30)
    at Object.Module._extensions..js (module.js:646:10)
    at Module.load (module.js:554:32)
    at tryModuleLoad (module.js:497:12)
    at Function.Module._load (module.js:489:3)
    at Function.Module.runMain (module.js:676:10)
    at startup (bootstrap_node.js:187:16)
    at bootstrap_node.js:608:3

The VS Code project is actually TypeScript but I've distilled it down to the crux of the problem in the JS files.

I believe it should work based on

https://www.typescriptlang.org/docs/handbook/modules.html

Import a module for side-effects only Though not recommended practice, some modules set up some global state that can be used by other modules. These modules may not have any exports, or the consumer is not interested in any of their exports. To import these modules, use:

import "./my-module.js";

How have I misunderstood that?

1
  • t1 needs to be exported from Test.js so that any other file can then import it. Commented Apr 17, 2018 at 22:25

2 Answers 2

2

Change Test.js to this:

//Test.js

function t1() {
    console.log("t1");
}

module.exports = t1;

And then do something more like this in main.js:

const t1 = require("./Test.js");
t1(); // prints "t1"

There's a lot of information about how modules work in the docs: https://nodejs.org/api/modules.html

Alternatively, if you want t1 to be a global, then assign it to global.t1 in Test.js:

//Test.js

global.t1 = function t1() {
    console.log("t1");
};

I wouldn't recommend that if you can avoid it, though, for all the reasons people recommend avoiding globals when possible

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

Comments

1

Require doesn't work quite like that, but you're close -- if you want to use a function you've created to in another file, just add it to that file's exports.

/// test.js
exports.t1 = function() ...

// or

module.exports = {
  t1: function() ...
}

Then you need to specifically save that off to use it

/// main.js
var t1 = require('./test.js').t1;

t1();

Global scoping doesn't work like it does in the browser, check out node's docs on it, or try a blog explaining it (I didn't write this and can't fully vouch)

Comments

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.