Skip to content
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

module 'tslib' cannot be found when importing from a sibling directory #231

Closed
birtles opened this issue Jun 11, 2020 · 6 comments
Closed
Labels
kind: support Asking for support with something or a specific use case scope: dependencies Issues or PRs about updating a dependency scope: upstream Issue in upstream dependency solution: tsc behavior This is tsc's behavior as well, so this is not a bug with this plugin solution: workaround available There is a workaround available for this issue topic: monorepo / symlinks Related to monorepos and/or symlinks (Lerna, Yarn, PNPM, Rush, etc) topic: TS version Related to a change in a TS version

Comments

@birtles
Copy link

birtles commented Jun 11, 2020

What happens and why it is wrong

This is similar to issue #12, #214, and #216 but none of the solutions mentioned there worked for me and I have clear steps to reproduce so I hope this bug is helpful.

Steps to reproduce:

  1. git clone https://github.com/birtles/rollup-ts-mono
  2. cd rollup-ts-mono/a
  3. yarn install
  4. yarn build

Expected results: it builds successfully.

Actual results:

[!] (plugin rpt2) Error: ../common/func.ts:1:24 - error TS2354: This syntax requires an imported helper but module 'tslib' cannot be found.

1 export function func({ a }: { a: string }) {

Note that:

  • npx tsc works
  • A more complex setup with ts-jest works (i.e. yarn test works but npx rollup -c produces the above error)
  • This setup deliberately doesn't include node-resolve etc. since I don't want the external dependencies to be bundled into the result. (This is for building an AWS lambda function where the node_modules will be uploaded along with the compiled file and keeping the compiled file smaller makes debugging easier.)
  • The directory structure is quite inflexible due to this all being run through sam-cli
  • It all worked fine until I started to use fancy syntax in the common/func.ts file
  • Adding a root tsconfig.json and extending from it doesn't seem to make a difference
  • Tweaking paths and baseUrl as per this comment can seem to help, but appears to break other things.

Environment

Node v12.14.0 running on Ubuntu 18.04 under WSL 1.

Versions

  • typescript: 3.9.5
  • rollup: 2.15.0
  • rollup-plugin-typescript2: 0.27.1

rollup.config.js

import typescript from 'rollup-plugin-typescript2';

export default {
  input: './index.ts',
  output: {
    file: 'index.js',
    format: 'cjs',
  },
  plugins: [
    typescript({
      include: ['*.ts', '../common/*.ts'],
      verbosity: 3,
    }),
  ],
};

tsconfig.json

{
  "compilerOptions": {
    "alwaysStrict": true,
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    "module": "ESNext",
    "moduleResolution": "node",
    "noImplicitAny": true,
    "noImplicitReturns": true,
    "noUnusedLocals": true,
    "pretty": true,
    "strictNullChecks": true,
    "target": "es2018"
  },
  "include": [
    "./*.ts",
    "../common/*.ts"
  ],
  "exclude": [
    "node_modules"
  ]
}

package.json

{
  "name": "a",
  "version": "1.0.0",
  "main": "index.ts",
  "license": "MIT",
  "scripts": {
    "build": "rollup -c"
  },
  "devDependencies": {
    "rollup": "^2.15.0",
    "rollup-plugin-typescript2": "^0.27.1",
    "typescript": "^3.9.5"
  }
}

plugin output with verbosity 3

log:
yarn run v1.22.4
$ rollup -c

./index.ts → index.js...
rpt2: built-in options overrides: {
    "noEmitHelpers": false,
    "importHelpers": true,
    "noResolve": false,
    "noEmit": false,
    "inlineSourceMap": false,
    "outDir": "/c/Users/Brian/rollup-ts-mono/a/node_modules/.cache/rollup-plugin-typescript2/placeholder",
    "moduleResolution": 2,
    "allowNonTsExtensions": true
}
rpt2: parsed tsconfig: {
    "options": {
        "alwaysStrict": true,
        "allowSyntheticDefaultImports": true,
        "esModuleInterop": true,
        "module": 99,
        "moduleResolution": 2,
        "noImplicitAny": true,
        "noImplicitReturns": true,
        "noUnusedLocals": true,
        "pretty": true,
        "strictNullChecks": true,
        "target": 5,
        "configFilePath": "/c/Users/Brian/rollup-ts-mono/a/tsconfig.json",
        "noEmitHelpers": false,
        "importHelpers": true,
        "noResolve": false,
        "noEmit": false,
        "inlineSourceMap": false,
        "outDir": "/c/Users/Brian/rollup-ts-mono/a/node_modules/.cache/rollup-plugin-typescript2/placeholder",
        "allowNonTsExtensions": true
    },
    "fileNames": [
        "/c/Users/Brian/rollup-ts-mono/a/index.ts",
        "/c/Users/Brian/rollup-ts-mono/common/func.ts"
    ],
    "typeAcquisition": {
        "enable": false,
        "include": [],
        "exclude": []
    },
    "raw": {
        "compilerOptions": {
            "alwaysStrict": true,
            "allowSyntheticDefaultImports": true,
            "esModuleInterop": true,
            "module": "ESNext",
            "moduleResolution": "node",
            "noImplicitAny": true,
            "noImplicitReturns": true,
            "noUnusedLocals": true,
            "pretty": true,
            "strictNullChecks": true,
            "target": "es2018"
        },
        "include": [
            "./*.ts",
            "../common/*.ts"
        ],
        "exclude": [
            "node_modules"
        ],
        "compileOnSave": false
    },
    "errors": [],
    "wildcardDirectories": {
        "/c/Users/Brian/rollup-ts-mono/a": 0,
        "/c/Users/Brian/rollup-ts-mono/common": 0
    },
    "compileOnSave": false,
    "configFileSpecs": {
        "includeSpecs": [
            "./*.ts",
            "../common/*.ts"
        ],
        "excludeSpecs": [
            "node_modules"
        ],
        "validatedIncludeSpecs": [
            "./*.ts",
            "../common/*.ts"
        ],
        "validatedExcludeSpecs": [
            "node_modules"
        ],
        "wildcardDirectories": {
            "/c/Users/Brian/rollup-ts-mono/a": 0,
            "/c/Users/Brian/rollup-ts-mono/common": 0
        }
    }
}
rpt2: typescript version: 3.9.5
rpt2: tslib version: 1.11.2
rpt2: rollup version: 2.15.0
rpt2: rollup-plugin-typescript2 version: 0.27.1
rpt2: plugin options:
{
    "include": [
        "*.ts",
        "../common/*.ts"
    ],
    "verbosity": 3,
    "check": true,
    "clean": false,
    "cacheRoot": "/c/Users/Brian/rollup-ts-mono/a/node_modules/.cache/rollup-plugin-typescript2",
    "exclude": [
        "*.d.ts",
        "**/*.d.ts"
    ],
    "abortOnError": true,
    "rollupCommonJSResolveHack": false,
    "useTsconfigDeclarationDir": false,
    "tsconfigOverride": {},
    "transformers": [],
    "tsconfigDefaults": {},
    "objectHashIgnoreUnknownHack": false,
    "cwd": "/c/Users/Brian/rollup-ts-mono/a",
    "typescript": "version 3.9.5"
}
rpt2: rollup config:
{
    "external": [],
    "input": "./index.ts",
    "plugins": [
        {
            "name": "rpt2"
        },
        {
            "name": "stdin"
        }
    ],
    "output": [
        {
            "file": "index.js",
            "format": "cjs",
            "plugins": []
        }
    ]
}
rpt2: tsconfig path: /c/Users/Brian/rollup-ts-mono/a/tsconfig.json
rpt2: included:
[
    "*.ts",
    "../common/*.ts"
]
rpt2: excluded:
[
    "*.d.ts",
    "**/*.d.ts"
]
rpt2: Ambient types:
rpt2:     /c/Users/Brian/rollup-ts-mono/a/node_modules/@types/estree/index.d.ts
rpt2: ambient types changed, redoing all semantic diagnostics
rpt2: transpiling '/c/Users/Brian/rollup-ts-mono/a/index.ts'
rpt2:     cache: '/c/Users/Brian/rollup-ts-mono/a/node_modules/.cache/rollup-plugin-typescript2/rpt2_86652b882e81b012e213895c8bb1ed1d48aba191/code/cache/42dd2afd58080ad46e2f26195c4b76ce6ba22e63'
rpt2:     cache miss
rpt2:     cache: '/c/Users/Brian/rollup-ts-mono/a/node_modules/.cache/rollup-plugin-typescript2/rpt2_86652b882e81b012e213895c8bb1ed1d48aba191/syntacticDiagnostics/cache/42dd2afd58080ad46e2f26195c4b76ce6ba22e63'
rpt2:     cache miss
rpt2:     cache: '/c/Users/Brian/rollup-ts-mono/a/node_modules/.cache/rollup-plugin-typescript2/rpt2_86652b882e81b012e213895c8bb1ed1d48aba191/semanticDiagnostics/cache/42dd2afd58080ad46e2f26195c4b76ce6ba22e63'
rpt2:     cache miss
rpt2: dependency '/c/Users/Brian/rollup-ts-mono/common/func.ts'
rpt2:     imported by '/c/Users/Brian/rollup-ts-mono/a/index.ts'
rpt2: resolving '../common/func' imported by '/c/Users/Brian/rollup-ts-mono/a/index.ts'
rpt2:     to '/c/Users/Brian/rollup-ts-mono/common/func.ts'
rpt2: transpiling '/c/Users/Brian/rollup-ts-mono/common/func.ts'
rpt2:     cache: '/c/Users/Brian/rollup-ts-mono/a/node_modules/.cache/rollup-plugin-typescript2/rpt2_86652b882e81b012e213895c8bb1ed1d48aba191/code/cache/154c4f09b6a8aebbd439964a86673337ca5a9381'
rpt2:     cache miss
rpt2:     cache: '/c/Users/Brian/rollup-ts-mono/a/node_modules/.cache/rollup-plugin-typescript2/rpt2_86652b882e81b012e213895c8bb1ed1d48aba191/syntacticDiagnostics/cache/154c4f09b6a8aebbd439964a86673337ca5a9381'
rpt2:     cache miss
rpt2:     cache: '/c/Users/Brian/rollup-ts-mono/a/node_modules/.cache/rollup-plugin-typescript2/rpt2_86652b882e81b012e213895c8bb1ed1d48aba191/semanticDiagnostics/cache/154c4f09b6a8aebbd439964a86673337ca5a9381'
rpt2:     cache miss
[!] (plugin rpt2) Error: ../common/func.ts:1:24 - error TS2354: This syntax requires an imported helper but module 'tslib' cannot be found.

1 export function func({ a }: { a: string }) {
                         ~

../common/func.ts
Error: ../common/func.ts:1:24 - error TS2354: This syntax requires an imported helper but module 'tslib' cannot be found.

1 export function func({ a }: { a: string }) {
                         ~

    at error (/c/Users/Brian/rollup-ts-mono/a/node_modules/rollup/dist/shared/rollup.js:213:30)
    at throwPluginError (/c/Users/Brian/rollup-ts-mono/a/node_modules/rollup/dist/shared/rollup.js:17117:12)
    at Object.error (/c/Users/Brian/rollup-ts-mono/a/node_modules/rollup/dist/shared/rollup.js:17911:24)
    at Object.error (/c/Users/Brian/rollup-ts-mono/a/node_modules/rollup/dist/shared/rollup.js:17290:38)
    at RollupContext.error (/c/Users/Brian/rollup-ts-mono/a/node_modules/rollup-plugin-typescript2/dist/rollup-plugin-typescript2.cjs.js:17195:30)
    at /c/Users/Brian/rollup-ts-mono/a/node_modules/rollup-plugin-typescript2/dist/rollup-plugin-typescript2.cjs.js:24992:19
    at arrayEach (/c/Users/Brian/rollup-ts-mono/a/node_modules/rollup-plugin-typescript2/dist/rollup-plugin-typescript2.cjs.js:535:11)
    at forEach (/c/Users/Brian/rollup-ts-mono/a/node_modules/rollup-plugin-typescript2/dist/rollup-plugin-typescript2.cjs.js:9361:14)
    at printDiagnostics (/c/Users/Brian/rollup-ts-mono/a/node_modules/rollup-plugin-typescript2/dist/rollup-plugin-typescript2.cjs.js:24968:5)
    at Object.transform (/c/Users/Brian/rollup-ts-mono/a/node_modules/rollup-plugin-typescript2/dist/rollup-plugin-typescript2.cjs.js:29071:17)

error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
@birtles
Copy link
Author

birtles commented Jun 11, 2020

  • It all worked fine until I started to use fancy syntax in the common/func.ts file

Just to clarify what I mean by this, if I change:

export function func({ a }: { a: string }) {
  console.log(`Hi ${a}`);
}

to:

export function func(a: string) {
  console.log(`Hi ${a}`);
}

it all works as intended.

@birtles
Copy link
Author

birtles commented Jun 11, 2020

FWIW, I think I may have worked around this by adding a package.json to common, adding tslib as a dependency there, and adding a preinstall step to each of the sibling directories to make them run npm install in common if it exists (since it won't when run under sam build).

@ezolenko ezolenko added solution: workaround available There is a workaround available for this issue kind: bug Something isn't working properly labels Aug 7, 2020
@SagnikPradhan
Copy link

SagnikPradhan commented Sep 16, 2020

I guess it is the same reason why my wrapper for rollup throws the same error when I changed tslib from a peer dependency to dependency. For side note, I am not using this plugin. 😅 It was the official plugin.

@fussel178

This comment was marked as off-topic.

fussel178 added a commit to wuespace/telestion-client that referenced this issue Feb 26, 2021
fussel178 added a commit to wuespace/telestion-client that referenced this issue Feb 26, 2021
@agilgur5 agilgur5 changed the title Module 'tslib' cannot be found when importing from a sibling directory module 'tslib' cannot be found when importing from a sibling directory Jun 9, 2022
@agilgur5 agilgur5 added topic: monorepo / symlinks Related to monorepos and/or symlinks (Lerna, Yarn, PNPM, Rush, etc) scope: upstream Issue in upstream dependency topic: TS version Related to a change in a TS version solution: out-of-scope This is out of scope for this project scope: dependencies Issues or PRs about updating a dependency kind: support Asking for support with something or a specific use case and removed kind: bug Something isn't working properly labels Jun 21, 2022
@agilgur5
Copy link
Collaborator

agilgur5 commented Jun 21, 2022

  • It all worked fine until I started to use fancy syntax in the common/func.ts file

This makes sense as newer syntax is not implemented in older versions of ECMAScript and so a "helper" is needed to basically "polyfill" the syntax. TS has its helpers in the tslib library.
Note that Babel has equivalent functionality for older versions with @babel/runtime, which is a similar set of helpers.

Basically, if the syntax you're using isn't supported by your target version, a helper must be imported from tslib.

  • git clone https://github.com/birtles/rollup-ts-mono
  • Tweaking paths and baseUrl as per this comment can seem to help, but appears to break other things.
  • typescript: 3.9.5

I checked out the repo and was able to reproduce this. I read the upstream issue linked and this seems to have been solved within TS itself later in that same issue: microsoft/TypeScript#37991 (comment) (PR: microsoft/TypeScript#43166).

Upgrading to a newer version of TS (at least v4.3? per the milestones mentioned there) makes this issue go away. For instance, I upgraded to TS 4.6.5 in your project and there was no error anymore.

It's written there that the issue is due to a "weak check" for the syntax. Your target, es2018, doesn't actually need a helper -- you'll see after running yarn build that the resulting bundled index.js doesn't actually have any imports.

FWIW, I think I may have worked around this by adding a package.json to common, adding tslib as a dependency there

This also makes sense as a workaround, as then TS would be able to find tslib in common.
Otherwise, you don't have a root package.json with tslib in the root's node_modules, nor one in common, so Node's resolution algorithm would be unable to find it. Node's resolution algorithm doesn't check sibling directories, it checks parent directories.

@agilgur5

This comment was marked as resolved.

@agilgur5 agilgur5 added solution: tsc behavior This is tsc's behavior as well, so this is not a bug with this plugin and removed solution: out-of-scope This is out of scope for this project labels Jun 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: support Asking for support with something or a specific use case scope: dependencies Issues or PRs about updating a dependency scope: upstream Issue in upstream dependency solution: tsc behavior This is tsc's behavior as well, so this is not a bug with this plugin solution: workaround available There is a workaround available for this issue topic: monorepo / symlinks Related to monorepos and/or symlinks (Lerna, Yarn, PNPM, Rush, etc) topic: TS version Related to a change in a TS version
Projects
None yet
Development

No branches or pull requests

5 participants