Why does the typescript compiler accept importing from a JS file in the local directory but raises an error when importing from node_modules
?
Code:
import { t2 } from "./t1.js"
t2.hello();
import { mat4 } from "./node_modules/gl-matrix/esm/index.js";
mat4.create();
Error:
main.ts:4:22 - error TS7016: Could not find a declaration file for module './node_modules/gl-matrix/esm/index.js'. '/.../node_modules/gl-matrix/esm/index.js' implicitly has an 'any' type.
4 import { mat4 } from "./node_modules/gl-matrix/esm/index.js";
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
If I comment out the last two lines, it compiles without errors.
To reproduce the issue, follow these steps:
npm install --save-dev typescript gl-matrix
tsconfig.json
:
{
"compilerOptions": {
"target": "ES2022",
"module": "ES2022",
"outDir": "./obj",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true,
"allowJs": true
}
}
t1.js
(simulates the content of index.js
from gl-matrix):
import * as t2 from "./t2.js"
export { t2 };
t2.js
(resembles the content of mat4.js
from gl-matrix):
export function hello() {
return "hello";
}