nha*_*nli 6 commonjs node.js typescript next.js ts-node
我想了解为什么当“module”设置为“esnext”时下面的代码无法编译。这是配置。
助手/seeder.ts
import * as faker from "faker";
import { User } from "../interfaces/user";
let user: User = {
userName: faker.name.firstName(),
};
console.log(user);
Run Code Online (Sandbox Code Playgroud)
文件夹结构
pages
components
helpers
|_run.ts
interfaces
|_user.d.ts
package.json
tsconfig.json
Run Code Online (Sandbox Code Playgroud)
包.json
{
"name": "with-typescript",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint"
},
"dependencies": {
"next": "11.1.2",
"react": "17.0.2",
"react-dom": "17.0.2", ....
},
"devDependencies": {
"@types/faker": "^5.5.8",
"@types/node": "^16.7.10",
"@types/react": "17.0.20",
"chai": "^4.3.4",
"esm": "^3.2.25",
"faker": "^5.5.3",
"typescript": "^4.4.2"
}
}
Run Code Online (Sandbox Code Playgroud)
tsconfig.json
{
"compilerOptions": {
"target": "ES2021",
"module": "esnext",
"baseUrl": ".",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"strict": true,
"esModuleInterop": true,
"noImplicitAny": true,
"strictNullChecks": true,
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"esModuleInterop": true,
"jsx": "preserve"
},
"include": [
"next-env.d.ts",
"**/*.ts",
"**/*.tsx",
"**/[*].ts",
"**/[*].tsx",
],
"exclude": ["node_modules"]
}
Run Code Online (Sandbox Code Playgroud)
来自 root 的命令
$ ts-node helpers/seeder.ts
案件
.tsconfig
.....
{
"module": "commonjs",
}
Run Code Online (Sandbox Code Playgroud)
干净的退出
案件
.tsconfig
.....
{
"module": "esnext",
}
Run Code Online (Sandbox Code Playgroud)
错误
import * as faker from "faker";
^^^^^^
SyntaxError: Cannot use import statement outside a module
at Object.compileFunction (node:vm:352:18)
at wrapSafe (node:internal/modules/cjs/loader:1031:15)
at Module._compile (node:internal/modules/cjs/loader:1065:27)
at Module.m._compile (/usr/local/lib/node_modules/ts-node/src/index.ts:1310:23)
at Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Object.require.extensions.<computed> [as .ts] (/usr/local/lib/node_modules/ts-node/src/index.ts:1313:12)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:79:12)
at main (/usr/local/lib/node_modules/ts-node/src/bin.ts:331:12)
Run Code Online (Sandbox Code Playgroud)
尝试过
添加"type": module
到 package.json。收到错误:
```
TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for
/root/helpers/run.ts
at new NodeError (node:internal/errors:371:5)
at Loader.defaultGetFormat [as _getFormat]
(node:internal/modules/esm/get_format:71:15)
at Loader.getFormat (node:internal/modules/esm/loader:105:42)
at Loader.getModuleJob (node:internal/modules/esm/loader:243:31)
at async Loader.import (node:internal/modules/esm/loader:177:17)
at async Object.loadESM (node:internal/process/esm_loader:68:5)
at async handleMainPromise (node:internal/modules/run_main:63:12) {
code: 'ERR_UNKNOWN_FILE_EXTENSION'
}
```
Run Code Online (Sandbox Code Playgroud)
添加"type": commonjs
到 package.json。无论如何,我不想恢复到 commonjs 并保留所有 es6 收到的错误:
SyntaxError: Cannot use import statement outside a module
at Object.compileFunction (node:vm:352:18)
at wrapSafe (node:internal/modules/cjs/loader:1031:15)
at Module._compile (node:internal/modules/cjs/loader:1065:27)
at Module.m._compile (/usr/local/lib/node_modules/ts-node/src/index.ts:1310:23)
at Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Object.require.extensions.<computed> [as .ts] (/usr/local/lib/node_modules/ts-
node/src/index.ts:1313:12)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain]
(node:internal/modules/run_main:79:12)
at main (/usr/local/lib/node_modules/ts-node/src/bin.ts:331:12)
Run Code Online (Sandbox Code Playgroud)