https://github.com/leegeunhyeok/node-self
🤙 self property for Node.js
https://github.com/leegeunhyeok/node-self
browser nodejs
Last synced: 21 days ago
JSON representation
🤙 self property for Node.js
- Host: GitHub
- URL: https://github.com/leegeunhyeok/node-self
- Owner: leegeunhyeok
- License: mit
- Created: 2020-05-13T07:38:16.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2023-10-18T20:18:27.000Z (over 1 year ago)
- Last Synced: 2025-03-25T08:02:29.155Z (about 1 month ago)
- Topics: browser, nodejs
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/node-self
- Size: 192 KB
- Stars: 7
- Watchers: 3
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# node-self
🤙 self property in Node.js[](https://badge.fury.io/js/node-self)
[](https://badge.fury.io/js/node-self)## Table of contents
- [Features](#features)
- [Compatibility](#compatibility)
- [How to use?](#how-to-use)
- [How it works?](#how-it-works)
- [License](#license)## What is this?
You can use `self` property in Node.js via this module.
```javascript
// In Browser (Window context)
console.log(self); // Window { ... }// In Browser (Dedicate worker context)
console.log(self); // DedicatedWorkerGlobalScope { ... }// But, in Node.js
console.log(self); // ReferenceError: self is not defined
```> Why?: `self` property is not implemented in Node.js.
## Features
You can use `self` property in Node.js.
- Super lightweight
- Pure Javascript
- Zero dependencies### Compatibility
Both Browser and Node.js.
## How to use?
Just use `self`. It's returns appropriate `Global Object` (Context Dependent)
Import [index.js](./index.js)
```javascript
// CommonJS
require('node-self');// ES6+
import 'node-self';
```or paste this code on the top
```javascript
void !function () {
typeof self == 'undefined'
&& typeof global == 'object'
&& (global.self = global);
}();
```There are can be `self` (Global Object)
- Browser
- Window
- WorkerGlobalScope
- DedicatedWorkerGlobalScope
- SharedWorkerGlobalScope
- ServiceWorkerGlobalScope
- Node.js
- global```javascript
self;// Browser
self === window; // true// Node.js
self === global; // true
```## How it works?
This project using `typeof` operator trickly.
`typeof` operator always return a string.
Even with undeclared identifiers, it will return `'undefined'` instead of throwing an error.```js
undeclared; // ReferenceError: undeclared is not definedtypeof undeclared; // 'undefined'
```- [MDN Docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof#interaction_with_undeclared_and_uninitialized_variables)
- [ECMA-262 Spec](https://tc39.es/ecma262/multipage/ecmascript-language-expressions.html#sec-typeof-operator)---
- `typeof self == 'undefined'`: Check existing `self` identifier (should be undefined)
- `typeof global == 'object'`: Check current context is Node.js
- `global.self = global`: Define `self` and assign reference of global object## License
[MIT](./LICENSE)