question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TSC fails to emit required files when run below node_modules

See original GitHub issue

TypeScript Version: 3.2.0-dev.20181107

Search Terms: node_modules

Code

/tmp/node_modules/tsc-test/tsconfig.json

{
  "compilerOptions": {
    "outDir": "dist"
  },
  "files": [
    "src/index.ts"
  ]
}

/tmp/node_modules/tsc-test/src/index.ts

import { A } from './A';

export const main = () => {
  console.log('A=', A);
};

/tmp/node_modules/tsc-test/src/A.ts

export const A = 'A';

Expected behavior:

Output files:

  • /tmp/node_modules/tsc-test/dist/index.js
  • /tmp/node_modules/tsc-test/dist/A.js

Actual behavior:

Output files:

/tmp/node_modules/tsc-test/dist/index.js

"use strict";
exports.__esModule = true;
var A_1 = require("./A");
exports.main = function () {
    console.log('A=', A_1.A);
};

No A.js is output

Notes:

I wanted to build a typescript project as part of an npm postinstall script, to enable installation from npm or from git. In the case of installation from npmjs, dist/ will already exist. In the case of installation from git, dist/ will be missing and build will be run.

After setting this up, I noticed that only files listed in my tsconfig files list were being emitted when installing from git. I looked around for tsconfig or tsc options that may change this behavior, but realized it probably had to do with 'node_modules' being in the path.

I debugged the issue by modifying the built tsc.js. In my case, the problematic code is in nodeModuleNameResolverWorker/tryResolve, specifically here:

const { path: candidate, parts } = normalizePathAndParts(combinePaths(containingDirectory, moduleName));
const resolved = nodeLoadModuleByRelativeName(extensions, candidate, /*onlyRecordFailures*/ false, state, /*considerPackageJson*/ true);
// Treat explicit "node_modules" import as an external library import.
return resolved && toSearchResult({ resolved, isExternalLibraryImport: contains(parts, "node_modules") });

The call to contains(parts, "node_modules") should likely consider the project root directory, and ignore "node_modules" in the path parts above the root.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
weswighamcommented, Nov 9, 2018

Right, what I’m getting at is that if the project root is /a/b/node_modules/c/d, anything beneath /a/b/node_modules/c/d should be built. /a/b/node_modules/c shouldn’t be, /a/b/node_modules/c/d/node_modules/e shouldn’t be.

1reaction
DanielRosenwassercommented, Dec 10, 2018

Since @weswigham seems to have some thoughts around this, we can discuss this in a backlog slog in the future

Read more comments on GitHub >

github_iconTop Results From Across the Web

Getting [TS] errors for files inside the node_modules folder
In my case, both exclude and skipLibCheck were set, but it still tried to compile files in node_modules. In the end, I found...
Read more >
ts-loader - npm
Start using ts-loader in your project by running `npm i ts-loader`. ... First, for ts-loader to produce sourcemaps, you will need to set...
Read more >
A Complete Guide to Using TypeScript in Node.js - Better Stack
We need to set up a configuration file ( tsconfig.json ) for our project before we can start compiling our source files. If...
Read more >
Typescript – How to solve the problem with unresolved path ...
This is a somehow detailed description of a well know issue related to the fact that the tsc compiler (transpiler) does not resolve/emit...
Read more >
Npm install command failing with message "could not find ...
I was following the project setup. When I run the npm install command, npm starts downloading the dependencies. But then, suddenly, it stops ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found