我有一个打字稿“测试”包在涡轮增压器和它的进出口打字本功能。
因为它是涡轮增压器,所以它被复制到节点_模块/测试中。
当我试图运行import {func} from "test"时。它给了我这个错误,测试包的SyntaxError: Cannot use import statement outside a module. Ts配置是。
{
"extends": "tsconfig/base.json",
"compilerOptions": {
"declaration": true,
"target": "es6",
"module": "commonjs",
"moduleResolution": "node",
"baseUrl": ".",
},
"include": ["**/*.ts"]
}此节点应用程序的Nodemon配置
{
"watch": ["src"],
"ignore": ["src/**/*.test.ts"],
"ext": "ts,mjs,js,json,graphql",
"exec": "tsc && node ./dist/index.js",
"legacyWatch": true
}但是,当我试图在nextjs项目中导入相同的内容时,其中包含在config中。
const withTM = require('next-transpile-modules')(['test'])它工作得很好。
在我看来,在节点服务器端。当我包括来自node_modules的那个node_modules的时候。不会被转移的。
有办法解决这个问题吗?
我还试着使用tsc构建包,但将dist文件夹保存在包中似乎并不理想。
发布于 2022-07-23 11:25:22
发布于 2022-09-19 12:40:09
这对我有用。
包装/试验/包装
{
"name": "test",
"version": "1.0.0",
"description": "",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"scripts": {
"build": "tsc",
"dev": "tsc-watch --onSuccess tsc ",
"postinstall": "tsc"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"tsc-watch": "^5.0.3"
}
}发布于 2022-10-30 17:05:46
对于节点服务器,我使用ts-node --transpile-only index.ts,它可以很好地处理.ts文件。
https://stackoverflow.com/questions/73077970
复制相似问题