Skip to content

Commit

Permalink
fix: ts-node & lodash & esm ✅
Browse files Browse the repository at this point in the history
  • Loading branch information
xgqfrms committed Mar 22, 2023
1 parent b0be036 commit 9c9fbd1
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 10 deletions.
23 changes: 21 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,30 @@ $ npx tsc --init
## dev

```sh
# cjs
$ yarn dev
# $ yarn esm
$ yarn app

$ npx ts-node ./src/index.ts
$ npx ts-node ./src/app.ts

```

# esm
$ yarn dev-esm
$ yarn app-esm

$ npx ts-node-esm ./src/index.ts
$ npx ts-node-esm ./src/app.ts

```

> fefs
https://typestrong.org/ts-node/docs/imports/

https://github.com/TypeStrong/ts-node/issues/1062#issuecomment-1094296803

https://github.com/xgqfrms/one-line-awesome-js-code/issues/1

https://www.cnblogs.com/xgqfrms/p/17242430.html#5160592

2 changes: 1 addition & 1 deletion env.js → env.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// default
const NODE_ENV = 'dev';
module.exports = NODE_ENV;
export default NODE_ENV;
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"main": "./src/index.ts",
"type": "module",
"scripts": {
"esm": "npx ts-node-esm ./src/index.ts",
"dev-esm": "npx ts-node-esm ./src/index.ts",
"app-esm": "npx ts-node-esm ./src/app.ts",
"dev": "npx ts-node ./src/index.ts",
"app": "npx ts-node ./src/app.ts",
"test": "echo \"Error: no test specified\" && exit 1"
Expand Down
8 changes: 4 additions & 4 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

import {toString as ToString} from "lodash-es";

import toString from "lodash-es/toString";
// import toString from "lodash-es/toString";


type Obj = {
Expand All @@ -15,9 +15,9 @@ const obj: Obj = {
};

const str1 = ToString(obj);
const str2 = toString(obj);
// const str2 = toString(obj);

console.log(`ToString =`, str1);
console.log(`toString =`, str2);
console.log(`ToString =`, JSON.stringify(str1));
// console.log(`toString =`, str2);

export {}
16 changes: 15 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@

import _ from "lodash";
// import _, {toString} from "lodash";

const { toString } = _;

/*
SyntaxError: Named export 'toString' not found.
The requested module 'lodash' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:
import pkg from 'lodash';
const { toString } = pkg;
*/

/*
(node:19404) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
Expand All @@ -14,7 +28,7 @@ SyntaxError: Cannot use import statement outside a module
*/

// ✅
import toString from "lodash/toString";
// import toString from "lodash/toString";

// import toString from "lodash/tostring";
// const toString = require("lodash/toString");
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"skipLibCheck": true
},
// "ts-node": {
// "esm": true
// "esm": true
// },
// "lib": ["esnext"]
}

0 comments on commit 9c9fbd1

Please sign in to comment.