You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If not specified, the default value will be the current file's directory.
@@ -81,7 +78,7 @@ If not specified, the default value will be the current file's directory.
81
78
82
79
Underneath the hood, `module-from-string` uses Node.js built-in `vm` module to execute code.
83
80
84
-
```ts
81
+
```js
85
82
// requireFromString
86
83
87
84
vm.runInNewContext(
@@ -96,7 +93,9 @@ vm.runInNewContext(
96
93
},
97
94
```
98
95
99
-
Take `requireFromString` for example, only the above variables are passed into the `contextObject`. In order to use other global objects and built-in modules you need to add them to option `globals`.
96
+
Take `requireFromString` for example, only the above module scope variables are passed into the `contextObject`.
97
+
98
+
In order to use other [global objects](https://nodejs.org/api/globals.html) that specific to Node.js, they need to be added to option `globals` **or** set option [`useCurrentGlobal`](#usecurrentglobal) to `true`.
100
99
101
100
```js
102
101
requireFromString(
@@ -105,6 +104,17 @@ requireFromString(
105
104
) // => $PWD
106
105
```
107
106
107
+
**Note**: by default the [built-in objects](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects) have a different prototype.
108
+
109
+
```js
110
+
consterr=requireFromString('module.exports = new Error()')
111
+
err instanceofError// => false
112
+
```
113
+
114
+
### useCurrentGlobal
115
+
116
+
Default to `false`. If set to `true`, all the available variables from the current `global` (or `globalThis`) are passed into the context.
117
+
108
118
### transformOptions
109
119
110
120
Function `importFromString` and `importFromStringSync` can use `esbuild` to transform code syntax. See [esbuild Transform API](https://esbuild.github.io/api/#transform-api) for documentation.
0 commit comments