Skip to content

Commit

Permalink
2.0.2 - referencing issue standard-things/esm#904
Browse files Browse the repository at this point in the history
  • Loading branch information
nktnet committed Oct 20, 2023
1 parent d19e6eb commit 9723b65
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
27 changes: 21 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@

Synchronously import dynamic ECMAScript Modules similar to CommonJS [require](https://nodejs.org/api/modules.html#requireid)

Basic wrapper around [esm](https://github.com/standard-things/esm) for compatibility with both ES6 and CJS projects in NodeJS
Basic wrapper around [esm](https://github.com/standard-things/esm) for compatibility with both ESM and CJS projects in NodeJS

Capable of importing pure-esm libraries such as [node-fetch@3](https://github.com/node-fetch/node-fetch#commonjs) in CJS projects
Capable of importing ESM-only libraries such as [node-fetch@3](https://github.com/node-fetch/node-fetch#commonjs) in CJS projects

[![Try with Replit](https://replit.com/badge?caption=Try%20with%20Replit)](https://replit.com/@nktnet1/import-sync-example#index.js)

Expand Down Expand Up @@ -250,9 +250,8 @@ instead when importing CommonJS modules.

### 5.1. Idea

**import-sync** was created to enable the implementation of a global dryrun
script that can be run by students undertaking
[COMP1531 Software Engineering Fundamentals](https://webcms3.cse.unsw.edu.au/COMP1531/23T2/outline) in their major group project. This requires the ability to import external ES6 modules from any directory or path for use in both CommonJS and ES6 programs.
**import-sync** was created to enable the implementation of a global dryrun script that can be run by students undertaking
[COMP1531 Software Engineering Fundamentals](https://webcms3.cse.unsw.edu.au/COMP1531/23T2/outline) in their major group project. This requires the ability to import external ES Modules from any directory or path for use in both CommonJS and ESM-based projects.

The dryrun serves as a sanity check before the
final submission is made, and is located in the centralised [COMP1531 course account](https://taggi.cse.unsw.edu.au/FAQ/Uploading_to_course_accounts/) at the path `~cs1531/bin`. Students who are connected to the CSE lab environment (e.g. via [VLAB](https://taggi.cse.unsw.edu.au/FAQ/VLAB_-_The_technical_details/)) can run the dryrun script from their major project repository, e.g. at the path `~z5313514/comp1531/project-backend`.
Expand Down Expand Up @@ -296,4 +295,20 @@ introduction of the exception starting from NodeJS version 13, as noted in
Further down the thread was a link to the solution by [@guybedford](https://github.com/guybedford)
- https://github.com/standard-things/esm/issues/868#issuecomment-594480715

which removes the exception through module extension and serves as a satisfactory workaround. This reduced the codebase of **import-sync** to simply a wrapper around [esm](https://www.youtube.com/watch?v=jQS-nEFxJeU).
which removes the exception through module extension and serves as a satisfactory workaround. This reduced the codebase of **import-sync** to simply a wrapper around [esm](https://github.com/standard-things/esm).

Another issue that **import-sync** (v2) addresses is [esm's open issue #904](https://github.com/standard-things/esm/issues/904), which yields the error message:
> Error [ERR_INVALID_PROTOCOL]: Protocol 'node:' not supported. Expected 'file:'
when importing ESM-only libraries such as [node-fetch@3](https://github.com/node-fetch/node-fetch/blob/8b3320d2a7c07bce4afc6b2bf6c3bbddda85b01f/README.md#commonjs) in a CommonJS module. This is done by overriding the default `Module._resolveFilename` function to remove the `node:` prefix, effectively changing any imports of the form (for example):
```javascript
import http from 'node:http';
```
to
```javascript
import http from 'http';
```
for all imported modules.

For further discussions about this issue, visit:
- https://stackoverflow.com/a/77329422/22324694
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"type": "git",
"url": "https://github.com/nktnet1/import-sync"
},
"version": "2.0.1",
"version": "2.0.2",
"files": [
"dist"
],
Expand Down Expand Up @@ -40,7 +40,7 @@
],
"author": "Khiet Tam Nguyen",
"license": "MIT",
"description": "Synchronously import dynamic ECMAScript Modules similar to CommonJS require. Basic wrapper around esm for compatibility with both ES6 and CJS projects in NodeJS.",
"description": "Synchronously import dynamic ECMAScript Modules similar to CommonJS require. Basic wrapper around esm for compatibility with both ESM and CJS projects in NodeJS.",
"devDependencies": {
"@types/esm": "^3.2.1",
"@types/jest": "^29.5.6",
Expand Down

0 comments on commit 9723b65

Please sign in to comment.