I am having a problem particularly with the built-in node libraries (path, fs, assert). Here are the reproduction steps:
File > Open Folder, and choose the root core-demo folder.core/library/waitFor.tests.ts file.assert package in red, and says Cannot find module 'assert' or its corresponding type definitions.
The interesting part of this is that the code actually runs and works. I am running this test module from a tsx script defined in package.json: "test": "tsx ./test.ts".
test.ts:
import { default as waitForTests } from './library/waitFor.tests.js'
const run = async () => {
console.log('Running core tests...')
await waitForTests()
console.log('š core tests pass.')
}
run()
This code executes and performs as expected -- npm run test runs the test and the test passes, confirmed with console output:
Running core tests...
Running waitFor tests...
š waitFor tests pass.
š core tests pass.
So why is VS Code flagging the import as a problem when TypeScript and TSX have no problem finding and running the script?
tsconfig.json:
{
"compilerOptions": {
"allowUmdGlobalAccess": true, // Solves React problems. https://stackoverflow.com/a/76409663/6100384
"declaration": true,
"declarationMap": true,
"jsx": "react",
"lib": ["es2022", "dom"],
"module": "node16",
"moduleResolution": "node16",
"outDir": "./.compiled",
"skipLibCheck": true, // We don't want Typescript to check our pulled-in dependencies. It's been shown that our dependency authors can't be trusted to write good typings.
"sourceMap": true,
"strict": true,
"target": "es2020",
"verbatimModuleSyntax": true
},
"include": [
"./index.ts"
]
}
package.json:
{
"name": "core",
"version": "1.0.0",
"description": "A sample core project for use in other projects",
"author": "demo",
"license": "ISC",
"engines": {
"node": "18.15.0"
},
"main": "./.compiled/index.js",
"types": "./.compiled/index.d.ts",
"files":[
"./.compiled/**"
],
"type": "module",
"scripts": {
"build": "npm run clean && npm install && tsc && npm run test && npm pack",
"clean": "rm -rf ./.compiled && rm -rf ./node_modules && rm -rf ./package-lock.json",
"test": "tsx ./test.ts"
},
"dependencies": {
"@types/node": "18.15.11",
"@types/react": "18.0.31",
"@uppy/aws-s3": "3.3.1",
"@uppy/core": "3.5.1",
"@uppy/dashboard": "3.5.4",
"classnames": "2.3.2",
"next": "13.2.4",
"react": "18.2.0",
"react-dom": "18.2.0",
"tsx": "4.6.1",
"typescript": "5.2.2",
"uuid": "9.0.1"
}
}
I am trying to create an npm package that can be installed on either client-side browser JavaScript or run in Node.js. There is a mix of React and Node.js features that I am defining in the project. I started with TypeScript's library recommendation for most of the tsconfig.json settings, and tweaked & added things as I ran into them.
My package.json does include "@types/node": "18.15.11" in the dependencies section, as that is what I have found normally causes this problem in other projects, and I confirmed that there is a node_modules/@types/node/assert.d.ts file present on my filesystem and it sure looks like it's filled with a proper definition file.
What else am I missing? Why can't VS Code find this definition? I have already attempted the answers from Typescript - Cannot find module ... or its corresponding type declarations without success. Restarting VSCode does not have any effect.
Edit
I've created a sample project that illustrates the problem for anyone trying to reproduce -- maybe it is just my machine. There are instructions on the readme.md on the root.
My VSCode details are:
Version: 1.84.2 (user setup)
Commit: 1a5daa3a0231a0fbba4f14db7ec463cf99d7768e
Date: 2023-11-09T10:51:52.184Z
Electron: 25.9.2
ElectronBuildId: 24603566
Chromium: 114.0.5735.289
Node.js: 18.15.0
V8: 11.4.183.29-electron.0
OS: Windows_NT x64 10.0.22621
Was also able to reproduce this on another machine. Same issue.
Version: 1.84.1 (Universal)
Commit: 2b35e1e6d88f1ce073683991d1eff5284a32690f
Date: 2023-11-06T12:37:18.666Z
Electron: 25.9.2
ElectronBuildId: 24603566
Chromium: 114.0.5735.289
Node.js: 18.15.0
V8: 11.4.183.29-electron.0
OS: Darwin x64 21.6.0
Figured it out. My project filesystem structure was causing the problem. The folder structure looks like this:
/package.json << A convenience `npm run build` that builds all three projects
/readme.md
/core/*
/projectA/*
/projectB/*
In VS Code, I was opening the / root directory folder when working with this project. So VS Code was apparently expecting to find the Node.js types in the /node_modules folder (there are no dependencies in /package.json).
Strange, in my opinion, that VSCode had no trouble finding the regular dependencies in /core/node_modules, only having issues finding the Node.js built-ins.
The two solves I've found for this issue are:
/readme.md and /package.json files. Unfortunately after even many years of developers asking, VS Code refuses to add individual files to workspaces.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With