Skip to content

Commit 93d7fa7

Browse files
committed
docs(README): update
1 parent 8e647e6 commit 93d7fa7

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

README.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ npm install module-from-string
1616
## Usage
1717

1818
```js
19-
const { requireFromString, importFromString } = require('module-from-string')
19+
import { requireFromString, importFromString } from 'module-from-string'
2020

2121
requireFromString("module.exports = 'hi'") // => 'hi'
2222
requireFromString("exports.greet = 'hi'") // => { greet: 'hi' }
@@ -54,22 +54,23 @@ export { importFromString, importFromStringSync, requireFromString }
5454
Underneath the hood, `module-from-string` uses Node.js built-in `vm` module to execute code.
5555

5656
```ts
57-
const _module = new Module(String(new Date().valueOf()))
57+
const contextModule = new Module(nanoid())
5858

59-
const context = vm.createContext({
60-
exports: _module.exports,
61-
module: _module,
59+
vm.runInNewContext(code, {
60+
exports: contextModule.exports,
61+
module: contextModule,
6262
require,
6363
...globals
6464
})
65-
66-
vm.runInContext(code, context)
6765
```
6866

69-
By default, only above variables are passed into the `contextObject`. In order to use other global objects you need to add them to option `globals`.
67+
By default, 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`.
7068

7169
```js
72-
requireFromString('module.exports = process.cwd()', { process })
70+
requireFromString({
71+
code: 'module.exports = process.cwd()',
72+
globals: { process }
73+
})
7374

7475
importFromStringSync({
7576
code: 'export default process.cwd()',

0 commit comments

Comments
 (0)