question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error when import some type from another package

See original GitHub issue

I have an issue, when I import some type from package (linked or regular) problem like: can’t resolve “path/to/my/node_modules/module” or “my type” is not exported from ‘path/to/my/module’.

Error:

babel-ts-decorator-error@1.0.0 start /Users/edelgarat/Documents/projects/babel-ts-decorator-error webpack

[webpack-cli] Compilation finished asset bundle.js 59 KiB [emitted] (name: main) runtime modules 1.13 KiB 5 modules cacheable modules 52 KiB ./src/index.ts 1.95 KiB [built] [code generated] ./node_modules/reflect-metadata/Reflect.js 50 KiB [built] [code generated]

ERROR in ./src/index.ts 10:0-73 Module not found: Error: Can’t resolve ‘test-package/file-with-declaration’ in ‘/Users/edelgarat/Documents/projects/babel-ts-decorator-error/src’

webpack 5.6.0 compiled with 1 error in 631 ms npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! babel-ts-decorator-error@1.0.0 start: webpack npm ERR! Exit status 1 npm ERR! npm ERR! Failed at the babel-ts-decorator-error@1.0.0 start script. npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in: npm ERR! /Users/edelgarat/.npm/_logs/2020-11-22T18_10_36_504Z-debug.log

Notes:

  1. When i remove ‘babel-plugin-transform-typescript-metadata’, this problem is missing
  2. When i remove “@decorator”, this problem is missing

Entry point file:

import {SomeTypeDeclaration} from "test-package/file-with-declaration";

const someDecorator: PropertyDecorator = (target, propertyKey) => {}

class Test {
	@someDecorator
	someProperty: SomeTypeDeclaration = 5;
}

Error:

Module not found: Error: Can’t resolve ‘test-package/file-with-declaration’ in ‘/Users/edelgarat/Documents/projects/babel-ts-decorator-error/src’

Webpack babel rule:

{
	test: /\.ts$/,
	use: {
		loader: "babel-loader",
		options: {
			presets: [
				["@babel/preset-typescript", {}]
			],
			plugins: [
				["@babel/plugin-proposal-decorators", { "legacy": true }],
				["@babel/plugin-proposal-class-properties", { "loose" : true }],
				"babel-plugin-transform-typescript-metadata",
			]
		}
	}
}

Demo project: https://github.com/edelgarat/babel-plugin-transform-typescript-metadata-import-error

Test stand:

  • Mac OS 10.15.4
  • Node v14.2.0

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
edelgaratcommented, Dec 13, 2020

I updated the repository to see the problem better

new code:

import { IKeyValueMap } from "mobx/lib/types/observablemap";

class myClass {
	@observable
	a: IKeyValueMap = {};
}

new error:

ERROR in ./src/index.ts 11:0-60
Module not found: Error: Can't resolve 'mobx/lib/types/observablemap'

if you compile through typescript, then for the property “a” property this will be:

__decorate([
    mobx_1.observable,
    __metadata("design:type", Object)
], B.prototype, "a", void 0);

but, as I understand it, this plugin (or babel) thinks that “IKeyValueMap” is an entity that can be accessed from js

0reactions
leonardfactorycommented, Mar 18, 2021

Using values as types seems an issue linked to #46. Could you try to use import type { ... } in order to give babel a hint? Since we don’t have access to TS Checker, type information is not accessible so we cannot distinguish between interfaces and concrete types.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Relative import error when importing from different package
You should not use a relative path to add to "sys.path" because it may not be relative to the module in which it...
Read more >
[ROS] How To Import a Python Module From Another Package
In this tutorial I'll show you how to import a Python module from another package. ROS allows you to do that, and as...
Read more >
Traps for the Unwary in Python's Import System
The reason this is problematic is that every module in that directory is now potentially accessible under two different names: as a top...
Read more >
Cannot import a package with "type": "module" from cjs even if ...
So the issue happening here is that the line "types": "./index.d.ts" declares a types file that is inferred as an ESM module because...
Read more >
Modules: Packages | Node.js v19.3.0 Documentation
Conditional exports provide a way to map to different paths depending on certain conditions. They are supported for both CommonJS and ES module...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found