![Maven Central Adds Sigstore Signature Validation](https://cdn.sanity.io/images/cgdhsj6q/production/7da3bc8a946cfb5df15d7fcf49767faedc72b483-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Maven Central Adds Sigstore Signature Validation
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.
import-sync
Advanced tools
Synchronously import dynamic ECMAScript Modules similar to CommonJS require. Basic wrapper around esm for compatibility with both ESM and CJS projects in NodeJS.
Synchronously import dynamic ECMAScript Modules similar to CommonJS require
Basic wrapper around esm for compatibility with both ESM and CJS projects in NodeJS
Capable of importing ESM-only libraries such as node-fetch@3 in CJS projects
npm install import-sync
Try with Replit.
importSync(id, options);
Importing from the same directory
const { someVariable, someFunction } = importSync('./some-module');
Importing .mjs
file from a different directory
const { someFunction } = importSync('../src/someModule.mjs');
Using a different basePath
const { someFunction } = importSync(
'./someModule',
{ basePath: process.cwd() }
);
Using additional esm options as described in esm's documentation
const { someFunction } = importSync(
'./someModule',
{
esmOptions: {
cjs: {
cache: true
},
mode: 'all',
force: 'true',
}
}
);
Importing an ESM-only module
const fetch = importSync('node-fetch'),
Module name or relative path similar to CommonJS require. For example,
'../animals/cats.js'
'./dogs.mjs'
'./minimal'
importSync
will look for matching extensions in the order [.js, .mjs, .cjs, .ts]
'node-fetch'
importSync
can import pure-esm node-fetch (v3) into your cjs projectOption | Description | Example | Default |
---|---|---|---|
basePath |
This will only take effect if the given id starts with ./ or ../ .
For example,
|
./myModule | __dirname |
esmOptions | Options for the esm module as described in esm's documentation. |
{ cjs: true, mode: 'auto' } | undefined |
The importSync
function returns the exported module content similar to NodeJS
require.
If an unknown file path is provided a default Error object is thrown.
Copyright (c) 2023 Khiet Tam Nguyen
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the “Software”),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
There are currently no known limitations.
Please note that loading ECMAScript Modules using require() are soon to be supported in NodeJS natively.
import-sync was created to enable the implementation of a global dryrun script that can be run by students undertaking COMP1531 Software Engineering Fundamentals 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 at the path ~cs1531/bin
. Students who are connected to the CSE lab environment (e.g. via VLAB) can run the dryrun script from their major project repository, e.g. at the path ~z5313514/comp1531/project-backend
.
Initially, the esm library looked promising. However, when the global dryrun script was executed in a mock student's project directory, the following error occurred:
Error [ERR_REQUIRE_ESM]: require() of ES Module /import/ravel/5/z5313515/project-backend/src/auth.js not supported.
Instead change the require of auth.js in null to a dynamic import() which is available in all CommonJS modules
This is due to the package.json
containing "type": "module"
, as iteration 1 of the student major project uses ESM for the seamless transition to future iterations.
The following approaches were thus attempted, but were unsatisfactory for our purpose:
.js
to all of their file imports in the project solely to use the dryrun. This resulted in ambiguous error messages and obscure dryrun requirements unrelated to the projectUpon a more thorough investigation into the initial issue with the esm module, the cause was the introduction of the exception starting from NodeJS version 13, as noted in @fregante's comment:
Further down the thread was a link to the solution by @guybedford
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.
Another issue that import-sync (v2) addresses is esm's open issue #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 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):
import http from 'node:http';
to
import http from 'http';
for all imported modules.
For further discussions about this issue, visit:
As of version 2.2.0, import-sync has switched from using the archived esm package to the fork @httptoolkit/esm. For further details, please see
FAQs
Synchronously import dynamic ECMAScript Modules similar to CommonJS require. Basic wrapper around esm for compatibility with both ESM and CJS projects in NodeJS.
The npm package import-sync receives a total of 572 weekly downloads. As such, import-sync popularity was classified as not popular.
We found that import-sync demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.
Security News
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
Research
Security News
Socket researchers uncovered a backdoored typosquat of BoltDB in the Go ecosystem, exploiting Go Module Proxy caching to persist undetected for years.