Reputation: 3557
Im working with a sample creating a simple module. Using requirejs in node i want to check if im running in node but cant seem to get it to show that i am. Here is my code:
define('defTemp', [],
function()
{
"use strict";
var self = {};
self.checkA = function()
{
if(typeof exports !== 'undefined' && this.exports !== exports)
console.log('a');
if (typeof module !== 'undefined' && module.exports)
console.log('b');
if (typeof exports === 'object')
console.log('c');
if(typeof define === 'function' && define.amd)
console.log('d');
}
return self;
});
and is ran from app.js:
var requirejs = require('requirejs');
console.log(__dirname);
requirejs.config(
{
baseUrl: __dirname,
paths:
{
'defTemp' : 'template/defTemp'
},
nodeRequire: require
});
requirejs(['defTemp'],
function(defTemp)
{
defTemp.checkA();
});
None of the console (a,b,c) shows except d which makes sense because im using requirejs. But I'm also running in node. I want in the method checkA() to be able to tell if im running in node so I can do something different then if running in the browser. Any help?
Updated: 12/4/2014 Suggestion from Leonid Beschastny is to check for window instead which work great
Upvotes: 0
Views: 379
Reputation: 51460
I would suggest checking for window
:
if (typeof window === 'undefined') {
// node.js
else {
// browser
}
of for module
:
if (typeof module !== 'undefined') {
// node.js
else {
// browser
}
Upvotes: 1