sheerlox / import-from-esm Goto Github PK
View Code? Open in Web Editor NEWAlmost drop-in "import-from" replacement that supports loading both ESM & CJS modules
License: MIT License
Almost drop-in "import-from" replacement that supports loading both ESM & CJS modules
License: MIT License
Fuzz testing takes a lot of time to run, and decreases the feedback loop of the test workflow running on PRs significantly.
Running it as a separate workflow (e.g. on main
commits) would also allow running it for longer, improving its benefits.
Working on: https://github.com/straw-hat-team/nodejs-monorepo/pull/119/files
I had to do the following:
// @ts-expect-error
import importFrom from "import-from-esm";
Since for some reason the type system is confused:
yarn build
src/openapi-web-sdk-generator.ts:2:8 - error TS1192: Module '"/Users/ubi/Developer/github.com/straw-hat-team/nodejs-monorepo/.yarn/cache/import-from-esm-npm-1.2.1-19132a3a23-17275e7d51.zip/node_modules/import-from-esm/index"' has no default export.
2 import importFrom from "import-from-esm";
~~~~~~~~~~
Found 1 error in src/openapi-web-sdk-generator.ts:2
Any thoughts?
npm ERR! code 2
npm ERR! path /home/rowlands/git/vite-plugin-twig-drupal/node_modules/import-from-esm
npm ERR! command failed
npm ERR! command sh -c cd tests/fixture/nested/ && npm ci
npm ERR! sh: 1: cd: can't cd to tests/fixture/nested/
I am using pnp
linker as follow:
v20.10.0
# .yarnrc.yml
compressionLevel: mixed
defaultSemverRangePrefix: ''
enableGlobalCache: false
nodeLinker: pnp
yarnPath: .yarn/releases/yarn-4.0.2.cjs
I am using import-from-esm
at https://github.com/straw-hat-team/nodejs-monorepo/tree/master/packages/%40straw-hat/openapi-web-sdk-generator
Here is the config:
# .openapi-web-sdk-generatorrc.yaml
generators:
- path: '@straw-hat/openapi-web-sdk-generator/dist/generators/fetcher/index.js'
config:
outputDir: 'src/lib/services/flashbot-api/operations'
- path: '@straw-hat/openapi-web-sdk-generator/dist/generators/react-query-fetcher/index.js'
config:
outputDir: 'src/lib/services/flashbot-api/react-query'
packageName: '../operations'
And the package.json
{
"packageManager": "[email protected]",
"engines": {
"node": "20.x"
},
"type": "module",
"scripts": {
"codegen:sdk": "sht-openapi-web-sdk-generator local --config='./openapi.json'"
}
}
When I do DEBUG=* yarn codegen:sdk
, I get the following outcome:
@straw-hat/openapi-web-sdk-generator:helpers Configuration file /Users/ubi/Developer/myapp/.openapi-web-sdk-generatorrc.yaml loaded +0ms
@straw-hat/openapi-web-sdk-generator:open-api-web-sdk-generator Loading Generator: @straw-hat/openapi-web-sdk-generator/dist/generators/fetcher/index.js +0ms
import-from-esm Executing importFrom('/Users/ubi/Developer/myapp', '@straw-hat/openapi-web-sdk-generator/dist/generators/fetcher/index.js') +0ms
import-from-esm '@straw-hat/openapi-web-sdk-generator/dist/generators/fetcher/index.js' is not a file module +0ms
import-from-esm Trying to resolve '@straw-hat/openapi-web-sdk-generator/dist/generators/fetcher/index.js' from 'file:///Users/ubi/Developer/myapp/noop.js' +0ms
import-from-esm Failed to resolve '@straw-hat/openapi-web-sdk-generator/dist/generators/fetcher/index.js' from 'file:///Users/ubi/Developer/myapp/noop.js': Error [ERR_MODULE_NOT_FOUND]: Cannot find package '@straw-hat/openapi-web-sdk-generator' imported from /Users/ubi/Developer/myapp/noop.js +1ms
import-from-esm Trying to resolve '@straw-hat/openapi-web-sdk-generator/dist/generators/fetcher/index.js.js' from 'file:///Users/ubi/Developer/myapp/noop.js' +0ms
import-from-esm Failed to resolve '@straw-hat/openapi-web-sdk-generator/dist/generators/fetcher/index.js.js' from 'file:///Users/ubi/Developer/myapp/noop.js': Error [ERR_MODULE_NOT_FOUND]: Cannot find package '@straw-hat/openapi-web-sdk-generator' imported from /Users/ubi/Developer/myapp/noop.js +1ms
import-from-esm Trying to resolve '@straw-hat/openapi-web-sdk-generator/dist/generators/fetcher/index.js.mjs' from 'file:///Users/ubi/Developer/myapp/noop.js' +0ms
import-from-esm Failed to resolve '@straw-hat/openapi-web-sdk-generator/dist/generators/fetcher/index.js.mjs' from 'file:///Users/ubi/Developer/myapp/noop.js': Error [ERR_MODULE_NOT_FOUND]: Cannot find package '@straw-hat/openapi-web-sdk-generator' imported from /Users/ubi/Developer/myapp/noop.js +0ms
import-from-esm Trying to resolve '@straw-hat/openapi-web-sdk-generator/dist/generators/fetcher/index.js.cjs' from 'file:///Users/ubi/Developer/myapp/noop.js' +0ms
import-from-esm Failed to resolve '@straw-hat/openapi-web-sdk-generator/dist/generators/fetcher/index.js.cjs' from 'file:///Users/ubi/Developer/myapp/noop.js': Error [ERR_MODULE_NOT_FOUND]: Cannot find package '@straw-hat/openapi-web-sdk-generator' imported from /Users/ubi/Developer/myapp/noop.js +0ms
import-from-esm Trying to resolve '@straw-hat/openapi-web-sdk-generator/dist/generators/fetcher/index.js.json' from 'file:///Users/ubi/Developer/myapp/noop.js' +0ms
import-from-esm Failed to resolve '@straw-hat/openapi-web-sdk-generator/dist/generators/fetcher/index.js.json' from 'file:///Users/ubi/Developer/myapp/noop.js': Error [ERR_MODULE_NOT_FOUND]: Cannot find package '@straw-hat/openapi-web-sdk-generator' imported from /Users/ubi/Developer/myapp/noop.js +1ms
import-from-esm Cannot find module '@straw-hat/openapi-web-sdk-generator/dist/generators/fetcher/index.js' +0ms
Error: Failed to load configuration file @straw-hat/openapi-web-sdk-generator/dist/generators/fetcher/index.js.
Cannot find module '@straw-hat/openapi-web-sdk-generator/dist/generators/fetcher/index.js'
If I change the linker to node-modules
then it works just fine, so something is broken using pnp
linker and this package.
Any thoughts?!
A good way to test import-from-esm
alignment with require
's specs would be to validate its outcome against require
's outcome using the same parameters (only ignoring ERR_REQUIRED_ESM
errors from require
).
This library is intended to be an almost drop-in replacement of sindresorhus/import-from (from which it is forked) that supports loading ESM & CJS modules like require()
does.
Since import-from
uses createRequire()
under the hood, it throws the ERR_REQUIRE_ESM
error when trying to import a pure ESM module.
The Node.js modules documentation states:
Due to the synchronous nature of require(), it is not possible to use it to load ECMAScript module files. Attempting to do so will throw a ERR_REQUIRE_ESM error. Use import() instead.
Switching from require()
to a dynamic import()
has a lot of implications, but these are the ones I encountered issues with:
import()
is asynchronous,This library will provide a module loading strategy as close to CJS' as possible, as well as work around the above issues (except the first one, nothing can be done about this).
It's only breaking change from import-from
should be adding an await
in front of the importFrom()
calls, and a .default
when importing default exports.
When an import throws an invalid module type for the file extension as follows:
This file is being treated as an ES module because it has a '.js' file extension and '/home/sherlox/workspace/OPENSOURCE/IMPORT_FROM/import-from-esm-repro-1/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
the error is caught by import-from-esm, which then throws the generic Cannot find module
error message.
import-from-esm
should not obfuscate this message, so the end user can understand the real cause of the issue.
Currently, the only way to see this error is by using DEBUG=import-from-esm
.
Steps to reproduce:
npm install
cd playground/
npm install
npx semantic-release
Add the ability to import an NPM package that contains an index.json
file or has a main
field pointing to a JSON file.
This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.
These updates have all been created already. Click a checkbox below to force a retry/rebase of any.
These are blocked by an existing closed PR and will not be recreated unless you click a checkbox below.
.github/workflows/codeql.yml
step-security/harden-runner v2.7.0@63c24ba6bd7ba022e95695ff85de572c04a18142
actions/checkout v4.1.1@b4ffde65f46336ab88eb53be808477a3936bae11
github/codeql-action v3.25.0@df5a14dc28094dc936e103b37d749c6628682b60
github/codeql-action v3.25.0@df5a14dc28094dc936e103b37d749c6628682b60
github/codeql-action v3.25.0@df5a14dc28094dc936e103b37d749c6628682b60
.github/workflows/release.yml
step-security/harden-runner v2.7.0@63c24ba6bd7ba022e95695ff85de572c04a18142
actions/checkout v4@b4ffde65f46336ab88eb53be808477a3936bae11
actions/setup-node v4@60edb5dd545a775178f52524783378180af0d1f8
.github/workflows/scorecard.yml
step-security/harden-runner v2.7.0@63c24ba6bd7ba022e95695ff85de572c04a18142
actions/checkout v4.1.1@b4ffde65f46336ab88eb53be808477a3936bae11
ossf/scorecard-action v2.3.1@0864cf19026789058feabb7e87baa5f140aac736
actions/upload-artifact v4.3.1@5d5d22a31266ced268874388b861e4b58bb5c2f3
github/codeql-action v3.25.0@df5a14dc28094dc936e103b37d749c6628682b60
.github/workflows/test.yml
insurgent-lab/is-in-pr-action v0.2.0@129df59687402c4a9c81a9a9e88d7448cdbba541
step-security/harden-runner v2.7.0@63c24ba6bd7ba022e95695ff85de572c04a18142
actions/checkout v4@b4ffde65f46336ab88eb53be808477a3936bae11
actions/setup-node v4@60edb5dd545a775178f52524783378180af0d1f8
actions/upload-artifact v4@5d5d22a31266ced268874388b861e4b58bb5c2f3
step-security/harden-runner v2.7.0@63c24ba6bd7ba022e95695ff85de572c04a18142
actions/checkout v4@b4ffde65f46336ab88eb53be808477a3936bae11
actions/download-artifact v4@c850b930e6ba138125429b7e5c93fc707a7f8427
SonarSource/sonarcloud-github-action v2.1.1@49e6cd3b187936a73b8280d59ffd9da69df63ec9
package.json
debug ^4.3.4
import-meta-resolve ^4.0.0
@fast-check/ava 1.2.1
@insurgent/export-map-test 1.0.1
@insurgent/json-index-test 2.0.0
@insurgent/json-main-test 2.0.0
@insurgent/subpaths-test 1.0.0
@insurgentlab/conventional-changelog-preset 7.0.0
@semantic-release/changelog 6.0.3
@semantic-release/git 10.0.1
@types/debug 4.1.12
ava 5.3.1
c8 9.1.0
cross-env 7.0.3
knip 4.6.0
semantic-release 23.1.1
tsd 0.31.0
xo 0.56.0
node >=16.20
As in Case 2 in semantic-release/commit-analyzer#589, import-from-esm
does not seem to resolve exports
in package.json correctly.
Case 2
It's about my contribution semantic-release/commit-analyzer#588. Perform the following steps on latest master branch:
npm i [email protected] -DAdd the new
test/presets.test.js
import test from "ava"; import { stub } from "sinon"; import { analyzeCommits } from "../index.js"; const cwd = process.cwd(); test.beforeEach((t) => { const log = stub(); t.context.log = log; t.context.logger = { log }; }); test('Accept "preset" option', async (t) => { const commits = [ { hash: "123", message: "Fix: First fix (fixes #123)" }, { hash: "456", message: "Update: Second feature (fixes #456)" }, ]; const releaseType = await analyzeCommits({ preset: "techor" }, { cwd, commits, logger: t.context.logger }); t.is(releaseType, null); });npm test
> Cannot find module 'conventional-changelog-techor'If I remove the
"exports": { ... }"
section from "node_modules/conventional-changelog-techor/package.json", it will work.So this is a problem with
import-from-esm
itself, regardless of whether it is a monorepo or not.When I change
import-from-esm
back toimport-from
everything still works. This means that the changes in v11.1 did not include tests for the new functionality (#537), and these tests would have failed before v11.1. Changes add a lot of uncertainty.
Ran into an issue when using semantic-release with a shared config (CommonJS) using subpaths.
With a destination package @example/package/subpath
, assuming the target file in the package @example/package
exists in ./subpath/index.js
, I get something like the following:
import-from-esm Trying to resolve '@example/package/subpath' from 'file:///Users/lukiffer/.npm/_npx/db57d9b03a028fc5/node_modules/semantic-release/lib/noop.js' +0ms
import-from-esm Failed to resolve '@example/package/subpath' from 'file:///Users/lukiffer/.npm/_npx/db57d9b03a028fc5/node_modules/semantic-release/lib/noop.js': Error [ERR_MODULE_NOT_FOUND]: Cannot find package '@example/package' imported from /Users/lukiffer/.npm/_npx/db57d9b03a028fc5/node_modules/semantic-release/lib/noop.js +0ms
If I instead move and rename the target file from ./subpath/index.js
to ./subpath.cjs
this works as expected.
I'm not sure if this is something that import-from
previously masked or if ./subpath/index.js
should actually be checked, and if so why it's failing to find the root package.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.