`module 'tslib' cannot be found` when importing from a sibling directory
See original GitHub issueWhat 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:
git clone https://github.com/birtles/rollup-ts-mono
cd rollup-ts-mono/a
yarn install
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 butnpx 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 thenode_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
andbaseUrl
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.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:6
Top Results From Across the Web
"This syntax requires an imported helper but module 'tslib ...
9. After updating to Ng12, with tslib already installed and getting This syntax requires an imported helper but module 'tslib' cannot be found....
Read more >How to import module from sibling directory? : r/learnpython
I want to import module shell.py from test_shell.py. How do I do this? ... Install a Python package on NixOS but it is...
Read more >48 answers on StackOverflow to the most popular Angular ...
Angular: Can't find Promise, Map, Set and Iterator ... lodash-es npm package: (has a defaut export in lodash.js i the package root folder)...
Read more >Firebase JavaScript SDK Release Notes - Google
Fixed a typing issue caused by a missing constructor for OAuthProvider . Cloud Firestore. Added export fields to make firestore/lite available in Node.js....
Read more >Python3 import and project layout - Gaoping Huang's Blog
Note that this command is running from the project directory, not inside ... we may want to import a module/method from a sibling...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
First of all, what a great project! It makes building TypeScript libraries so much simpler! We use rollup and this plugin in our project https://github.com/TelestionTeam/telestion-client and after the initial setup it worked flawlessly.
After a dependabot upgrade (https://github.com/TelestionTeam/telestion-client/pull/344) for TypeScript v4.2 the plugin stopped working with
tslib
is not found or some parts are not exported (e. g.__spreadArray
). It is important to mention that our project is a monorepo so the complexity increases further and there may be other parts that can cause this issue. To compile the packages we need to call rollup in every package directory.Iβve dug around a bit in the issues and dependencies and found something interesting: After the installation, npm deduplicates the
tslib
package only in an earlier version (for usv1.14.1
) and installs the newer version (herev2.1.0
) in thenode_modules
folder for every package that needs it. You can verify it with the commandnpm ls tslib
. Ours looks like this:npm ls tslib output without explicitly installed tslib
Also I manually checked that and the installed versions are:
project_path/node_modules/tslib
v1.14.0
project_path/node_modules/rollup-plugin-typescript2/node_modules/tslib
v2.1.0
After explicity installing
tslib
vianpm i -D tslib
the deduplication for the older version is gone and the βworkingβ version is located directly in thenode_modules
of the project:npm ls tslib output with explicitly installed tslib
And the project transpiles again successfully.
I think, there is a resolution error either in the rollup-plugin or in rollup itself which leads to resolving to the package directly in the projects
node_modules
and not the installed in thenode_modules
of the installed package. Unfortunately I am not familiar with the code base of both projects so itβs only a guess.Any ideas where this can happen?
Just for reference, the configuration files and the logs of our run:
package.json
rollup.config.js
tsconfig.build.json
build log without explicitly installed tslib
build log with explicitly installed tslib
Installed/used tools:
node --version
v14.15.4
npm --version
v6.14.11
rollup --version
v2.39.0
FWIW, I think I may have worked around this by adding a
package.json
tocommon
, addingtslib
as a dependency there, and adding a preinstall step to each of the sibling directories to make them runnpm install
incommon
if it exists (since it wonβt when run undersam build
).