관리 메뉴

필사(筆寫)

Node.vm 으로 하위 가상 모듈 만들기 사용법 메모 본문

Service Developments/Node.js,Express

Node.vm 으로 하위 가상 모듈 만들기 사용법 메모

코딩필사 2024. 7. 19. 17:59

https://nodejs.org/api/vm.html

 

VM (executing JavaScript) | Node.js v22.5.0 Documentation

VM (executing JavaScript)# Source Code: lib/vm.js The node:vm module enables compiling and running code within V8 Virtual Machine contexts. The node:vm module is not a security mechanism. Do not use it to run untrusted code. JavaScript code can be compiled

nodejs.org

 

가끔 코어단 설계시 사용하는 vm 설계 구문 메모해두기


engine.vm = async () => {
  // const script = new vm.Script(bundle, { filename: 'bundle.js' })
  // const sandbox = { require, module: {}, process, __dirname: path.dirname(entry), console }
  // const context = vm.createContext(sandbox)
  // script.runInContext(context)
  // return sandbox.module.exports.default;
}

 

cjs 때는 위와 같은 context 를 수반하고, esm는 import 구문을 주로 사용한다.