Definitelytyped: Lodash submodules are not working anymore

Created on 26 Jan 2017  路  16Comments  路  Source: DefinitelyTyped/DefinitelyTyped

  • [x] I tried using the latest lodash/index.d.ts file in this repo and had problems.
  • [x] I tried using the latest stable version of tsc. https://www.npmjs.com/package/typescript
  • [x] I have a question that is inappropriate for StackOverflow. (Please ask any appropriate questions there).
  • [x] I want to talk about lodash/index.d.ts.

@mhegazy, @andy-ms
This PR #12995 breaks importing lodash submodules with ES6 imports:

import * as uniq from "lodash/uniq"
// or
import * as uniq from "lodash.uniq"

Gives me this error:

message: 'Module '"c:/Users/asvet/work/applications/frontend/node_modules/@types/lodash.uniq/index"' resolves to a non-module entity and cannot be imported using this construct.'

Unfortunately i'm compiling to ES2015 modules so import uniq = require("lodash/uniq") gives me other error

Most helpful comment

Why was the PR closed? I'm having the same issue.

All 16 comments

Related: Microsoft/TypeScript#5073.

import { sample, shuffle } from 'lodash' nor import * as _ from 'lodash' doesn't work anymore as well (TS compiles, but the resulting sample and shuffle variables are undefined, and _ is { __useDefault: true, default: [Object]})

Why was the PR closed? I'm having the same issue.

the namespace hack had issues with other libraries :(

@pocesar The NPM package doesn't use a default export. How did you install lodash?

@andy-ms through npm the usual way, only if the mix of Typescript nightly and babel are breaking the imports (target: "es6")

Some tool you're using is creating a __useDefault property on the imported module.

Had similar issue and import cloneDeep = require("lodash.clonedeep"); worked for me. Not sure it is related to OP's issue though.

@shuntksh It's related, but import a = require("b") construction doesn't work if you target to es6 modules, i.e. have "module": "es6" in your tsconfig.json, for example when using wepback2.

I'll try to fire another PR

i changed all the import {x} from "lodash"
export = x

to export {x} from "lodash"

Why does the compiler forbid import foo = require('bar') when the module target is "es2015"? It should just emit const foo = require('bar').

When the target is es6, we expect to emit an ES6 module, not a CommonJS one. You might be interested in lodash-es.

@andy-ms however, being a module does not forbid access to require under the node interop spec.

I guess it can be a problem if you are going to assume your module must work even on a browser that wouldn鈥檛 provide require.

@Kovensky I'm not familiar with the node interop spec; this feature might predate that. Do you have a link?

@andy-ms hm, never mind what I say; the new spec also removes access to require. I assume it became unnecessary for node after the introduction of dynamic import().

The current interop spec is at https://github.com/nodejs/node-eps/blob/master/002-es-modules.md; the version on master was outdated for quite a while but recently a new rewrite actually got merged.

I think this is fixed if you use esModuleInterop.

Was this page helpful?
0 / 5 - 0 ratings