node脚本 兼容运行ES6模块化规范代码【日常tips】

此类博客不多BB,复制粘贴运行一把梭。

1.package.json

{
    
    
  "scripts": {
    
    
    "script": "node entry.js"
  },
  "devDependencies": {
    
    
    "@babel/core": "^7.1.2",
    "@babel/plugin-transform-modules-commonjs": "^7.2.0",
    "@babel/register": "^7.0.0"
  }
}

2.entry.js

require('@babel/register')({
    
    
  plugins: ['@babel/plugin-transform-modules-commonjs']
});
require('./script'); // 脚本入口

3.script.js

import zhCN from './zh-CN';
import enUS from './en-US';
var fs = require("fs");
// xxx

4.运行

npm run script

猜你喜欢

转载自blog.csdn.net/jw2268136570/article/details/109291617