-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Building projects with @nrwl/js:tsc and @nrwl/js:swc fails when "moduleResolution": "nodenext", "type": "module", and "module": "ESNext", with error TS2307: Cannot find module or its corresponding type declarations. #15464
Comments
I fixed this with the following diff for This is not super resilient, could use some more checks etc, but it works for me. If there is interest in a PR that implements this I'd be happy to provide one :) diff --git a/src/utilities/buildable-libs-utils.js b/src/utilities/buildable-libs-utils.js
index 1ec6eb948138ebb4ea2b86cf286a6ca99d38fc38..ac23420a413f324ea81ae2d1ec79c7788d577d0e 100644
--- a/src/utilities/buildable-libs-utils.js
+++ b/src/utilities/buildable-libs-utils.js
@@ -33,42 +33,42 @@ function calculateProjectDependencies(projGraph, root, projectName, targetName,
}
const dependencies = collectedDeps
.map(({ name: dep, isTopLevel }) => {
- let project = null;
- const depNode = projGraph.nodes[dep] || projGraph.externalNodes[dep];
- if (depNode.type === 'lib') {
- if (isBuildable(targetName, depNode)) {
- const libPackageJsonPath = (0, path_1.join)(root, depNode.data.root, 'package.json');
+ let project = null;
+ const depNode = projGraph.nodes[dep] || projGraph.externalNodes[dep];
+ if (depNode.type === 'lib') {
+ if (isBuildable(targetName, depNode)) {
+ const libPackageJsonPath = (0, path_1.join)(root, depNode.data.root, 'package.json');
+ project = {
+ name: (0, fileutils_1.fileExists)(libPackageJsonPath)
+ ? (0, devkit_1.readJsonFile)(libPackageJsonPath).name // i.e. @workspace/mylib
+ : dep,
+ outputs: (0, devkit_1.getOutputsForTargetAndConfiguration)({
+ overrides: {},
+ target: {
+ project: projectName,
+ target: targetName,
+ configuration: configurationName,
+ },
+ }, depNode),
+ node: depNode,
+ };
+ }
+ else {
+ nonBuildableDependencies.push(dep);
+ }
+ }
+ else if (depNode.type === 'npm') {
project = {
- name: (0, fileutils_1.fileExists)(libPackageJsonPath)
- ? (0, devkit_1.readJsonFile)(libPackageJsonPath).name // i.e. @workspace/mylib
- : dep,
- outputs: (0, devkit_1.getOutputsForTargetAndConfiguration)({
- overrides: {},
- target: {
- project: projectName,
- target: targetName,
- configuration: configurationName,
- },
- }, depNode),
+ name: depNode.data.packageName,
+ outputs: [],
node: depNode,
};
}
- else {
- nonBuildableDependencies.push(dep);
+ if (project && isTopLevel) {
+ topLevelDependencies.push(project);
}
- }
- else if (depNode.type === 'npm') {
- project = {
- name: depNode.data.packageName,
- outputs: [],
- node: depNode,
- };
- }
- if (project && isTopLevel) {
- topLevelDependencies.push(project);
- }
- return project;
- })
+ return project;
+ })
.filter((x) => !!x);
dependencies.sort((a, b) => (a.name > b.name ? 1 : b.name > a.name ? -1 : 0));
return {
@@ -116,10 +116,37 @@ function readTsConfigWithRemappedPaths(tsConfig, generatedTsConfigPath, dependen
*/
function computeCompilerOptionsPaths(tsConfig, dependencies) {
const paths = readPaths(tsConfig) || {};
- updatePaths(dependencies, paths);
+ const moduleResolution = readModule(tsConfig) || 0
+ const needsDirectResolution = moduleResolution > 2
+ updatePaths(dependencies, paths, needsDirectResolution);
return paths;
}
exports.computeCompilerOptionsPaths = computeCompilerOptionsPaths;
+function readModule(tsConfig) {
+ var _a;
+ if (!tsModule) {
+ tsModule = (0, typescript_1.ensureTypescript)();
+ }
+ try {
+ let config;
+ if (typeof tsConfig === 'string') {
+ const configFile = tsModule.readConfigFile(tsConfig, tsModule.sys.readFile);
+ config = tsModule.parseJsonConfigFileContent(configFile.config, tsModule.sys, (0, path_1.dirname)(tsConfig));
+ }
+ else {
+ config = tsConfig;
+ }
+ if ((_a = config.options) === null || _a === void 0 ? void 0 : _a.moduleResolution) {
+ return config.options.moduleResolution;
+ }
+ else {
+ return null;
+ }
+ }
+ catch (e) {
+ return null;
+ }
+}
function readPaths(tsConfig) {
var _a;
if (!tsModule) {
@@ -164,7 +191,7 @@ function cleanupTmpTsConfigFile(tmpTsConfigPath) {
function checkDependentProjectsHaveBeenBuilt(root, projectName, targetName, projectDependencies) {
const missing = findMissingBuildDependencies(root, projectName, targetName, projectDependencies);
if (missing.length > 0) {
- console.error((0, devkit_1.stripIndents) `
+ console.error((0, devkit_1.stripIndents)`
It looks like all of ${projectName}'s dependencies have not been built yet:
${missing.map((x) => ` - ${x.node.name}`).join('\n')}
@@ -193,15 +220,24 @@ function findMissingBuildDependencies(root, projectName, targetName, projectDepe
return depLibsToBuildFirst;
}
exports.findMissingBuildDependencies = findMissingBuildDependencies;
-function updatePaths(dependencies, paths) {
+function updatePaths(dependencies, paths, moduleResolution) {
const pathsKeys = Object.keys(paths);
// For each registered dependency
dependencies.forEach((dep) => {
var _a;
// If there are outputs
if (dep.outputs && dep.outputs.length > 0) {
- // Directly map the dependency name to the output paths (dist/packages/..., etc.)
- paths[dep.name] = dep.outputs;
+ // if moduleResolution is node16 or nodenext, we need to add a direct path to the output file
+ // we do this by looking at the main entry of the package.json file of the already compiled package, as its the most reliable way
+ if (moduleResolution) {
+ const outputPackageJSON = (0, path_1.join)(dep.outputs[0], 'package.json')
+ const { main, module, exports } = (0, devkit_1.readJsonFile)(outputPackageJSON)
+ const entry = exports?.['.']?.['import'] ?? module ?? main ?? './index.js'
+ paths[dep.name] = [(0, path_1.join)(dep.outputs[0], entry)]
+ } else {
+ // Directly map the dependency name to the output paths (dist/packages/..., etc.)
+ paths[dep.name] = dep.outputs;
+ }
// check for secondary entrypoints
// For each registered path
for (const path of pathsKeys) { |
Scratch that actually, that didn't work at all |
I was having issues using I kinda solved my issue using this patch: diff --git a/node_modules/tsconfig-paths/lib/register.js b/node_modules/tsconfig-paths/lib/register.js
index e1cda8b..316c197 100644
--- a/node_modules/tsconfig-paths/lib/register.js
+++ b/node_modules/tsconfig-paths/lib/register.js
@@ -95,6 +95,9 @@ function register(params) {
var isCoreModule = coreModules.hasOwnProperty(request);
if (!isCoreModule) {
var found = matchPath(request);
+ if (!found && request.startsWith('./lib/') && request.endsWith('.js')) {
+ found = request.replace('.js', '');
+ }
if (found) {
var modifiedArguments = __spreadArray([found], [].slice.call(arguments, 1), true); // Passes all arguments. Even those that is not specified above.
return originalResolveFilename.apply(this, modifiedArguments); However What helped me is changing from |
This issue has been automatically marked as stale because it hasn't had any recent activity. It will be closed in 14 days if no further activity occurs. |
having the same issue |
I found it. In my case, the problem was in name of package in |
Hmmm.. I have lots of feature libraries that have multiple "/" in the name and they work fine? |
This should work, as demonstrated here https://github.com/jaysoo/issue-15464. Please try with the newest Nx version, and open a new issue with repro if you have further problems. |
This issue has been closed for more than 30 days. If this issue is still occuring, please open a new issue with more recent context. |
Current Behavior
When I try to build a project instantiated with
@nrwl/js:lib
withmoduleResolution: nodenext
(ornode16
) in mytsconfig.base.json
, the build always fails claiming that it cannot find one of the dependent modules that are listed in the paths oftsconfig.base.json
.Specifically this only seems to happen if I try to create an ESM only module, if I compile to
commonjs
everythings fine.When running
tsc -P libs/test/tsconfig.lib.json
the project builds as expected with no errors.Specifically, when enabling
traceResolution: true
, I can see that when runningnx build test
(either when using@nrwl/js:tsc or @nrwl/swc:tsc
), it tries to resolve the dependent moduleRunning
pnpm tsc -P libs/test/tsconfig.lib.json
Running
pnpm nx build test
Expected Behavior
The build step should resolve the modules correctly.
GitHub Repo
https://github.com/tefkah/nx-nodenext-repro
Steps to Reproduce
pnpm install
pnpm nx build test
pnpm tsc -P libs/test/tsconfig.lib.json
to see correctly functioning compilation.tsc-log.txt
andnx-log.txt
for the logs of the operations.Nx Report
Failure Logs
Additional Information
tsconfig.base.json
libs/test/project.json['targets']['build']
libs/test/tsconfig.json
libs/test/tsconfig.lib.json
libs/test/package.json
The text was updated successfully, but these errors were encountered: