Node v12: module is not defined when exporting - javascript

I export a function in my index.js
module.exports = {
myFunc
};
and I run node index.js I got error of
module.exports = {
^
ReferenceError: module is not defined
Is there any new way to exports something in node version 12?

Looks like your app is using es-modules which means you need to use export/import instead of module.exports/require. Here's how you could fix it:
const myFunc = () => {
console.log("test");
}
export {myFunc};
You can then import the function using:
import {myFunc} from './index.js'
myFunc();

Related

Bypass some of the require() statements in the testing file

I want to write unit tests for myFunction() in the server.js file:
Main file: server.js
const config = require('./configuration'); // <==== local js file that breaks when unit testing
const other = require('some-module'); // <==== some module that is needed and it does not break when testing.
module.exports { myFunction };
function myFunction() {
// ...
}
function someOtherFunctions() { /*...*/ }
Test file: server.test.js:
const server = require('../server'); // <=== one level up in the folder hierarchy
it('should work', () => {
});
Problem:
jest breaks at const config = require('./configuration') and I don't actually need this to test myFunction.
However, I need the second: require('some-module').
I need to mock or bypass the first const config = require('./configuration').
Question:
How can I only import the myFunction() from server.js and somehow mock the first require(...) statements in the server.js ?

How to call function from other local file Node.js?

I tried like this from main.js
// Modules to control application life and create native browser window
const {app, BrowserWindow} = require('electron')
const parser = require('./parser.js');
parser.parseHash(url)
But got error that: parser.parseHash is not function
// parser.js file
function parseHash(url) {
...
}
Are you exporting parseHash function in parser.js file?
module.exports = {
parseHash,
}
// parser.js
exports.parseHash = function (url) {
...
};
//main.js
const parseHash = require('./parser').parseHash;
parseHash(url)
You need to export the function like this:
// parser.js file
function parseHash(url) {
...
}
module.exports = {
parseHash: parseHash
}

ES6 imports are not called

I got problem with this code:
/* src/app.js */
import axios from 'axios'
export const run = async () => {
console.log(axios)
}
/* test/app.js */
import {run} from ../src/app
describe('#run', () => {
it('should work', function () {
run()
})
})
When I run the code I get this error:
ReferenceError: axios is not defined
I'm also using Babel, but I don't think it's relevant.
I really don't get why axios is not defined there. With old-style require syntax it works correctly, why it doesn't with ES6?
Code is run with Node.js.

Module Import error - cannot find module

I was trying to import the module dynamically in ES6 on Meteor application. But getting an error cannot find module. The same import works when I'm using the static import statement.
Please have a look at the below code -
const JOBS = ['update-report-cron'];
const jobs = {
start() {
JOBS.forEach((job) => {
console.log(`job ${job} has been started`);
let fileName = './' + job + '.js';
console.log(require(fileName));
})
}
};
module.exports = {jobs};
ERROR - Cannot find module './update-report-cron.js'
Try
export default const jobs = {
// your code
}
When you import, use
import { jobs } from './update-report-cron.js'
Ru's answer is not correct, not sure how it's your answer.
Try
export default const jobs = {
// your code
}
When you import, use
import jobs from './update-report-cron.js'

Require not behaving as expected

I'm using the proxyquire library, which mocks packages on import.
I'm creating my own proxyquire function, which stubs a variety of packages I use regularly and want to stub regularly (meteor packages, which have a special import syntax):
// myProxyquire.js
import proxyquire from 'proxyquire';
const importsToStub = {
'meteor/meteor': { Meteor: { defer: () => {} } },
};
const myProxyquire = filePath => proxyquire(filePath, importsToStub);
export default myProxyquire;
Now I want to write a test of a file which uses one of these packages:
// src/foo.js
import { Meteor } from 'meteor/meteor'; // This import should be stubbed
export const foo = () => {
Meteor.defer(() => console.log('hi')); // This call should be stubbed
return 'bar';
};
And finally I test it like this:
// src/foo.test.js
import myProxyquire from '../myProxyquire';
// This should be looking in the `src` folder
const { foo } = myProxyquire('./foo'); // error: ENOENT: no such file
describe('foo', () => {
it("should return 'bar'", () => {
expect(foo()).to.equal('bar');
});
});
Note that my last 2 files are nested inside a subfolder src. So when I try to run this test, I get an error saying that the module ./foo couldn't be found, as it is being looked for in the "root" directory, where the myProxyquire.js file is, not the src directory as expected.
You might be able to work around that (expected) behaviour by using a module like caller-path to determine from which file myProxyquire was called, and resolving the passed path relative to that file:
'use strict'; // this line is important and should not be removed
const callerPath = require('caller-path');
const { dirname, resolve } = require('path');
module.exports.default = path => require(resolve(dirname(callerPath()), path));
However, I have no idea of this works with import (and, presumably, transpilers).

Categories

Resources