我有一个ES6模块right.mjs。将其作为参数执行即可node:
$ node --version
v8.10.0
$ node --experimental-modules right.mjs
(node:4492) ExperimentalWarning: The ESM module loader is experimental.
executing right module
Run Code Online (Sandbox Code Playgroud)
executing right module 是模块的输出。
与此相反,REPL中的以下输入等待进一步的输入:
$ node --experimental-modules
> (node:4526) ExperimentalWarning: The ESM module loader is experimental.
> import 'right.mjs';
...
Run Code Online (Sandbox Code Playgroud)
我不明白为什么。
与:
> import './right.mjs';
...
Run Code Online (Sandbox Code Playgroud)
尝试require导致:
> require('./right.mjs');
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /home/xxx/right.mjs
at Object.Module._extensions..mjs (module.js:686:11)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Module.require …Run Code Online (Sandbox Code Playgroud) 我一直遇到节点 js 的问题。
这是相关代码(app.ts):
const express = require("express");
const auth = require("./service/auth");
import { Request, Response } from "express";
import User from "./model/User";
import con from "./config/db";
const port = 3000;
const app = express();
app.use(express.json());
...
Run Code Online (Sandbox Code Playgroud)
我得到的错误是:
(node:16524) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
C:\Users\010bo\OneDrive\Documents\GitHub\rpg-backend\app\app.ts:4
import { Request, Response } from "express";
^^^^^^
SyntaxError: Cannot …Run Code Online (Sandbox Code Playgroud) 我使用 Nodejs 和 TypeScript 运行一个应用程序。这是我的第一步。虽然我想在 heroku 中部署后端应用程序,但出现错误(本地主机中没有错误):
警告:要加载 ES 模块,请在 package.json 中设置 "type": "module" 或使用 .mjs 扩展名。
当然我已经设置了“type”:“module”,但是这次我遇到了这个错误:
[ERR_UNKNOWN_FILE_EXTENSION]:未知的文件扩展名“.ts”
我已经尝试了在与这些错误相关的不同线程中提出的多种解决方案
不幸的是,它们都不起作用(提醒一下,当我没有使用 type:module 时它在本地起作用)
节点版本:v14.15.4
这是我的 tsconfig.json
{
"compilerOptions": {
/* Basic Options */
"target": "ESNext",
/* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' …Run Code Online (Sandbox Code Playgroud) 我一直在尝试在 nodejs 中导入一个用 typescript 编写的 ESM 模块。但我收到以下错误:
An import path cannot end with a '.ts' extension.
Run Code Online (Sandbox Code Playgroud)
实用程序
export class Util {
constructor ( ) {
}
log(msg) {
console.log(msg)
}
}
Run Code Online (Sandbox Code Playgroud)
索引.ts
import {log} from './Util.ts'
log(task.query.criteria, payload.parameters)
Run Code Online (Sandbox Code Playgroud)
我也在"type":"module"里面添加了package.json
我将 .ts 更改为 .js 只是为了看看它是否有效,然后我得到:
Object.defineProperty(exports, "__esModule", { value: true }); ^
ReferenceError: exports is not defined
at file:///C:/Users/abc/NestJsPOC/NestPOC/dist/main.js:2:23
Run Code Online (Sandbox Code Playgroud)
配置文件
{
"compilerOptions": {
"module": "commonjs",
"declaration": true,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"target": "es2017",
"sourceMap": …Run Code Online (Sandbox Code Playgroud)