为什么 esm 加载程序找不到我的导入?
Why isn't my import being found by the esm loader?
我有一个小型快递服务器,在我的 bin/www.ts
中,我像这样导入我的 app.ts 文件:
import app from '../app';
当我构建我的项目并将它变成 JavaScript 时使用:tsc --project ./
然后 运行 它与 nodemon ./build/bin/www
我在我的控制台中收到一条错误消息:
internal/process/esm_loader.js:74
internalBinding('errors').triggerUncaughtException(
^
Error [ERR_MODULE_NOT_FOUND]:
Cannot find module '/Users/t86b/Desktop/Dev/Projects/TestServerProject/Server/build/app'
imported from /Users/t86b/Desktop/Dev/Projects/TestServerProject/Server/build/bin/www.js
该文件存在于我指定的位置,我已经检查并且我已经将 "type":"module"
添加到我的 package.json
文件中。我也从 app.ts 文件中删除了所有要求,但仍然没有。我不确定此时该做什么。这是我的 package.json
文件(简明扼要):
{
...
"scripts": {
"build": "tsc --project ./",
"start": "nodemon ./build/bin/www",
"start:dev": "nodemon -r ./bin/www.ts",
"tsc": "tsc",
"tsStart": "node ./build/bin/www"
},
...
"dependencies": {
...
"express": "^4.17.1",
"typescript": "^4.0.3"
},
"type": "module",
"devDependencies": {
...
"nodemon": "^2.0.7",
"ts-node": "^9.1.1"
}
}
我的ts.config
:
{
"compilerOptions": {
"target": "es2017",
"module": "ESNext",
"lib": ["ES2017"],
"outDir": "./build",
"rootDir": "./",
"strict": true,
"moduleResolution": "node",
"esModuleInterop": true,
/* Advanced Options */
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
}
}
如果有帮助,这是我的 app.ts,它也没有错误(为清楚起见进行了压缩):
import express from 'express';
import indexRouter from './routes/index';
...
let app = express();
app.use('/', indexRouter);
export default app;
如何让我的项目看到我的文件,以便我可以启动我的服务器?提前致谢,如果您需要更多详细信息,请告诉我。
@ASDFGerte 指出在 esm 中你必须 include the file's extension for relative imports。所以我能够通过更改来修复和 运行 我的代码:
import app from '../app';
到 import app from '../app.js';
我有一个小型快递服务器,在我的 bin/www.ts
中,我像这样导入我的 app.ts 文件:
import app from '../app';
当我构建我的项目并将它变成 JavaScript 时使用:tsc --project ./
然后 运行 它与 nodemon ./build/bin/www
我在我的控制台中收到一条错误消息:
internal/process/esm_loader.js:74
internalBinding('errors').triggerUncaughtException(
^
Error [ERR_MODULE_NOT_FOUND]:
Cannot find module '/Users/t86b/Desktop/Dev/Projects/TestServerProject/Server/build/app'
imported from /Users/t86b/Desktop/Dev/Projects/TestServerProject/Server/build/bin/www.js
该文件存在于我指定的位置,我已经检查并且我已经将 "type":"module"
添加到我的 package.json
文件中。我也从 app.ts 文件中删除了所有要求,但仍然没有。我不确定此时该做什么。这是我的 package.json
文件(简明扼要):
{
...
"scripts": {
"build": "tsc --project ./",
"start": "nodemon ./build/bin/www",
"start:dev": "nodemon -r ./bin/www.ts",
"tsc": "tsc",
"tsStart": "node ./build/bin/www"
},
...
"dependencies": {
...
"express": "^4.17.1",
"typescript": "^4.0.3"
},
"type": "module",
"devDependencies": {
...
"nodemon": "^2.0.7",
"ts-node": "^9.1.1"
}
}
我的ts.config
:
{
"compilerOptions": {
"target": "es2017",
"module": "ESNext",
"lib": ["ES2017"],
"outDir": "./build",
"rootDir": "./",
"strict": true,
"moduleResolution": "node",
"esModuleInterop": true,
/* Advanced Options */
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
}
}
如果有帮助,这是我的 app.ts,它也没有错误(为清楚起见进行了压缩):
import express from 'express';
import indexRouter from './routes/index';
...
let app = express();
app.use('/', indexRouter);
export default app;
如何让我的项目看到我的文件,以便我可以启动我的服务器?提前致谢,如果您需要更多详细信息,请告诉我。
@ASDFGerte 指出在 esm 中你必须 include the file's extension for relative imports。所以我能够通过更改来修复和 运行 我的代码:
import app from '../app';
到 import app from '../app.js';