Skip to content

Commit

Permalink
ep 013
Browse files Browse the repository at this point in the history
  • Loading branch information
realtux committed Jun 9, 2023
1 parent dfb4199 commit 1a78ff2
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 0 deletions.
10 changes: 10 additions & 0 deletions _new/013-es-modules/commonjs/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const fs = require('fs');
const crypto = require('crypto');

const util = require('./util');

(async () => {
let contents = await util.open('package.json');

console.log(contents);
})();
3 changes: 3 additions & 0 deletions _new/013-es-modules/commonjs/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"description": "i'm a commonjs app"
}
11 changes: 11 additions & 0 deletions _new/013-es-modules/commonjs/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const fs = require('fs/promises');

module.exports = {

async open(file) {
let content = await fs.readFile(file);

return content.toString();
}

};
8 changes: 8 additions & 0 deletions _new/013-es-modules/esm/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import fs from 'fs';
import crypto from 'crypto';

import util from './util.js';

let contents = await util.open('package.json');

console.log(contents);
4 changes: 4 additions & 0 deletions _new/013-es-modules/esm/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"type": "module",
"description": "i'm an esm app"
}
11 changes: 11 additions & 0 deletions _new/013-es-modules/esm/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import fs from 'fs/promises';

export default {

async open(file) {
let content = await fs.readFile(file);

return content.toString();
}

};
16 changes: 16 additions & 0 deletions _new/013-es-modules/notes.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
prereqs:
- upgrade to at least node 12, 16 preferred

how to go esm:
- add "type": "module" to package.json
- change out calls to require() with import
- change out module.exports to export default
- remove top level self-executing functions
- remove 'use strict'; from everywhere

caveats:
- the above steps get you 90% the way

import * as url from 'url';
const __filename = url.fileURLToPath(import.meta.url);
const __dirname = url.fileURLToPath(new URL('.', import.meta.url));

0 comments on commit 1a78ff2

Please sign in to comment.