Skip to content

Unable to import ts-files outside of project dir #20374

@tx46

Description

@tx46

Bug report

Describe the bug

I have three projects; A, B and C. C is the "common" project that is used by both A (next-js) and B (pure ts using tsc). C transpiles fine, as does B.

Importing code from C in project A (next-js) gives syntax errors, as if the .ts-files are not being treated as TypeScript:

Module parse failed: Unexpected token (62:58)

The unexpected token happens to be a colon denoting a type (i.e., varName: ...). It works and imorts fine from B and it's definitely a valid TS source file.

To Reproduce

Steps to reproduce the behavior, please provide code snippets or a repository:

mkdir bug-repro
cd bug-repro
mkdir b
touch b/tsconfig.json
echo export default function foo(a: number) { return 2 * a; } > b/foo.ts
npx create-next-app
cd my-app
touch tsconfig.json

Add the following just under "import styles ..." in pages/index.js:

import foo from "../../b/foo";
foo(1);
mv pages/index.js pages/index.tsx
npm install --save-dev typescript @types/react @types/node
npm run dev
ctrl-c

Add this to my-app/tsconfig.json:

"references": [{"path":"../b"}]

Add this as b/tsconfig.json:

{
  "compilerOptions": {
    "composite": true,
    "declaration": true,
    "declarationMap": true,
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "module": "commonjs",
    "noImplicitAny": true,
    "outDir": "build",
    "rootDir": "src",
    "skipLibCheck": true,
    "strict": true,
    "target": "es5"
  },
  "references": []
}
npm run dev

Visit localhost:3000

Check error in terminal:

error - ../b/foo.ts 1:29
Module parse failed: Unexpected token (1:29)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
> export default function foo(a: number) { return 2 * a; }
|

Expected behavior

The app imports foo.ts and runs fine.

Screenshots

image

System information

  • OS: Windows 10 20H2 19042.685 x64
  • Browser:
  • Version of Next.js: 10.0.0.3
  • Version of Node.js: v12.19.0
  • Deployment: npm run dev

Additional context

n/a

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions